exceptions.py 6.5 KB

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