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