exceptions.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. from __future__ import annotations
  2. import socket
  3. import typing
  4. import warnings
  5. from email.errors import MessageDefect
  6. from http.client import IncompleteRead as httplib_IncompleteRead
  7. if typing.TYPE_CHECKING:
  8. from .connection import HTTPConnection
  9. from .connectionpool import ConnectionPool
  10. from .response import HTTPResponse
  11. from .util.retry import Retry
  12. # Base Exceptions
  13. class HTTPError(Exception):
  14. """Base exception used by this module."""
  15. class HTTPWarning(Warning):
  16. """Base warning used by this module."""
  17. _TYPE_REDUCE_RESULT = tuple[typing.Callable[..., object], tuple[object, ...]]
  18. class PoolError(HTTPError):
  19. """Base exception for errors caused within a pool."""
  20. def __init__(self, pool: ConnectionPool, message: str) -> None:
  21. self.pool = pool
  22. super().__init__(f"{pool}: {message}")
  23. def __reduce__(self) -> _TYPE_REDUCE_RESULT:
  24. # For pickling purposes.
  25. return self.__class__, (None, None)
  26. class RequestError(PoolError):
  27. """Base exception for PoolErrors that have associated URLs."""
  28. def __init__(self, pool: ConnectionPool, url: str, message: str) -> None:
  29. self.url = url
  30. super().__init__(pool, message)
  31. def __reduce__(self) -> _TYPE_REDUCE_RESULT:
  32. # For pickling purposes.
  33. return self.__class__, (None, self.url, None)
  34. class SSLError(HTTPError):
  35. """Raised when SSL certificate fails in an HTTPS connection."""
  36. class ProxyError(HTTPError):
  37. """Raised when the connection to a proxy fails."""
  38. # The original error is also available as __cause__.
  39. original_error: Exception
  40. def __init__(self, message: str, error: Exception) -> None:
  41. super().__init__(message, error)
  42. self.original_error = error
  43. class DecodeError(HTTPError):
  44. """Raised when automatic decoding based on Content-Type fails."""
  45. class ProtocolError(HTTPError):
  46. """Raised when something unexpected happens mid-request/response."""
  47. #: Renamed to ProtocolError but aliased for backwards compatibility.
  48. ConnectionError = ProtocolError
  49. # Leaf Exceptions
  50. class MaxRetryError(RequestError):
  51. """Raised when the maximum number of retries is exceeded.
  52. :param pool: The connection pool
  53. :type pool: :class:`~urllib3.connectionpool.HTTPConnectionPool`
  54. :param str url: The requested Url
  55. :param reason: The underlying error
  56. :type reason: :class:`Exception`
  57. """
  58. def __init__(
  59. self, pool: ConnectionPool, url: str, reason: Exception | None = None
  60. ) -> None:
  61. self.reason = reason
  62. message = f"Max retries exceeded with url: {url} (Caused by {reason!r})"
  63. super().__init__(pool, url, message)
  64. class HostChangedError(RequestError):
  65. """Raised when an existing pool gets a request for a foreign host."""
  66. def __init__(
  67. self, pool: ConnectionPool, url: str, retries: Retry | int = 3
  68. ) -> None:
  69. message = f"Tried to open a foreign host with url: {url}"
  70. super().__init__(pool, url, message)
  71. self.retries = retries
  72. class TimeoutStateError(HTTPError):
  73. """Raised when passing an invalid state to a timeout"""
  74. class TimeoutError(HTTPError):
  75. """Raised when a socket timeout error occurs.
  76. Catching this error will catch both :exc:`ReadTimeoutErrors
  77. <ReadTimeoutError>` and :exc:`ConnectTimeoutErrors <ConnectTimeoutError>`.
  78. """
  79. class ReadTimeoutError(TimeoutError, RequestError):
  80. """Raised when a socket timeout occurs while receiving data from a server"""
  81. # This timeout error does not have a URL attached and needs to inherit from the
  82. # base HTTPError
  83. class ConnectTimeoutError(TimeoutError):
  84. """Raised when a socket timeout occurs while connecting to a server"""
  85. class NewConnectionError(ConnectTimeoutError, HTTPError):
  86. """Raised when we fail to establish a new connection. Usually ECONNREFUSED."""
  87. def __init__(self, conn: HTTPConnection, message: str) -> None:
  88. self.conn = conn
  89. super().__init__(f"{conn}: {message}")
  90. def __reduce__(self) -> _TYPE_REDUCE_RESULT:
  91. # For pickling purposes.
  92. return self.__class__, (None, None)
  93. @property
  94. def pool(self) -> HTTPConnection:
  95. warnings.warn(
  96. "The 'pool' property is deprecated and will be removed "
  97. "in urllib3 v2.1.0. Use 'conn' instead.",
  98. DeprecationWarning,
  99. stacklevel=2,
  100. )
  101. return self.conn
  102. class NameResolutionError(NewConnectionError):
  103. """Raised when host name resolution fails."""
  104. def __init__(self, host: str, conn: HTTPConnection, reason: socket.gaierror):
  105. message = f"Failed to resolve '{host}' ({reason})"
  106. super().__init__(conn, message)
  107. def __reduce__(self) -> _TYPE_REDUCE_RESULT:
  108. # For pickling purposes.
  109. return self.__class__, (None, None, None)
  110. class EmptyPoolError(PoolError):
  111. """Raised when a pool runs out of connections and no more are allowed."""
  112. class FullPoolError(PoolError):
  113. """Raised when we try to add a connection to a full pool in blocking mode."""
  114. class ClosedPoolError(PoolError):
  115. """Raised when a request enters a pool after the pool has been closed."""
  116. class LocationValueError(ValueError, HTTPError):
  117. """Raised when there is something wrong with a given URL input."""
  118. class LocationParseError(LocationValueError):
  119. """Raised when get_host or similar fails to parse the URL input."""
  120. def __init__(self, location: str) -> None:
  121. message = f"Failed to parse: {location}"
  122. super().__init__(message)
  123. self.location = location
  124. class URLSchemeUnknown(LocationValueError):
  125. """Raised when a URL input has an unsupported scheme."""
  126. def __init__(self, scheme: str):
  127. message = f"Not supported URL scheme {scheme}"
  128. super().__init__(message)
  129. self.scheme = scheme
  130. class ResponseError(HTTPError):
  131. """Used as a container for an error reason supplied in a MaxRetryError."""
  132. GENERIC_ERROR = "too many error responses"
  133. SPECIFIC_ERROR = "too many {status_code} error responses"
  134. class SecurityWarning(HTTPWarning):
  135. """Warned when performing security reducing actions"""
  136. class InsecureRequestWarning(SecurityWarning):
  137. """Warned when making an unverified HTTPS request."""
  138. class NotOpenSSLWarning(SecurityWarning):
  139. """Warned when using unsupported SSL library"""
  140. class SystemTimeWarning(SecurityWarning):
  141. """Warned when system time is suspected to be wrong"""
  142. class InsecurePlatformWarning(SecurityWarning):
  143. """Warned when certain TLS/SSL configuration is not available on a platform."""
  144. class DependencyWarning(HTTPWarning):
  145. """
  146. Warned when an attempt is made to import a module with missing optional
  147. dependencies.
  148. """
  149. class ResponseNotChunked(ProtocolError, ValueError):
  150. """Response needs to be chunked in order to read it as chunks."""
  151. class BodyNotHttplibCompatible(HTTPError):
  152. """
  153. Body should be :class:`http.client.HTTPResponse` like
  154. (have an fp attribute which returns raw chunks) for read_chunked().
  155. """
  156. class IncompleteRead(HTTPError, httplib_IncompleteRead):
  157. """
  158. Response length doesn't match expected Content-Length
  159. Subclass of :class:`http.client.IncompleteRead` to allow int value
  160. for ``partial`` to avoid creating large objects on streamed reads.
  161. """
  162. partial: int # type: ignore[assignment]
  163. expected: int
  164. def __init__(self, partial: int, expected: int) -> None:
  165. self.partial = partial
  166. self.expected = expected
  167. def __repr__(self) -> str:
  168. return "IncompleteRead(%i bytes read, %i more expected)" % (
  169. self.partial,
  170. self.expected,
  171. )
  172. class InvalidChunkLength(HTTPError, httplib_IncompleteRead):
  173. """Invalid chunk length in a chunked response."""
  174. def __init__(self, response: HTTPResponse, length: bytes) -> None:
  175. self.partial: int = response.tell() # type: ignore[assignment]
  176. self.expected: int | None = response.length_remaining
  177. self.response = response
  178. self.length = length
  179. def __repr__(self) -> str:
  180. return "InvalidChunkLength(got length %r, %i bytes read)" % (
  181. self.length,
  182. self.partial,
  183. )
  184. class InvalidHeader(HTTPError):
  185. """The header provided was somehow invalid."""
  186. class ProxySchemeUnknown(AssertionError, URLSchemeUnknown):
  187. """ProxyManager does not support the supplied scheme"""
  188. # TODO(t-8ch): Stop inheriting from AssertionError in v2.0.
  189. def __init__(self, scheme: str | None) -> None:
  190. # 'localhost' is here because our URL parser parses
  191. # localhost:8080 -> scheme=localhost, remove if we fix this.
  192. if scheme == "localhost":
  193. scheme = None
  194. if scheme is None:
  195. message = "Proxy URL had no scheme, should start with http:// or https://"
  196. else:
  197. message = f"Proxy URL had unsupported scheme {scheme}, should use http:// or https://"
  198. super().__init__(message)
  199. class ProxySchemeUnsupported(ValueError):
  200. """Fetching HTTPS resources through HTTPS proxies is unsupported"""
  201. class HeaderParsingError(HTTPError):
  202. """Raised by assert_header_parsing, but we convert it to a log.warning statement."""
  203. def __init__(
  204. self, defects: list[MessageDefect], unparsed_data: bytes | str | None
  205. ) -> None:
  206. message = f"{defects or 'Unknown'}, unparsed data: {unparsed_data!r}"
  207. super().__init__(message)
  208. class UnrewindableBodyError(HTTPError):
  209. """urllib3 encountered an error when trying to rewind a body"""