exceptions.py 6.4 KB

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