1 # urllib3/exceptions.py
2 # Copyright 2008-2013 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
4 # This module is part of urllib3 and is released under
5 # the MIT License: http://www.opensource.org/licenses/mit-license.php
10 class HTTPError(Exception):
11 "Base exception 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."
54 class MaxRetryError(RequestError):
55 "Raised when the maximum number of retries is exceeded."
57 def __init__(self, pool, url, reason=None):
60 message = "Max retries exceeded with url: %s" % url
62 message += " (Caused by %s: %s)" % (type(reason), reason)
64 message += " (Caused by redirect)"
66 RequestError.__init__(self, pool, url, message)
69 class HostChangedError(RequestError):
70 "Raised when an existing pool gets a request for a foreign host."
72 def __init__(self, pool, url, retries=3):
73 message = "Tried to open a foreign host with url: %s" % url
74 RequestError.__init__(self, pool, url, message)
75 self.retries = retries
78 class TimeoutStateError(HTTPError):
79 """ Raised when passing an invalid state to a timeout """
83 class TimeoutError(HTTPError):
84 """ Raised when a socket timeout error occurs.
86 Catching this error will catch both :exc:`ReadTimeoutErrors
87 <ReadTimeoutError>` and :exc:`ConnectTimeoutErrors <ConnectTimeoutError>`.
92 class ReadTimeoutError(TimeoutError, RequestError):
93 "Raised when a socket timeout occurs while receiving data from a server"
97 # This timeout error does not have a URL attached and needs to inherit from the
99 class ConnectTimeoutError(TimeoutError):
100 "Raised when a socket timeout occurs while connecting to a server"
104 class EmptyPoolError(PoolError):
105 "Raised when a pool runs out of connections and no more are allowed."
109 class ClosedPoolError(PoolError):
110 "Raised when a request enters a pool after the pool has been closed."
114 class LocationParseError(ValueError, HTTPError):
115 "Raised when get_host or similar fails to parse the URL input."
117 def __init__(self, location):
118 message = "Failed to parse: %s" % location
119 HTTPError.__init__(self, message)
121 self.location = location