1"""
2requests.exceptions
3~~~~~~~~~~~~~~~~~~~
4
5This module contains the set of Requests' exceptions.
6"""
7from urllib3.exceptions import HTTPError as BaseHTTPError
8
9from .compat import JSONDecodeError as CompatJSONDecodeError
10
11
12class RequestException(IOError):
13 """There was an ambiguous exception that occurred while handling your
14 request.
15 """
16
17 def __init__(self, *args, **kwargs):
18 """Initialize RequestException with `request` and `response` objects."""
19 response = kwargs.pop("response", None)
20 self.response = response
21 self.request = kwargs.pop("request", None)
22 if response is not None and not self.request and hasattr(response, "request"):
23 self.request = self.response.request
24 super().__init__(*args, **kwargs)
25
26
27class InvalidJSONError(RequestException):
28 """A JSON error occurred."""
29
30
31class JSONDecodeError(InvalidJSONError, CompatJSONDecodeError):
32 """Couldn't decode the text into json"""
33
34 def __init__(self, *args, **kwargs):
35 """
36 Construct the JSONDecodeError instance first with all
37 args. Then use it's args to construct the IOError so that
38 the json specific args aren't used as IOError specific args
39 and the error message from JSONDecodeError is preserved.
40 """
41 CompatJSONDecodeError.__init__(self, *args)
42 InvalidJSONError.__init__(self, *self.args, **kwargs)
43
44 def __reduce__(self):
45 """
46 The __reduce__ method called when pickling the object must
47 be the one from the JSONDecodeError (be it json/simplejson)
48 as it expects all the arguments for instantiation, not just
49 one like the IOError, and the MRO would by default call the
50 __reduce__ method from the IOError due to the inheritance order.
51 """
52 return CompatJSONDecodeError.__reduce__(self)
53
54
55class HTTPError(RequestException):
56 """An HTTP error occurred."""
57
58
59class ConnectionError(RequestException):
60 """A Connection error occurred."""
61
62
63class ProxyError(ConnectionError):
64 """A proxy error occurred."""
65
66
67class SSLError(ConnectionError):
68 """An SSL error occurred."""
69
70
71class Timeout(RequestException):
72 """The request timed out.
73
74 Catching this error will catch both
75 :exc:`~requests.exceptions.ConnectTimeout` and
76 :exc:`~requests.exceptions.ReadTimeout` errors.
77 """
78
79
80class ConnectTimeout(ConnectionError, Timeout):
81 """The request timed out while trying to connect to the remote server.
82
83 Requests that produced this error are safe to retry.
84 """
85
86
87class ReadTimeout(Timeout):
88 """The server did not send any data in the allotted amount of time."""
89
90
91class URLRequired(RequestException):
92 """A valid URL is required to make a request."""
93
94
95class TooManyRedirects(RequestException):
96 """Too many redirects."""
97
98
99class MissingSchema(RequestException, ValueError):
100 """The URL scheme (e.g. http or https) is missing."""
101
102
103class InvalidSchema(RequestException, ValueError):
104 """The URL scheme provided is either invalid or unsupported."""
105
106
107class InvalidURL(RequestException, ValueError):
108 """The URL provided was somehow invalid."""
109
110
111class InvalidHeader(RequestException, ValueError):
112 """The header value provided was somehow invalid."""
113
114
115class InvalidProxyURL(InvalidURL):
116 """The proxy URL provided is invalid."""
117
118
119class ChunkedEncodingError(RequestException):
120 """The server declared chunked encoding but sent an invalid chunk."""
121
122
123class ContentDecodingError(RequestException, BaseHTTPError):
124 """Failed to decode response content."""
125
126
127class StreamConsumedError(RequestException, TypeError):
128 """The content for this response was already consumed."""
129
130
131class RetryError(RequestException):
132 """Custom retries logic failed"""
133
134
135class UnrewindableBodyError(RequestException):
136 """Requests encountered an error when trying to rewind a body."""
137
138
139# Warnings
140
141
142class RequestsWarning(Warning):
143 """Base warning for Requests."""
144
145
146class FileModeWarning(RequestsWarning, DeprecationWarning):
147 """A file was opened in text mode, but Requests determined its binary length."""
148
149
150class RequestsDependencyWarning(RequestsWarning):
151 """An imported dependency doesn't match the expected version range."""