exceptions.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. """
  2. requests.exceptions
  3. ~~~~~~~~~~~~~~~~~~~
  4. This module contains the set of Requests' exceptions.
  5. """
  6. from urllib3.exceptions import HTTPError as BaseHTTPError
  7. from .compat import JSONDecodeError as CompatJSONDecodeError
  8. class RequestException(IOError):
  9. """There was an ambiguous exception that occurred while handling your
  10. request.
  11. """
  12. def __init__(self, *args, **kwargs):
  13. """Initialize RequestException with `request` and `response` objects."""
  14. response = kwargs.pop("response", None)
  15. self.response = response
  16. self.request = kwargs.pop("request", None)
  17. if response is not None and not self.request and hasattr(response, "request"):
  18. self.request = self.response.request
  19. super().__init__(*args, **kwargs)
  20. class InvalidJSONError(RequestException):
  21. """A JSON error occurred."""
  22. class JSONDecodeError(InvalidJSONError, CompatJSONDecodeError):
  23. """Couldn't decode the text into json"""
  24. def __init__(self, *args, **kwargs):
  25. """
  26. Construct the JSONDecodeError instance first with all
  27. args. Then use it's args to construct the IOError so that
  28. the json specific args aren't used as IOError specific args
  29. and the error message from JSONDecodeError is preserved.
  30. """
  31. CompatJSONDecodeError.__init__(self, *args)
  32. InvalidJSONError.__init__(self, *self.args, **kwargs)
  33. class HTTPError(RequestException):
  34. """An HTTP error occurred."""
  35. class ConnectionError(RequestException):
  36. """A Connection error occurred."""
  37. class ProxyError(ConnectionError):
  38. """A proxy error occurred."""
  39. class SSLError(ConnectionError):
  40. """An SSL error occurred."""
  41. class Timeout(RequestException):
  42. """The request timed out.
  43. Catching this error will catch both
  44. :exc:`~requests.exceptions.ConnectTimeout` and
  45. :exc:`~requests.exceptions.ReadTimeout` errors.
  46. """
  47. class ConnectTimeout(ConnectionError, Timeout):
  48. """The request timed out while trying to connect to the remote server.
  49. Requests that produced this error are safe to retry.
  50. """
  51. class ReadTimeout(Timeout):
  52. """The server did not send any data in the allotted amount of time."""
  53. class URLRequired(RequestException):
  54. """A valid URL is required to make a request."""
  55. class TooManyRedirects(RequestException):
  56. """Too many redirects."""
  57. class MissingSchema(RequestException, ValueError):
  58. """The URL scheme (e.g. http or https) is missing."""
  59. class InvalidSchema(RequestException, ValueError):
  60. """The URL scheme provided is either invalid or unsupported."""
  61. class InvalidURL(RequestException, ValueError):
  62. """The URL provided was somehow invalid."""
  63. class InvalidHeader(RequestException, ValueError):
  64. """The header value provided was somehow invalid."""
  65. class InvalidProxyURL(InvalidURL):
  66. """The proxy URL provided is invalid."""
  67. class ChunkedEncodingError(RequestException):
  68. """The server declared chunked encoding but sent an invalid chunk."""
  69. class ContentDecodingError(RequestException, BaseHTTPError):
  70. """Failed to decode response content."""
  71. class StreamConsumedError(RequestException, TypeError):
  72. """The content for this response was already consumed."""
  73. class RetryError(RequestException):
  74. """Custom retries logic failed"""
  75. class UnrewindableBodyError(RequestException):
  76. """Requests encountered an error when trying to rewind a body."""
  77. # Warnings
  78. class RequestsWarning(Warning):
  79. """Base warning for Requests."""
  80. class FileModeWarning(RequestsWarning, DeprecationWarning):
  81. """A file was opened in text mode, but Requests determined its binary length."""
  82. class RequestsDependencyWarning(RequestsWarning):
  83. """An imported dependency doesn't match the expected version range."""