aio-libs / aiohttp
1
"""HTTP related errors."""
2

3 10
import asyncio
4 10
from typing import TYPE_CHECKING, Any, Optional, Tuple, Union
5

6 10
from .http_parser import RawResponseMessage
7 10
from .typedefs import LooseHeaders
8

9 10
try:
10 10
    import ssl
11

12 10
    SSLContext = ssl.SSLContext
13
except ImportError:  # pragma: no cover
14
    ssl = SSLContext = None  # type: ignore[assignment]
15

16

17
if TYPE_CHECKING:  # pragma: no cover
18
    from .client_reqrep import ClientResponse, ConnectionKey, Fingerprint, RequestInfo
19
else:
20 10
    RequestInfo = ClientResponse = ConnectionKey = None
21

22 10
__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
)
42

43

44 10
class ClientError(Exception):
45
    """Base class for client connection errors."""
46

47

48 10
class ClientResponseError(ClientError):
49
    """Connection error during reading response.
50

51
    request_info: instance of RequestInfo
52
    """
53

54 10
    def __init__(
55
        self,
56
        request_info: RequestInfo,
57
        history: Tuple[ClientResponse, ...],
58
        *,
59
        status: Optional[int] = None,
60
        message: str = "",
61
        headers: Optional[LooseHeaders] = None,
62
    ) -> None:
63 10
        self.request_info = request_info
64 10
        if status is not None:
65 10
            self.status = status
66
        else:
67 10
            self.status = 0
68 10
        self.message = message
69 10
        self.headers = headers
70 10
        self.history = history
71 10
        self.args = (request_info, history)
72

73 10
    def __str__(self) -> str:
74 10
        return "{}, message={!r}, url={!r}".format(
75
            self.status,
76
            self.message,
77
            self.request_info.real_url,
78
        )
79

80 10
    def __repr__(self) -> str:
81 10
        args = f"{self.request_info!r}, {self.history!r}"
82 10
        if self.status != 0:
83 10
            args += f", status={self.status!r}"
84 10
        if self.message != "":
85 10
            args += f", message={self.message!r}"
86 10
        if self.headers is not None:
87 10
            args += f", headers={self.headers!r}"
88 10
        return "{}({})".format(type(self).__name__, args)
89

90

91 10
class ContentTypeError(ClientResponseError):
92
    """ContentType found is not valid."""
93

94

95 10
class WSServerHandshakeError(ClientResponseError):
96
    """websocket server handshake error."""
97

98

99 10
class ClientHttpProxyError(ClientResponseError):
100
    """HTTP proxy error.
101

102
    Raised in :class:`aiohttp.connector.TCPConnector` if
103
    proxy responds with status other than ``200 OK``
104
    on ``CONNECT`` request.
105
    """
106

107

108 10
class TooManyRedirects(ClientResponseError):
109
    """Client was redirected too many times."""
110

111

112 10
class ClientConnectionError(ClientError):
113
    """Base class for client socket errors."""
114

115

116 10
class ClientOSError(ClientConnectionError, OSError):
117
    """OSError error."""
118

119

120 10
class ClientConnectorError(ClientOSError):
121
    """Client connector error.
122

123
    Raised in :class:`aiohttp.connector.TCPConnector` if
124
        connection to proxy can not be established.
125
    """
126

127 10
    def __init__(self, connection_key: ConnectionKey, os_error: OSError) -> None:
128 10
        self._conn_key = connection_key
129 10
        self._os_error = os_error
130 10
        super().__init__(os_error.errno, os_error.strerror)
131 10
        self.args = (connection_key, os_error)
132

133 10
    @property
134 10
    def os_error(self) -> OSError:
135 10
        return self._os_error
136

137 10
    @property
138 10
    def host(self) -> str:
139 10
        return self._conn_key.host
140

141 10
    @property
142 10
    def port(self) -> Optional[int]:
143 10
        return self._conn_key.port
144

145 10
    @property
146 10
    def ssl(self) -> Union[SSLContext, None, bool, "Fingerprint"]:
147 10
        return self._conn_key.ssl
148

149 10
    def __str__(self) -> str:
150 10
        return "Cannot connect to host {0.host}:{0.port} ssl:{1} [{2}]".format(
151
            self, self.ssl if self.ssl is not None else "default", self.strerror
152
        )
153

154
    # OSError.__reduce__ does too much black magick
155 10
    __reduce__ = BaseException.__reduce__
156

157

158 10
class ClientProxyConnectionError(ClientConnectorError):
159
    """Proxy connection error.
160

161
    Raised in :class:`aiohttp.connector.TCPConnector` if
162
        connection to proxy can not be established.
163
    """
164

165

