exceptions.py 3.4 KB

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