|
11 | 11 | from types import TracebackType |
12 | 12 | from typing import Any, Callable, Literal, cast |
13 | 13 |
|
14 | | -from ..client import ClientProtocol, backoff |
| 14 | +from ..client import ClientProtocol, backoff, process_exception |
15 | 15 | from ..datastructures import Headers, HeadersLike |
16 | 16 | from ..exceptions import ( |
17 | | - InvalidMessage, |
18 | 17 | InvalidProxyMessage, |
19 | 18 | InvalidProxyStatus, |
20 | 19 | InvalidStatus, |
@@ -128,50 +127,6 @@ def process_event(self, event: Event) -> None: |
128 | 127 | super().process_event(event) |
129 | 128 |
|
130 | 129 |
|
131 | | -def process_exception(exc: Exception) -> Exception | None: |
132 | | - """ |
133 | | - Determine whether a connection error is retryable or fatal. |
134 | | -
|
135 | | - When reconnecting automatically with ``async for ... in connect(...)``, if a |
136 | | - connection attempt fails, :func:`process_exception` is called to determine |
137 | | - whether to retry connecting or to raise the exception. |
138 | | -
|
139 | | - This function defines the default behavior, which is to retry on: |
140 | | -
|
141 | | - * :exc:`EOFError`, :exc:`OSError`, :exc:`asyncio.TimeoutError`: network |
142 | | - errors; |
143 | | - * :exc:`~websockets.exceptions.InvalidStatus` when the status code is 500, |
144 | | - 502, 503, or 504: server or proxy errors. |
145 | | -
|
146 | | - All other exceptions are considered fatal. |
147 | | -
|
148 | | - You can change this behavior with the ``process_exception`` argument of |
149 | | - :func:`connect`. |
150 | | -
|
151 | | - Return :obj:`None` if the exception is retryable i.e. when the error could |
152 | | - be transient and trying to reconnect with the same parameters could succeed. |
153 | | - The exception will be logged at the ``INFO`` level. |
154 | | -
|
155 | | - Return an exception, either ``exc`` or a new exception, if the exception is |
156 | | - fatal i.e. when trying to reconnect will most likely produce the same error. |
157 | | - That exception will be raised, breaking out of the retry loop. |
158 | | -
|
159 | | - """ |
160 | | - # This catches python-socks' ProxyConnectionError and ProxyTimeoutError. |
161 | | - if isinstance(exc, (OSError, TimeoutError)): |
162 | | - return None |
163 | | - if isinstance(exc, InvalidMessage) and isinstance(exc.__cause__, EOFError): |
164 | | - return None |
165 | | - if isinstance(exc, InvalidStatus) and exc.response.status_code in [ |
166 | | - 500, # Internal Server Error |
167 | | - 502, # Bad Gateway |
168 | | - 503, # Service Unavailable |
169 | | - 504, # Gateway Timeout |
170 | | - ]: |
171 | | - return None |
172 | | - return exc |
173 | | - |
174 | | - |
175 | 130 | # This is spelled in lower case because it's exposed as a callable in the API. |
176 | 131 | class connect: |
177 | 132 | """ |
@@ -224,7 +179,8 @@ class connect: |
224 | 179 | <../../topics/proxies>` for details. |
225 | 180 | process_exception: When reconnecting automatically, tell whether an |
226 | 181 | error is transient or fatal. The default behavior is defined by |
227 | | - :func:`process_exception`. Refer to its documentation for details. |
| 182 | + :func:`~websockets.client.process_exception`. Refer to its |
| 183 | + documentation for details. |
228 | 184 | open_timeout: Timeout for opening the connection in seconds. |
229 | 185 | :obj:`None` disables the timeout. |
230 | 186 | ping_interval: Interval between keepalive pings in seconds. |
|
0 commit comments