exceptions.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. ## Base Exceptions
  2. class HTTPError(Exception):
  3. "Base exception used by this module."
  4. pass
  5. class HTTPWarning(Warning):
  6. "Base warning used by this module."
  7. pass
  8. class PoolError(HTTPError):
  9. "Base exception for errors caused within a pool."
  10. def __init__(self, pool, message):
  11. self.pool = pool
  12. HTTPError.__init__(self, "%s: %s" % (pool, message))
  13. def __reduce__(self):
  14. # For pickling purposes.
  15. return self.__class__, (None, None)
  16. class RequestError(PoolError):
  17. "Base exception for PoolErrors that have associated URLs."
  18. def __init__(self, pool, url, message):
  19. self.url = url
  20. PoolError.__init__(self, pool, message)
  21. def __reduce__(self):
  22. # For pickling purposes.
  23. return self.__class__, (None, self.url, None)
  24. class SSLError(HTTPError):
  25. "Raised when SSL certificate fails in an HTTPS connection."
  26. pass
  27. class ProxyError(HTTPError):
  28. "Raised when the connection to a proxy fails."
  29. pass
  30. class DecodeError(HTTPError):
  31. "Raised when automatic decoding based on Content-Type fails."
  32. pass
  33. class ProtocolError(HTTPError):
  34. "Raised when something unexpected happens mid-request/response."
  35. pass
  36. #: Renamed to ProtocolError but aliased for backwards compatibility.
  37. ConnectionError = ProtocolError
  38. ## Leaf Exceptions
  39. class MaxRetryError(RequestError):
  40. """Raised when the maximum number of retries is exceeded.
  41. :param pool: The connection pool
  42. :type pool: :class:`~urllib3.connectionpool.HTTPConnectionPool`
  43. :param string url: The requested Url
  44. :param exceptions.Exception reason: The underlying error
  45. """
  46. def __init__(self, pool, url, reason=None):
  47. self.reason = reason
  48. message = "Max retries exceeded with url: %s (Caused by %r)" % (
  49. url, reason)
  50. RequestError.__init__(self, pool, url, message)
  51. class HostChangedError(RequestError):
  52. "Raised when an existing pool gets a request for a foreign host."
  53. def __init__(self, pool, url, retries=3):
  54. message = "Tried to open a foreign host with url: %s" % url
  55. RequestError.__init__(self, pool, url, message)
  56. self.retries = retries
  57. class TimeoutStateError(HTTPError):
  58. """ Raised when passing an invalid state to a timeout """
  59. pass
  60. class TimeoutError(HTTPError):
  61. """ Raised when a socket timeout error occurs.
  62. Catching this error will catch both :exc:`ReadTimeoutErrors
  63. <ReadTimeoutError>` and :exc:`ConnectTimeoutErrors <ConnectTimeoutError>`.
  64. """
  65. pass
  66. class ReadTimeoutError(TimeoutError, RequestError):
  67. "Raised when a socket timeout occurs while receiving data from a server"
  68. pass
  69. # This timeout error does not have a URL attached and needs to inherit from the
  70. # base HTTPError
  71. class ConnectTimeoutError(TimeoutError):
  72. "Raised when a socket timeout occurs while connecting to a server"
  73. pass
  74. class EmptyPoolError(PoolError):
  75. "Raised when a pool runs out of connections and no more are allowed."
  76. pass
  77. class ClosedPoolError(PoolError):
  78. "Raised when a request enters a pool after the pool has been closed."
  79. pass
  80. class LocationValueError(ValueError, HTTPError):
  81. "Raised when there is something wrong with a given URL input."
  82. pass
  83. class LocationParseError(LocationValueError):
  84. "Raised when get_host or similar fails to parse the URL input."
  85. def __init__(self, location):
  86. message = "Failed to parse: %s" % location
  87. HTTPError.__init__(self, message)
  88. self.location = location
  89. class ResponseError(HTTPError):
  90. "Used as a container for an error reason supplied in a MaxRetryError."
  91. GENERIC_ERROR = 'too many error responses'
  92. SPECIFIC_ERROR = 'too many {status_code} error responses'
  93. class SecurityWarning(HTTPWarning):
  94. "Warned when perfoming security reducing actions"
  95. pass
  96. class InsecureRequestWarning(SecurityWarning):
  97. "Warned when making an unverified HTTPS request."
  98. pass
  99. class SystemTimeWarning(SecurityWarning):
  100. "Warned when system time is suspected to be wrong"
  101. pass
  102. class InsecurePlatformWarning(SecurityWarning):
  103. "Warned when certain SSL configuration is not available on a platform."
  104. pass
  105. class ResponseNotChunked(ProtocolError, ValueError):
  106. "Response needs to be chunked in order to read it as chunks."
  107. pass