1919from functools import wraps
2020from typing import TYPE_CHECKING , Any , Callable , Coroutine , Optional , Union
2121
22- from httpx import ConnectError , ConnectTimeout , ReadTimeout , WriteError , WriteTimeout
22+ from httpx import ConnectError , ConnectTimeout , CookieConflict , HTTPError , InvalidURL , ReadTimeout , StreamError
2323
2424from acouchbase_analytics .protocol ._core .anyio_utils import sleep
2525from couchbase_analytics .common .errors import AnalyticsError , InternalSDKError , TimeoutError
@@ -39,7 +39,7 @@ class AsyncRetryHandler:
3939
4040 @staticmethod
4141 async def handle_httpx_retry (
42- ex : Union [ConnectError , ConnectTimeout , WriteError , WriteTimeout ], ctx : AsyncRequestContext
42+ ex : Union [ConnectError , ConnectTimeout ], ctx : AsyncRequestContext
4343 ) -> Optional [Exception ]:
4444 err_str = str (ex )
4545 if 'SSL:' in err_str :
@@ -107,7 +107,7 @@ async def wrapped_fn(self: AsyncHttpStreamingResponse) -> None: # noqa: C901
107107 continue
108108 await self ._request_context .shutdown (type (ex ), ex , ex .__traceback__ )
109109 raise err from None
110- except (ConnectError , ConnectTimeout , WriteError , WriteTimeout ) as ex :
110+ except (ConnectError , ConnectTimeout ) as ex :
111111 err = await AsyncRetryHandler .handle_httpx_retry (ex , self ._request_context )
112112 if err is None :
113113 continue
@@ -120,6 +120,12 @@ async def wrapped_fn(self: AsyncHttpStreamingResponse) -> None: # noqa: C901
120120 raise TimeoutError (
121121 message = 'Request timed out.' , context = str (self ._request_context .error_context )
122122 ) from None
123+ except (CookieConflict , HTTPError , StreamError , InvalidURL ) as ex :
124+ # these are not retriable errors, so we just shutdown the request context and raise the error
125+ await self ._request_context .shutdown (type (ex ), ex , ex .__traceback__ )
126+ raise AnalyticsError (
127+ cause = ex , message = str (ex ), context = str (self ._request_context .error_context )
128+ ) from None
123129 except AnalyticsError :
124130 # if an AnalyticsError is raised, we have already shut down the request context
125131 raise
@@ -142,9 +148,15 @@ async def wrapped_fn(self: AsyncHttpStreamingResponse) -> None: # noqa: C901
142148 raise CancelledError ('Request was cancelled.' ) from None
143149 if self ._request_context .request_error is not None :
144150 raise self ._request_context .request_error from None
145- raise InternalSDKError (
146- cause = ex , message = str (ex ), context = str (self ._request_context .error_context )
147- ) from None
151+ if isinstance (ex , Exception ):
152+ # If the exception is an Exception, we raise it as an InternalSDKError as this is
153+ # an unexpected error in the SDK
154+ raise InternalSDKError (
155+ cause = ex , message = str (ex ), context = str (self ._request_context .error_context )
156+ ) from None
157+ # we should have handled CancelledError and TimeoutError above, so if we get here,
158+ # raise the BaseException as is (most likely a KeyboardInterrupt)
159+ raise ex
148160 finally :
149161 if not RequestState .is_okay (self ._request_context .request_state ):
150162 await self .close ()
0 commit comments