exceptions.py 4.5 KB

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