1 from __future__ import absolute_import
5 class HTTPError(Exception):
6 "Base exception used by this module."
10 class HTTPWarning(Warning):
11 "Base warning used by this module."
15 class PoolError(HTTPError):
16 "Base exception for errors caused within a pool."
17 def __init__(self, pool, message):
19 HTTPError.__init__(self, "%s: %s" % (pool, message))
22 # For pickling purposes.
23 return self.__class__, (None, None)
26 class RequestError(PoolError):
27 "Base exception for PoolErrors that have associated URLs."
28 def __init__(self, pool, url, message):
30 PoolError.__init__(self, pool, message)
33 # For pickling purposes.
34 return self.__class__, (None, self.url, None)
37 class SSLError(HTTPError):
38 "Raised when SSL certificate fails in an HTTPS connection."
42 class ProxyError(HTTPError):
43 "Raised when the connection to a proxy fails."
47 class DecodeError(HTTPError):
48 "Raised when automatic decoding based on Content-Type fails."
52 class ProtocolError(HTTPError):
53 "Raised when something unexpected happens mid-request/response."
57 #: Renamed to ProtocolError but aliased for backwards compatibility.
58 ConnectionError = ProtocolError
63 class MaxRetryError(RequestError):
64 """Raised when the maximum number of retries is exceeded.
66 :param pool: The connection pool
67 :type pool: :class:`~urllib3.connectionpool.HTTPConnectionPool`
68 :param string url: The requested Url
69 :param exceptions.Exception reason: The underlying error
73 def __init__(self, pool, url, reason=None):
76 message = "Max retries exceeded with url: %s (Caused by %r)" % (
79 RequestError.__init__(self, pool, url, message)
82 class HostChangedError(RequestError):
83 "Raised when an existing pool gets a request for a foreign host."
85 def __init__(self, pool, url, retries=3):
86 message = "Tried to open a foreign host with url: %s" % url
87 RequestError.__init__(self, pool, url, message)
88 self.retries = retries
91 class TimeoutStateError(HTTPError):
92 """ Raised when passing an invalid state to a timeout """
96 class TimeoutError(HTTPError):
97 """ Raised when a socket timeout error occurs.
99 Catching this error will catch both :exc:`ReadTimeoutErrors
100 <ReadTimeoutError>` and :exc:`ConnectTimeoutErrors <ConnectTimeoutError>`.
105 class ReadTimeoutError(TimeoutError, RequestError):
106 "Raised when a socket timeout occurs while receiving data from a server"
110 # This timeout error does not have a URL attached and needs to inherit from the
112 class ConnectTimeoutError(TimeoutError):
113 "Raised when a socket timeout occurs while connecting to a server"
117 class NewConnectionError(ConnectTimeoutError, PoolError):
118 "Raised when we fail to establish a new connection. Usually ECONNREFUSED."
122 class EmptyPoolError(PoolError):
123 "Raised when a pool runs out of connections and no more are allowed."
127 class ClosedPoolError(PoolError):
128 "Raised when a request enters a pool after the pool has been closed."
132 class LocationValueError(ValueError, HTTPError):
133 "Raised when there is something wrong with a given URL input."
137 class LocationParseError(LocationValueError):
138 "Raised when get_host or similar fails to parse the URL input."
140 def __init__(self, location):
141 message = "Failed to parse: %s" % location
142 HTTPError.__init__(self, message)
144 self.location = location
147 class ResponseError(HTTPError):
148 "Used as a container for an error reason supplied in a MaxRetryError."
149 GENERIC_ERROR = 'too many error responses'
150 SPECIFIC_ERROR = 'too many {status_code} error responses'
153 class SecurityWarning(HTTPWarning):
154 "Warned when perfoming security reducing actions"
158 class SubjectAltNameWarning(SecurityWarning):
159 "Warned when connecting to a host with a certificate missing a SAN."
163 class InsecureRequestWarning(SecurityWarning):
164 "Warned when making an unverified HTTPS request."
168 class SystemTimeWarning(SecurityWarning):
169 "Warned when system time is suspected to be wrong"
173 class InsecurePlatformWarning(SecurityWarning):
174 "Warned when certain SSL configuration is not available on a platform."
178 class SNIMissingWarning(HTTPWarning):
179 "Warned when making a HTTPS request without SNI available."
183 class DependencyWarning(HTTPWarning):
185 Warned when an attempt is made to import a module with missing optional
191 class ResponseNotChunked(ProtocolError, ValueError):
192 "Response needs to be chunked in order to read it as chunks."
196 class ProxySchemeUnknown(AssertionError, ValueError):
197 "ProxyManager does not support the supplied scheme"
198 # TODO(t-8ch): Stop inheriting from AssertionError in v2.0.
200 def __init__(self, scheme):
201 message = "Not supported proxy scheme %s" % scheme
202 super(ProxySchemeUnknown, self).__init__(message)
205 class HeaderParsingError(HTTPError):
206 "Raised by assert_header_parsing, but we convert it to a log.warning statement."
207 def __init__(self, defects, unparsed_data):
208 message = '%s, unparsed data: %r' % (defects or 'Unknown', unparsed_data)
209 super(HeaderParsingError, self).__init__(message)