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