exceptions.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. from __future__ import absolute_import
  2. from .packages.six.moves.http_client import IncompleteRead as httplib_IncompleteRead
  3. # Base Exceptions
  4. class HTTPError(Exception):
  5. "Base exception used by this module."
  6. pass
  7. class HTTPWarning(Warning):
  8. "Base warning used by this module."
  9. pass
  10. class PoolError(HTTPError):
  11. "Base exception for errors caused within a pool."
  12. def __init__(self, pool, message):
  13. self.pool = pool
  14. HTTPError.__init__(self, "%s: %s" % (pool, message))
  15. def __reduce__(self):
  16. # For pickling purposes.
  17. return self.__class__, (None, None)
  18. class RequestError(PoolError):
  19. "Base exception for PoolErrors that have associated URLs."
  20. def __init__(self, pool, url, message):
  21. self.url = url
  22. PoolError.__init__(self, pool, message)
  23. def __reduce__(self):
  24. # For pickling purposes.
  25. return self.__class__, (None, self.url, None)
  26. class SSLError(HTTPError):
  27. "Raised when SSL certificate fails in an HTTPS connection."
  28. pass
  29. class ProxyError(HTTPError):
  30. "Raised when the connection to a proxy fails."
  31. pass
  32. class DecodeError(HTTPError):
  33. "Raised when automatic decoding based on Content-Type fails."
  34. pass
  35. class ProtocolError(HTTPError):
  36. "Raised when something unexpected happens mid-request/response."
  37. pass
  38. #: Renamed to ProtocolError but aliased for backwards compatibility.
  39. ConnectionError = ProtocolError
  40. # Leaf Exceptions
  41. class MaxRetryError(RequestError):
  42. """Raised when the maximum number of retries is exceeded.
  43. :param pool: The connection pool
  44. :type pool: :class:`~urllib3.connectionpool.HTTPConnectionPool`
  45. :param string url: The requested Url
  46. :param exceptions.Exception reason: The underlying error
  47. """
  48. def __init__(self, pool, url, reason=None):
  49. self.reason = reason
  50. message = "Max retries exceeded with url: %s (Caused by %r)" % (url, reason)
  51. RequestError.__init__(self, pool, url, message)
  52. class HostChangedError(RequestError):
  53. "Raised when an existing pool gets a request for a foreign host."
  54. def __init__(self, pool, url, retries=3):
  55. message = "Tried to open a foreign host with url: %s" % url
  56. RequestError.__init__(self, pool, url, message)
  57. self.retries = retries
  58. class TimeoutStateError(HTTPError):
  59. """ Raised when passing an invalid state to a timeout """
  60. pass
  61. class TimeoutError(HTTPError):
  62. """ Raised when a socket timeout error occurs.
  63. Catching this error will catch both :exc:`ReadTimeoutErrors
  64. <ReadTimeoutError>` and :exc:`ConnectTimeoutErrors <ConnectTimeoutError>`.
  65. """
  66. pass
  67. class ReadTimeoutError(TimeoutError, RequestError):
  68. "Raised when a socket timeout occurs while receiving data from a server"
  69. pass
  70. # This timeout error does not have a URL attached and needs to inherit from the
  71. # base HTTPError
  72. class ConnectTimeoutError(TimeoutError):
  73. "Raised when a socket timeout occurs while connecting to a server"
  74. pass
  75. class NewConnectionError(ConnectTimeoutError, PoolError):
  76. "Raised when we fail to establish a new connection. Usually ECONNREFUSED."
  77. pass
  78. class EmptyPoolError(PoolError):
  79. "Raised when a pool runs out of connections and no more are allowed."
  80. pass
  81. class ClosedPoolError(PoolError):
  82. "Raised when a request enters a pool after the pool has been closed."
  83. pass
  84. class LocationValueError(ValueError, HTTPError):
  85. "Raised when there is something wrong with a given URL input."
  86. pass
  87. class LocationParseError(LocationValueError):
  88. "Raised when get_host or similar fails to parse the URL input."
  89. def __init__(self, location):
  90. message = "Failed to parse: %s" % location
  91. HTTPError.__init__(self, message)
  92. self.location = location
  93. class ResponseError(HTTPError):
  94. "Used as a container for an error reason supplied in a MaxRetryError."
  95. GENERIC_ERROR = "too many error responses"
  96. SPECIFIC_ERROR = "too many {status_code} error responses"
  97. class SecurityWarning(HTTPWarning):
  98. "Warned when performing security reducing actions"
  99. pass
  100. class SubjectAltNameWarning(SecurityWarning):
  101. "Warned when connecting to a host with a certificate missing a SAN."
  102. pass
  103. class InsecureRequestWarning(SecurityWarning):
  104. "Warned when making an unverified HTTPS request."
  105. pass
  106. class SystemTimeWarning(SecurityWarning):
  107. "Warned when system time is suspected to be wrong"
  108. pass
  109. class InsecurePlatformWarning(SecurityWarning):
  110. "Warned when certain SSL configuration is not available on a platform."
  111. pass
  112. class SNIMissingWarning(HTTPWarning):
  113. "Warned when making a HTTPS request without SNI available."
  114. pass
  115. class DependencyWarning(HTTPWarning):
  116. """
  117. Warned when an attempt is made to import a module with missing optional
  118. dependencies.
  119. """
  120. pass
  121. class ResponseNotChunked(ProtocolError, ValueError):
  122. "Response needs to be chunked in order to read it as chunks."
  123. pass
  124. class BodyNotHttplibCompatible(HTTPError):
  125. """
  126. Body should be httplib.HTTPResponse like (have an fp attribute which
  127. returns raw chunks) for read_chunked().
  128. """
  129. pass
  130. class IncompleteRead(HTTPError, httplib_IncompleteRead):
  131. """
  132. Response length doesn't match expected Content-Length
  133. Subclass of http_client.IncompleteRead to allow int value
  134. for `partial` to avoid creating large objects on streamed
  135. reads.
  136. """
  137. def __init__(self, partial, expected):
  138. super(IncompleteRead, self).__init__(partial, expected)
  139. def __repr__(self):
  140. return "IncompleteRead(%i bytes read, %i more expected)" % (
  141. self.partial,
  142. self.expected,
  143. )
  144. class InvalidHeader(HTTPError):
  145. "The header provided was somehow invalid."
  146. pass
  147. class ProxySchemeUnknown(AssertionError, ValueError):
  148. "ProxyManager does not support the supplied scheme"
  149. # TODO(t-8ch): Stop inheriting from AssertionError in v2.0.
  150. def __init__(self, scheme):
  151. message = "Not supported proxy scheme %s" % scheme
  152. super(ProxySchemeUnknown, self).__init__(message)
  153. class HeaderParsingError(HTTPError):
  154. "Raised by assert_header_parsing, but we convert it to a log.warning statement."
  155. def __init__(self, defects, unparsed_data):
  156. message = "%s, unparsed data: %r" % (defects or "Unknown", unparsed_data)
  157. super(HeaderParsingError, self).__init__(message)
  158. class UnrewindableBodyError(HTTPError):
  159. "urllib3 encountered an error when trying to rewind a body"
  160. pass