pip/src/pip/_vendor/urllib3/exceptions.py

314 lines
7.6 KiB
Python
Raw Normal View History

2016-01-19 00:28:54 +01:00
from __future__ import absolute_import
2020-11-19 15:20:52 +01:00
2019-10-09 16:16:37 +02:00
from .packages.six.moves.http_client import IncompleteRead as httplib_IncompleteRead
2016-01-19 00:28:54 +01:00
# Base Exceptions
2013-08-15 15:33:47 +02:00
class HTTPError(Exception):
2020-11-19 15:20:52 +01:00
"""Base exception used by this module."""
2013-08-15 15:33:47 +02:00
pass
2016-01-19 00:28:54 +01:00
class HTTPWarning(Warning):
2020-11-19 15:20:52 +01:00
"""Base warning used by this module."""
pass
2013-08-15 15:33:47 +02:00
class PoolError(HTTPError):
2020-11-19 15:20:52 +01:00
"""Base exception for errors caused within a pool."""
2019-10-09 16:16:37 +02:00
2013-08-15 15:33:47 +02:00
def __init__(self, pool, message):
self.pool = pool
HTTPError.__init__(self, "%s: %s" % (pool, message))
def __reduce__(self):
# For pickling purposes.
return self.__class__, (None, None)
class RequestError(PoolError):
2020-11-19 15:20:52 +01:00
"""Base exception for PoolErrors that have associated URLs."""
2019-10-09 16:16:37 +02:00
2013-08-15 15:33:47 +02:00
def __init__(self, pool, url, message):
self.url = url
PoolError.__init__(self, pool, message)
def __reduce__(self):
# For pickling purposes.
return self.__class__, (None, self.url, None)
class SSLError(HTTPError):
2020-11-19 15:20:52 +01:00
"""Raised when SSL certificate fails in an HTTPS connection."""
2013-08-15 15:33:47 +02:00
pass
class ProxyError(HTTPError):
2020-11-19 15:20:52 +01:00
"""Raised when the connection to a proxy fails."""
2020-07-22 01:40:07 +02:00
def __init__(self, message, error, *args):
super(ProxyError, self).__init__(message, error, *args)
self.original_error = error
2013-08-15 15:33:47 +02:00
class DecodeError(HTTPError):
2020-11-19 15:20:52 +01:00
"""Raised when automatic decoding based on Content-Type fails."""
2014-05-16 20:08:09 +02:00
pass
class ProtocolError(HTTPError):
2020-11-19 15:20:52 +01:00
"""Raised when something unexpected happens mid-request/response."""
2013-08-15 15:33:47 +02:00
pass
#: Renamed to ProtocolError but aliased for backwards compatibility.
ConnectionError = ProtocolError
2016-01-19 00:28:54 +01:00
# Leaf Exceptions
2013-08-15 15:33:47 +02:00
2019-10-09 16:16:37 +02:00
2013-08-15 15:33:47 +02:00
class MaxRetryError(RequestError):
"""Raised when the maximum number of retries is exceeded.
:param pool: The connection pool
:type pool: :class:`~urllib3.connectionpool.HTTPConnectionPool`
:param string url: The requested Url
:param exceptions.Exception reason: The underlying error
"""
2013-08-15 15:33:47 +02:00
def __init__(self, pool, url, reason=None):
self.reason = reason
2019-10-09 16:16:37 +02:00
message = "Max retries exceeded with url: %s (Caused by %r)" % (url, reason)
2013-08-15 15:33:47 +02:00
RequestError.__init__(self, pool, url, message)
class HostChangedError(RequestError):
2020-11-19 15:20:52 +01:00
"""Raised when an existing pool gets a request for a foreign host."""
2013-08-15 15:33:47 +02:00
def __init__(self, pool, url, retries=3):
message = "Tried to open a foreign host with url: %s" % url
RequestError.__init__(self, pool, url, message)
self.retries = retries
class TimeoutStateError(HTTPError):
2020-11-19 15:20:52 +01:00
"""Raised when passing an invalid state to a timeout"""
2019-10-09 16:16:37 +02:00
2013-08-15 15:33:47 +02:00
pass
class TimeoutError(HTTPError):
2020-11-19 15:20:52 +01:00
"""Raised when a socket timeout error occurs.
2013-08-15 15:33:47 +02:00
Catching this error will catch both :exc:`ReadTimeoutErrors
<ReadTimeoutError>` and :exc:`ConnectTimeoutErrors <ConnectTimeoutError>`.
"""
2019-10-09 16:16:37 +02:00
2013-08-15 15:33:47 +02:00
pass
class ReadTimeoutError(TimeoutError, RequestError):
2020-11-19 15:20:52 +01:00
"""Raised when a socket timeout occurs while receiving data from a server"""
2013-08-15 15:33:47 +02:00
pass
# This timeout error does not have a URL attached and needs to inherit from the
# base HTTPError
class ConnectTimeoutError(TimeoutError):
2020-11-19 15:20:52 +01:00
"""Raised when a socket timeout occurs while connecting to a server"""
2013-08-15 15:33:47 +02:00
pass
2016-01-19 00:28:54 +01:00
class NewConnectionError(ConnectTimeoutError, PoolError):
2020-11-19 15:20:52 +01:00
"""Raised when we fail to establish a new connection. Usually ECONNREFUSED."""
2016-01-19 00:28:54 +01:00
pass
2013-08-15 15:33:47 +02:00
class EmptyPoolError(PoolError):
2020-11-19 15:20:52 +01:00
"""Raised when a pool runs out of connections and no more are allowed."""
2013-08-15 15:33:47 +02:00
pass
class ClosedPoolError(PoolError):
2020-11-19 15:20:52 +01:00
"""Raised when a request enters a pool after the pool has been closed."""
2013-08-15 15:33:47 +02:00
pass
class LocationValueError(ValueError, HTTPError):
2020-11-19 15:20:52 +01:00
"""Raised when there is something wrong with a given URL input."""
pass
class LocationParseError(LocationValueError):
2020-11-19 15:20:52 +01:00
"""Raised when get_host or similar fails to parse the URL input."""
2013-08-15 15:33:47 +02:00
def __init__(self, location):
message = "Failed to parse: %s" % location
HTTPError.__init__(self, message)
self.location = location
2020-11-19 15:20:52 +01:00
class URLSchemeUnknown(LocationValueError):
"""Raised when a URL input has an unsupported scheme."""
def __init__(self, scheme):
message = "Not supported URL scheme %s" % scheme
super(URLSchemeUnknown, self).__init__(message)
self.scheme = scheme
2014-12-02 00:48:38 +01:00
class ResponseError(HTTPError):
2020-11-19 15:20:52 +01:00
"""Used as a container for an error reason supplied in a MaxRetryError."""
2019-10-09 16:16:37 +02:00
GENERIC_ERROR = "too many error responses"
SPECIFIC_ERROR = "too many {status_code} error responses"
2014-12-02 00:48:38 +01:00
class SecurityWarning(HTTPWarning):
2020-11-19 15:20:52 +01:00
"""Warned when performing security reducing actions"""
pass
2016-01-19 00:28:54 +01:00
class SubjectAltNameWarning(SecurityWarning):
2020-11-19 15:20:52 +01:00
"""Warned when connecting to a host with a certificate missing a SAN."""
2016-01-19 00:28:54 +01:00
pass
class InsecureRequestWarning(SecurityWarning):
2020-11-19 15:20:52 +01:00
"""Warned when making an unverified HTTPS request."""
pass
class SystemTimeWarning(SecurityWarning):
2020-11-19 15:20:52 +01:00
"""Warned when system time is suspected to be wrong"""
pass
class InsecurePlatformWarning(SecurityWarning):
2020-11-19 15:20:52 +01:00
"""Warned when certain TLS/SSL configuration is not available on a platform."""
pass
2015-04-30 02:16:19 +02:00
2016-01-19 00:28:54 +01:00
class SNIMissingWarning(HTTPWarning):
2020-11-19 15:20:52 +01:00
"""Warned when making a HTTPS request without SNI available."""
2016-01-19 00:28:54 +01:00
pass
class DependencyWarning(HTTPWarning):
"""
Warned when an attempt is made to import a module with missing optional
dependencies.
"""
2019-10-09 16:16:37 +02:00
pass
2015-04-30 02:16:19 +02:00
class ResponseNotChunked(ProtocolError, ValueError):
2020-11-19 15:20:52 +01:00
"""Response needs to be chunked in order to read it as chunks."""
2015-04-30 02:16:19 +02:00
pass
2016-01-19 00:28:54 +01:00
class BodyNotHttplibCompatible(HTTPError):
"""
2020-11-19 15:20:52 +01:00
Body should be :class:`http.client.HTTPResponse` like
(have an fp attribute which returns raw chunks) for read_chunked().
"""
2019-10-09 16:16:37 +02:00
pass
class IncompleteRead(HTTPError, httplib_IncompleteRead):
"""
Response length doesn't match expected Content-Length
2020-11-19 15:20:52 +01:00
Subclass of :class:`http.client.IncompleteRead` to allow int value
for ``partial`` to avoid creating large objects on streamed reads.
"""
2019-10-09 16:16:37 +02:00
def __init__(self, partial, expected):
super(IncompleteRead, self).__init__(partial, expected)
def __repr__(self):
2020-01-21 08:49:56 +01:00
return "IncompleteRead(%i bytes read, %i more expected)" % (
2019-10-09 16:16:37 +02:00
self.partial,
self.expected,
)
2020-11-19 15:20:52 +01:00
class InvalidChunkLength(HTTPError, httplib_IncompleteRead):
"""Invalid chunk length in a chunked response."""
def __init__(self, response, length):
super(InvalidChunkLength, self).__init__(
response.tell(), response.length_remaining
)
self.response = response
self.length = length
def __repr__(self):
return "InvalidChunkLength(got length %r, %i bytes read)" % (
self.length,
self.partial,
)
class InvalidHeader(HTTPError):
2020-11-19 15:20:52 +01:00
"""The header provided was somehow invalid."""
pass
2020-11-19 15:20:52 +01:00
class ProxySchemeUnknown(AssertionError, URLSchemeUnknown):
"""ProxyManager does not support the supplied scheme"""
2016-01-19 00:28:54 +01:00
# TODO(t-8ch): Stop inheriting from AssertionError in v2.0.
def __init__(self, scheme):
message = "Not supported proxy scheme %s" % scheme
super(ProxySchemeUnknown, self).__init__(message)
2020-11-19 15:20:52 +01:00
class ProxySchemeUnsupported(ValueError):
"""Fetching HTTPS resources through HTTPS proxies is unsupported"""
pass
2016-01-19 00:28:54 +01:00
class HeaderParsingError(HTTPError):
2020-11-19 15:20:52 +01:00
"""Raised by assert_header_parsing, but we convert it to a log.warning statement."""
2019-10-09 16:16:37 +02:00
2016-01-19 00:28:54 +01:00
def __init__(self, defects, unparsed_data):
2019-10-09 16:16:37 +02:00
message = "%s, unparsed data: %r" % (defects or "Unknown", unparsed_data)
2016-01-19 00:28:54 +01:00
super(HeaderParsingError, self).__init__(message)
class UnrewindableBodyError(HTTPError):
2020-11-19 15:20:52 +01:00
"""urllib3 encountered an error when trying to rewind a body"""
pass