← requests / src/requests/exceptions.py
| 1 | """ |
| 2 | requests.exceptions |
| 3 | ~~~~~~~~~~~~~~~~~~~ |
| 4 | |
| 5 | This module contains the set of Requests' exceptions. |
| 6 | """ |
| 7 | |
| 8 | from __future__ import annotations |
| 9 | |
| 10 | from typing import TYPE_CHECKING, Any |
| 11 | |
| 12 | from urllib3.exceptions import HTTPError as BaseHTTPError |
| 13 | |
| 14 | from .compat import JSONDecodeError as CompatJSONDecodeError |
| 15 | |
| 16 | if TYPE_CHECKING: |
| 17 | from .models import PreparedRequest, Request, Response |
| 18 | |
| 19 | |
| 20 | class RequestException(IOError): |
| 21 | """There was an ambiguous exception that occurred while handling your |
| 22 | request. |
| 23 | """ |
| 24 | |
| 25 | response: Response | None |
| 26 | request: Request | PreparedRequest | None |
| 27 | |
| 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) |
| 36 | |
| 37 | |
| 38 | class InvalidJSONError(RequestException): |
| 39 | """A JSON error occurred.""" |
| 40 | |
| 41 | |
| 42 | class JSONDecodeError(InvalidJSONError, CompatJSONDecodeError): |
| 43 | """Couldn't decode the text into json""" |
| 44 | |
| 45 | def __init__(self, *args: Any, **kwargs: Any) -> None: |
| 46 | """ |
| 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. |
| 51 | """ |
| 52 | CompatJSONDecodeError.__init__(self, *args) |
| 53 | InvalidJSONError.__init__(self, *self.args, **kwargs) |
| 54 | |
| 55 | def __reduce__(self) -> tuple[Any, ...] | str: |
| 56 | """ |
| 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. |
| 62 | """ |
| 63 | return CompatJSONDecodeError.__reduce__(self) |
| 64 | |
| 65 | |
| 66 | class HTTPError(RequestException): |
| 67 | """An HTTP error occurred.""" |
| 68 | |
| 69 | |
| 70 | class ConnectionError(RequestException): |
| 71 | """A Connection error occurred.""" |
| 72 | |
| 73 | |
| 74 | class ProxyError(ConnectionError): |
| 75 | """A proxy error occurred.""" |
| 76 | |
| 77 | |
| 78 | class SSLError(ConnectionError): |
| 79 | """An SSL error occurred.""" |
| 80 | |
| 81 | |
| 82 | class Timeout(RequestException): |
| 83 | """The request timed out. |
| 84 | |
| 85 | Catching this error will catch both |
| 86 | :exc:`~requests.exceptions.ConnectTimeout` and |
| 87 | :exc:`~requests.exceptions.ReadTimeout` errors. |
| 88 | """ |
| 89 | |
| 90 | |
| 91 | class ConnectTimeout(ConnectionError, Timeout): |
| 92 | """The request timed out while trying to connect to the remote server. |
| 93 | |
| 94 | Requests that produced this error are safe to retry. |
| 95 | """ |
| 96 | |
| 97 | |
| 98 | class ReadTimeout(Timeout): |
| 99 | """The server did not send any data in the allotted amount of time.""" |
| 100 | |
| 101 | |
| 102 | class URLRequired(RequestException): |
| 103 | """A valid URL is required to make a request.""" |
| 104 | |
| 105 | |
| 106 | class TooManyRedirects(RequestException): |
| 107 | """Too many redirects.""" |
| 108 | |
| 109 | |
| 110 | class MissingSchema(RequestException, ValueError): |
| 111 | """The URL scheme (e.g. http or https) is missing.""" |
| 112 | |
| 113 | |
| 114 | class InvalidSchema(RequestException, ValueError): |
| 115 | """The URL scheme provided is either invalid or unsupported.""" |
| 116 | |
| 117 | |
| 118 | class InvalidURL(RequestException, ValueError): |
| 119 | """The URL provided was somehow invalid.""" |
| 120 | |
| 121 | |
| 122 | class InvalidHeader(RequestException, ValueError): |
| 123 | """The header value provided was somehow invalid.""" |
| 124 | |
| 125 | |
| 126 | class InvalidProxyURL(InvalidURL): |
| 127 | """The proxy URL provided is invalid.""" |
| 128 | |
| 129 | |
| 130 | class ChunkedEncodingError(RequestException): |
| 131 | """The server declared chunked encoding but sent an invalid chunk.""" |
| 132 | |
| 133 | |
| 134 | class ContentDecodingError(RequestException, BaseHTTPError): |
| 135 | """Failed to decode response content.""" |
| 136 | |
| 137 | |
| 138 | class StreamConsumedError(RequestException, TypeError): |
| 139 | """The content for this response was already consumed.""" |
| 140 | |
| 141 | |
| 142 | class RetryError(RequestException): |
| 143 | """Custom retries logic failed""" |
| 144 | |
| 145 | |
| 146 | class UnrewindableBodyError(RequestException): |
| 147 | """Requests encountered an error when trying to rewind a body.""" |
| 148 | |
| 149 | |
| 150 | # Warnings |
| 151 | |
| 152 | |
| 153 | class RequestsWarning(Warning): |
| 154 | """Base warning for Requests.""" |
| 155 | |
| 156 | |
| 157 | class FileModeWarning(RequestsWarning, DeprecationWarning): |
| 158 | """A file was opened in text mode, but Requests determined its binary length.""" |
| 159 | |
| 160 | |
| 161 | class RequestsDependencyWarning(RequestsWarning): |
| 162 | """An imported dependency doesn't match the expected version range.""" |
| 163 |