1import contextlib
2from typing import Iterator, Mapping, Type
3
4ExceptionMapping = Mapping[Type[Exception], Type[Exception]]
5
6
7@contextlib.contextmanager
8def map_exceptions(map: ExceptionMapping) -> Iterator[None]:
9 try:
10 yield
11 except Exception as exc: # noqa: PIE786
12 for from_exc, to_exc in map.items():
13 if isinstance(exc, from_exc):
14 raise to_exc(exc) from exc
15 raise # pragma: nocover
16
17
18class ConnectionNotAvailable(Exception):
19 pass
20
21
22class ProxyError(Exception):
23 pass
24
25
26class UnsupportedProtocol(Exception):
27 pass
28
29
30class ProtocolError(Exception):
31 pass
32
33
34class RemoteProtocolError(ProtocolError):
35 pass
36
37
38class LocalProtocolError(ProtocolError):
39 pass
40
41
42# Timeout errors
43
44
45class TimeoutException(Exception):
46 pass
47
48
49class PoolTimeout(TimeoutException):
50 pass
51
52
53class ConnectTimeout(TimeoutException):
54 pass
55
56
57class ReadTimeout(TimeoutException):
58 pass
59
60
61class WriteTimeout(TimeoutException):
62 pass
63
64
65# Network errors
66
67
68class NetworkError(Exception):
69 pass
70
71
72class ConnectError(NetworkError):
73 pass
74
75
76class ReadError(NetworkError):
77 pass
78
79
80class WriteError(NetworkError):
81 pass