5
This module contains the set of Requests' exceptions.
8
from __future__ import annotations
10
from typing import TYPE_CHECKING, Any
12
from urllib3.exceptions import HTTPError as BaseHTTPError
14
from .compat import JSONDecodeError as CompatJSONDecodeError
17
from .models import PreparedRequest, Request, Response
20
class RequestException(IOError):
21
"""There was an ambiguous exception that occurred while handling your
25
response: Response | None
26
request: Request | PreparedRequest | None
28
def __init__(self, *args: Any, **kwargs: Any) -> None:
29
"""Initialize RequestException with `request` and `response` objects."""
30
response: Response | None = kwargs.pop("response", None)
31
self.response = response
32
self.request = kwargs.pop("request", None)
33
if response is not None and not self.request and hasattr(response, "request"):
34
self.request = response.request
35
super().__init__(*args, **kwargs)
38
class InvalidJSONError(RequestException):
39
"""A JSON error occurred."""
42
class JSONDecodeError(InvalidJSONError, CompatJSONDecodeError):
43
"""Couldn't decode the text into json"""
45
def __init__(self, *args: Any, **kwargs: Any) -> None:
47
Construct the JSONDecodeError instance first with all
48
args. Then use it's args to construct the IOError so that
49
the json specific args aren't used as IOError specific args
50
and the error message from JSONDecodeError is preserved.
52
CompatJSONDecodeError.__init__(self, *args)
53
InvalidJSONError.__init__(self, *self.args, **kwargs)
49
the json specific args aren't used as IOError specific args
50
and the error message from JSONDecodeError is preserved.
52
CompatJSONDecodeError.__init__(self, *args)
53
InvalidJSONError.__init__(self, *self.args, **kwargs)
55
def __reduce__(self) -> tuple[Any, ...] | str:
57
The __reduce__ method called when pickling the object must
58
be the one from the JSONDecodeError (be it json/simplejson)
59
as it expects all the arguments for instantiation, not just
60
one like the IOError, and the MRO would by default call the
61
__reduce__ method from the IOError due to the inheritance order.
63
return CompatJSONDecodeError.__reduce__(self)
66
class HTTPError(RequestException):
67
"""An HTTP error occurred."""
70
class ConnectionError(RequestException):
71
"""A Connection error occurred."""
74
class ProxyError(ConnectionError):
75
"""A proxy error occurred."""
78
class SSLError(ConnectionError):
79
"""An SSL error occurred."""
82
class Timeout(RequestException):
83
"""The request timed out.
85
Catching this error will catch both
86
:exc:`~requests.exceptions.ConnectTimeout` and
87
:exc:`~requests.exceptions.ReadTimeout` errors.
91
class ConnectTimeout(ConnectionError, Timeout):
92
"""The request timed out while trying to connect to the remote server.
94
Requests that produced this error are safe to retry.
91
class ConnectTimeout(ConnectionError, Timeout):
92
"""The request timed out while trying to connect to the remote server.
94
Requests that produced this error are safe to retry.
98
class ReadTimeout(Timeout):
99
"""The server did not send any data in the allotted amount of time."""
102
class URLRequired(RequestException):
103
"""A valid URL is required to make a request."""
106
class TooManyRedirects(RequestException):
107
"""Too many redirects."""
110
class MissingSchema(RequestException, ValueError):
111
"""The URL scheme (e.g. http or https) is missing."""
114
class InvalidSchema(RequestException, ValueError):
115
"""The URL scheme provided is either invalid or unsupported."""
118
class InvalidURL(RequestException, ValueError):
119
"""The URL provided was somehow invalid."""
122
class InvalidHeader(RequestException, ValueError):
123
"""The header value provided was somehow invalid."""
126
class InvalidProxyURL(InvalidURL):
127
"""The proxy URL provided is invalid."""
130
class ChunkedEncodingError(RequestException):
131
"""The server declared chunked encoding but sent an invalid chunk."""
134
class ContentDecodingError(RequestException, BaseHTTPError):
135
"""Failed to decode response content."""
138
class StreamConsumedError(RequestException, TypeError):
139
"""The content for this response was already consumed."""
142
class RetryError(RequestException):
143
"""Custom retries logic failed"""
139
"""The content for this response was already consumed."""
142
class RetryError(RequestException):
143
"""Custom retries logic failed"""
146
class UnrewindableBodyError(RequestException):
147
"""Requests encountered an error when trying to rewind a body."""
153
class RequestsWarning(Warning):
154
"""Base warning for Requests."""
157
class FileModeWarning(RequestsWarning, DeprecationWarning):
158
"""A file was opened in text mode, but Requests determined its binary length."""
161
class RequestsDependencyWarning(RequestsWarning):
162
"""An imported dependency doesn't match the expected version range."""