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