Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/aiohttp/client_exceptions.py
7757 views
"""HTTP related errors."""12import asyncio3import warnings4from typing import TYPE_CHECKING, Any, Optional, Tuple, Union56from .http_parser import RawResponseMessage7from .typedefs import LooseHeaders89try:10import ssl1112SSLContext = ssl.SSLContext13except ImportError: # pragma: no cover14ssl = SSLContext = None # type: ignore[assignment]151617if TYPE_CHECKING: # pragma: no cover18from .client_reqrep import ClientResponse, ConnectionKey, Fingerprint, RequestInfo19else:20RequestInfo = ClientResponse = ConnectionKey = None2122__all__ = (23"ClientError",24"ClientConnectionError",25"ClientOSError",26"ClientConnectorError",27"ClientProxyConnectionError",28"ClientSSLError",29"ClientConnectorSSLError",30"ClientConnectorCertificateError",31"ServerConnectionError",32"ServerTimeoutError",33"ServerDisconnectedError",34"ServerFingerprintMismatch",35"ClientResponseError",36"ClientHttpProxyError",37"WSServerHandshakeError",38"ContentTypeError",39"ClientPayloadError",40"InvalidURL",41)424344class ClientError(Exception):45"""Base class for client connection errors."""464748class ClientResponseError(ClientError):49"""Connection error during reading response.5051request_info: instance of RequestInfo52"""5354def __init__(55self,56request_info: RequestInfo,57history: Tuple[ClientResponse, ...],58*,59code: Optional[int] = None,60status: Optional[int] = None,61message: str = "",62headers: Optional[LooseHeaders] = None,63) -> None:64self.request_info = request_info65if code is not None:66if status is not None:67raise ValueError(68"Both code and status arguments are provided; "69"code is deprecated, use status instead"70)71warnings.warn(72"code argument is deprecated, use status instead",73DeprecationWarning,74stacklevel=2,75)76if status is not None:77self.status = status78elif code is not None:79self.status = code80else:81self.status = 082self.message = message83self.headers = headers84self.history = history85self.args = (request_info, history)8687def __str__(self) -> str:88return "{}, message={!r}, url={!r}".format(89self.status,90self.message,91self.request_info.real_url,92)9394def __repr__(self) -> str:95args = f"{self.request_info!r}, {self.history!r}"96if self.status != 0:97args += f", status={self.status!r}"98if self.message != "":99args += f", message={self.message!r}"100if self.headers is not None:101args += f", headers={self.headers!r}"102return f"{type(self).__name__}({args})"103104@property105def code(self) -> int:106warnings.warn(107"code property is deprecated, use status instead",108DeprecationWarning,109stacklevel=2,110)111return self.status112113@code.setter114def code(self, value: int) -> None:115warnings.warn(116"code property is deprecated, use status instead",117DeprecationWarning,118stacklevel=2,119)120self.status = value121122123class ContentTypeError(ClientResponseError):124"""ContentType found is not valid."""125126127class WSServerHandshakeError(ClientResponseError):128"""websocket server handshake error."""129130131class ClientHttpProxyError(ClientResponseError):132"""HTTP proxy error.133134Raised in :class:`aiohttp.connector.TCPConnector` if135proxy responds with status other than ``200 OK``136on ``CONNECT`` request.137"""138139140class TooManyRedirects(ClientResponseError):141"""Client was redirected too many times."""142143144class ClientConnectionError(ClientError):145"""Base class for client socket errors."""146147148class ClientOSError(ClientConnectionError, OSError):149"""OSError error."""150151152class ClientConnectorError(ClientOSError):153"""Client connector error.154155Raised in :class:`aiohttp.connector.TCPConnector` if156connection to proxy can not be established.157"""158159def __init__(self, connection_key: ConnectionKey, os_error: OSError) -> None:160self._conn_key = connection_key161self._os_error = os_error162super().__init__(os_error.errno, os_error.strerror)163self.args = (connection_key, os_error)164165@property166def os_error(self) -> OSError:167return self._os_error168169@property170def host(self) -> str:171return self._conn_key.host172173@property174def port(self) -> Optional[int]:175return self._conn_key.port176177@property178def ssl(self) -> Union[SSLContext, None, bool, "Fingerprint"]:179return self._conn_key.ssl180181def __str__(self) -> str:182return "Cannot connect to host {0.host}:{0.port} ssl:{1} [{2}]".format(183self, self.ssl if self.ssl is not None else "default", self.strerror184)185186# OSError.__reduce__ does too much black magick187__reduce__ = BaseException.__reduce__188189190class ClientProxyConnectionError(ClientConnectorError):191"""Proxy connection error.192193Raised in :class:`aiohttp.connector.TCPConnector` if194connection to proxy can not be established.195"""196197198class UnixClientConnectorError(ClientConnectorError):199"""Unix connector error.200201Raised in :py:class:`aiohttp.connector.UnixConnector`202if connection to unix socket can not be established.203"""204205def __init__(206self, path: str, connection_key: ConnectionKey, os_error: OSError207) -> None:208self._path = path209super().__init__(connection_key, os_error)210211@property212def path(self) -> str:213return self._path214215def __str__(self) -> str:216return "Cannot connect to unix socket {0.path} ssl:{1} [{2}]".format(217self, self.ssl if self.ssl is not None else "default", self.strerror218)219220221class ServerConnectionError(ClientConnectionError):222"""Server connection errors."""223224225class ServerDisconnectedError(ServerConnectionError):226"""Server disconnected."""227228def __init__(self, message: Union[RawResponseMessage, str, None] = None) -> None:229if message is None:230message = "Server disconnected"231232self.args = (message,)233self.message = message234235236class ServerTimeoutError(ServerConnectionError, asyncio.TimeoutError):237"""Server timeout error."""238239240class ServerFingerprintMismatch(ServerConnectionError):241"""SSL certificate does not match expected fingerprint."""242243def __init__(self, expected: bytes, got: bytes, host: str, port: int) -> None:244self.expected = expected245self.got = got246self.host = host247self.port = port248self.args = (expected, got, host, port)249250def __repr__(self) -> str:251return "<{} expected={!r} got={!r} host={!r} port={!r}>".format(252self.__class__.__name__, self.expected, self.got, self.host, self.port253)254255256class ClientPayloadError(ClientError):257"""Response payload error."""258259260class InvalidURL(ClientError, ValueError):261"""Invalid URL.262263URL used for fetching is malformed, e.g. it doesn't contains host264part.265"""266267# Derive from ValueError for backward compatibility268269def __init__(self, url: Any) -> None:270# The type of url is not yarl.URL because the exception can be raised271# on URL(url) call272super().__init__(url)273274@property275def url(self) -> Any:276return self.args[0]277278def __repr__(self) -> str:279return f"<{self.__class__.__name__} {self.url}>"280281282class ClientSSLError(ClientConnectorError):283"""Base error for ssl.*Errors."""284285286if ssl is not None:287cert_errors = (ssl.CertificateError,)288cert_errors_bases = (289ClientSSLError,290ssl.CertificateError,291)292293ssl_errors = (ssl.SSLError,)294ssl_error_bases = (ClientSSLError, ssl.SSLError)295else: # pragma: no cover296cert_errors = tuple()297cert_errors_bases = (298ClientSSLError,299ValueError,300)301302ssl_errors = tuple()303ssl_error_bases = (ClientSSLError,)304305306class ClientConnectorSSLError(*ssl_error_bases): # type: ignore[misc]307"""Response ssl error."""308309310class ClientConnectorCertificateError(*cert_errors_bases): # type: ignore[misc]311"""Response certificate error."""312313def __init__(314self, connection_key: ConnectionKey, certificate_error: Exception315) -> None:316self._conn_key = connection_key317self._certificate_error = certificate_error318self.args = (connection_key, certificate_error)319320@property321def certificate_error(self) -> Exception:322return self._certificate_error323324@property325def host(self) -> str:326return self._conn_key.host327328@property329def port(self) -> Optional[int]:330return self._conn_key.port331332@property333def ssl(self) -> bool:334return self._conn_key.is_ssl335336def __str__(self) -> str:337return (338"Cannot connect to host {0.host}:{0.port} ssl:{0.ssl} "339"[{0.certificate_error.__class__.__name__}: "340"{0.certificate_error.args}]".format(self)341)342343344