annotate urllib3/exceptions.py @ 15:0a3943480712

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