166 10
class UnixClientConnectorError(ClientConnectorError):
167
    """Unix connector error.
168

169
    Raised in :py:class:`aiohttp.connector.UnixConnector`
170
    if connection to unix socket can not be established.
171
    """
172

173 10
    def __init__(
174
        self, path: str, connection_key: ConnectionKey, os_error: OSError
175
    ) -> None:
176 7
        self._path = path
177 7
        super().__init__(connection_key, os_error)
178

179 10
    @property
180 10
    def path(self) -> str:
181 0
        return self._path
182

183 10
    def __str__(self) -> str:
184 0
        return "Cannot connect to unix socket {0.path} ssl:{1} [{2}]".format(
185
            self, self.ssl if self.ssl is not None else "default", self.strerror
186
        )
187

188

189 10
class ServerConnectionError(ClientConnectionError):
190
    """Server connection errors."""
191

192

193 10
class ServerDisconnectedError(ServerConnectionError):
194
    """Server disconnected."""
195

196 10
    def __init__(self, message: Union[RawResponseMessage, str, None] = None) -> None:
197 10
        if message is None:
198 10
            message = "Server disconnected"
199

200 10
        self.args = (message,)
201 10
        self.message = message
202

203

204 10
class ServerTimeoutError(ServerConnectionError, asyncio.TimeoutError):
205
    """Server timeout error."""
206

207

208 10
class ServerFingerprintMismatch(ServerConnectionError):
209
    """SSL certificate does not match expected fingerprint."""
210

211 10
    def __init__(self, expected: bytes, got: bytes, host: str, port: int) -> None:
212 10
        self.expected = expected
213 10
        self.got = got
214 10
        self.host = host
215 10
        self.port = port
216 10
        self.args = (expected, got, host, port)
217

218 10
    def __repr__(self) -> str:
219 10
        return "<{} expected={!r} got={!r} host={!r} port={!r}>".format(
220
            self.__class__.__name__, self.expected, self.got, self.host, self.port
221
        )
222

223

224 10
class ClientPayloadError(ClientError):
225
    """Response payload error."""
226

227

228 10
class InvalidURL(ClientError, ValueError):
229
    """Invalid URL.
230

231
    URL used for fetching is malformed, e.g. it doesn't contains host
232
    part."""
233

234
    # Derive from ValueError for backward compatibility
235

236 10
    def __init__(self, url: Any) -> None:
237
        # The type of url is not yarl.URL because the exception can be raised
238
        # on URL(url) call
239 10
        super().__init__(url)
240

241 10
    @property
242 10
    def url(self) -> Any:
243 10
        return self.args[0]
244

245 10
    def __repr__(self) -> str:
246 10
        return f"<{self.__class__.__name__} {self.url}>"
247

248

249 10
class ClientSSLError(ClientConnectorError):
250
    """Base error for ssl.*Errors."""
251

252

253 10
if ssl is not None:
254 10
    cert_errors = (ssl.CertificateError,)
255 10
    cert_errors_bases = (
256
        ClientSSLError,
257
        ssl.CertificateError,
258
    )
259

260 10
    ssl_errors = (ssl.SSLError,)
261 10
    ssl_error_bases = (ClientSSLError, ssl.SSLError)
262
else:  # pragma: no cover
263
    cert_errors = tuple()
264
    cert_errors_bases = (
265
        ClientSSLError,
266
        ValueError,
267
    )
268

269
    ssl_errors = tuple()
270
    ssl_error_bases = (ClientSSLError,)
271

272

273 10
class ClientConnectorSSLError(*ssl_error_bases):  # type: ignore[misc]
274
    """Response ssl error."""
275

276

277 10
class ClientConnectorCertificateError(*cert_errors_bases):  # type: ignore[misc]
278
    """Response certificate error."""
279

280 10
    def __init__(
281
        self, connection_key: ConnectionKey, certificate_error: Exception
282
    ) -> None:
283 10
        self._conn_key = connection_key
284 10
        self._certificate_error = certificate_error
285 10
        self.args = (connection_key, certificate_error)
286

287 10
    @property
288 10
    def certificate_error(self) -> Exception:
289 10
        return self._certificate_error
290

291 10
    @property
292 10
    def host(self) -> str:
293 10
        return self._conn_key.host
294

295 10
    @property
296 10
    def port(self) -> Optional[int]:
297 10
        return self._conn_key.port
298

299 10
    @property
300 10
    def ssl(self) -> bool:
301 10
        return self._conn_key.is_ssl
302

303 10
    def __str__(self) -> str:
304 10
        return (
305
            "Cannot connect to host {0.host}:{0.port} ssl:{0.ssl} "
306
            "[{0.certificate_error.__class__.__name__}: "
307
            "{0.certificate_error.args}]".format(self)
308
        )

Read our documentation on viewing source code .

Loading