jpayne@68: """ jpayne@68: requests.exceptions jpayne@68: ~~~~~~~~~~~~~~~~~~~ jpayne@68: jpayne@68: This module contains the set of Requests' exceptions. jpayne@68: """ jpayne@68: from urllib3.exceptions import HTTPError as BaseHTTPError jpayne@68: jpayne@68: from .compat import JSONDecodeError as CompatJSONDecodeError jpayne@68: jpayne@68: jpayne@68: class RequestException(IOError): jpayne@68: """There was an ambiguous exception that occurred while handling your jpayne@68: request. jpayne@68: """ jpayne@68: jpayne@68: def __init__(self, *args, **kwargs): jpayne@68: """Initialize RequestException with `request` and `response` objects.""" jpayne@68: response = kwargs.pop("response", None) jpayne@68: self.response = response jpayne@68: self.request = kwargs.pop("request", None) jpayne@68: if response is not None and not self.request and hasattr(response, "request"): jpayne@68: self.request = self.response.request jpayne@68: super().__init__(*args, **kwargs) jpayne@68: jpayne@68: jpayne@68: class InvalidJSONError(RequestException): jpayne@68: """A JSON error occurred.""" jpayne@68: jpayne@68: jpayne@68: class JSONDecodeError(InvalidJSONError, CompatJSONDecodeError): jpayne@68: """Couldn't decode the text into json""" jpayne@68: jpayne@68: def __init__(self, *args, **kwargs): jpayne@68: """ jpayne@68: Construct the JSONDecodeError instance first with all jpayne@68: args. Then use it's args to construct the IOError so that jpayne@68: the json specific args aren't used as IOError specific args jpayne@68: and the error message from JSONDecodeError is preserved. jpayne@68: """ jpayne@68: CompatJSONDecodeError.__init__(self, *args) jpayne@68: InvalidJSONError.__init__(self, *self.args, **kwargs) jpayne@68: jpayne@68: def __reduce__(self): jpayne@68: """ jpayne@68: The __reduce__ method called when pickling the object must jpayne@68: be the one from the JSONDecodeError (be it json/simplejson) jpayne@68: as it expects all the arguments for instantiation, not just jpayne@68: one like the IOError, and the MRO would by default call the jpayne@68: __reduce__ method from the IOError due to the inheritance order. jpayne@68: """ jpayne@68: return CompatJSONDecodeError.__reduce__(self) jpayne@68: jpayne@68: jpayne@68: class HTTPError(RequestException): jpayne@68: """An HTTP error occurred.""" jpayne@68: jpayne@68: jpayne@68: class ConnectionError(RequestException): jpayne@68: """A Connection error occurred.""" jpayne@68: jpayne@68: jpayne@68: class ProxyError(ConnectionError): jpayne@68: """A proxy error occurred.""" jpayne@68: jpayne@68: jpayne@68: class SSLError(ConnectionError): jpayne@68: """An SSL error occurred.""" jpayne@68: jpayne@68: jpayne@68: class Timeout(RequestException): jpayne@68: """The request timed out. jpayne@68: jpayne@68: Catching this error will catch both jpayne@68: :exc:`~requests.exceptions.ConnectTimeout` and jpayne@68: :exc:`~requests.exceptions.ReadTimeout` errors. jpayne@68: """ jpayne@68: jpayne@68: jpayne@68: class ConnectTimeout(ConnectionError, Timeout): jpayne@68: """The request timed out while trying to connect to the remote server. jpayne@68: jpayne@68: Requests that produced this error are safe to retry. jpayne@68: """ jpayne@68: jpayne@68: jpayne@68: class ReadTimeout(Timeout): jpayne@68: """The server did not send any data in the allotted amount of time.""" jpayne@68: jpayne@68: jpayne@68: class URLRequired(RequestException): jpayne@68: """A valid URL is required to make a request.""" jpayne@68: jpayne@68: jpayne@68: class TooManyRedirects(RequestException): jpayne@68: """Too many redirects.""" jpayne@68: jpayne@68: jpayne@68: class MissingSchema(RequestException, ValueError): jpayne@68: """The URL scheme (e.g. http or https) is missing.""" jpayne@68: jpayne@68: jpayne@68: class InvalidSchema(RequestException, ValueError): jpayne@68: """The URL scheme provided is either invalid or unsupported.""" jpayne@68: jpayne@68: jpayne@68: class InvalidURL(RequestException, ValueError): jpayne@68: """The URL provided was somehow invalid.""" jpayne@68: jpayne@68: jpayne@68: class InvalidHeader(RequestException, ValueError): jpayne@68: """The header value provided was somehow invalid.""" jpayne@68: jpayne@68: jpayne@68: class InvalidProxyURL(InvalidURL): jpayne@68: """The proxy URL provided is invalid.""" jpayne@68: jpayne@68: jpayne@68: class ChunkedEncodingError(RequestException): jpayne@68: """The server declared chunked encoding but sent an invalid chunk.""" jpayne@68: jpayne@68: jpayne@68: class ContentDecodingError(RequestException, BaseHTTPError): jpayne@68: """Failed to decode response content.""" jpayne@68: jpayne@68: jpayne@68: class StreamConsumedError(RequestException, TypeError): jpayne@68: """The content for this response was already consumed.""" jpayne@68: jpayne@68: jpayne@68: class RetryError(RequestException): jpayne@68: """Custom retries logic failed""" jpayne@68: jpayne@68: jpayne@68: class UnrewindableBodyError(RequestException): jpayne@68: """Requests encountered an error when trying to rewind a body.""" jpayne@68: jpayne@68: jpayne@68: # Warnings jpayne@68: jpayne@68: jpayne@68: class RequestsWarning(Warning): jpayne@68: """Base warning for Requests.""" jpayne@68: jpayne@68: jpayne@68: class FileModeWarning(RequestsWarning, DeprecationWarning): jpayne@68: """A file was opened in text mode, but Requests determined its binary length.""" jpayne@68: jpayne@68: jpayne@68: class RequestsDependencyWarning(RequestsWarning): jpayne@68: """An imported dependency doesn't match the expected version range."""