1818 _read_capped_async ,
1919 _validate_max_response_body_bytes ,
2020)
21- from httpware ._internal .exception_mapping import (
22- _httpx2_exception_mapper ,
23- _httpx2_exception_mapper_sync ,
24- map_httpx2_exception ,
25- )
21+ from httpware ._internal .exception_mapping import MAPPED_HTTPX2_ERRORS , map_httpx2_exception
2622from httpware ._internal .status import (
2723 STREAMING_BODY_MARKER ,
2824 _is_streaming_body_async ,
3228)
3329from httpware .decoders import ResponseDecoder
3430from httpware .decoders ._resolver import _DecoderResolver
31+ from httpware .errors import TransportError
3532from httpware .middleware import AsyncMiddleware , AsyncNext , Middleware , Next
3633from httpware .middleware .chain import compose , compose_async
3734
@@ -444,12 +441,18 @@ def __init__(
444441
445442 async def _terminal (self , request : httpx2 .Request ) -> httpx2 .Response :
446443 cap = self ._max_response_body_bytes
447- async with _httpx2_exception_mapper ( self . _httpx2_client ) :
444+ try :
448445 if cap is None :
449446 response = await self ._httpx2_client .send (request )
450447 else :
451448 streaming = await _send_capped_async (self ._httpx2_client , request , cap )
452449 response = await _read_capped_and_close_async (streaming , cap )
450+ except MAPPED_HTTPX2_ERRORS as exc :
451+ raise map_httpx2_exception (exc ) from exc
452+ except RuntimeError as exc :
453+ if self ._httpx2_client .is_closed :
454+ raise TransportError (str (exc )) from exc
455+ raise
453456 _raise_on_status_error (response )
454457 return response
455458
@@ -1294,7 +1297,7 @@ async def stream( # noqa: PLR0913 — mirrors httpx2 per-method signatures; kwa
12941297 accessible. You lose the streaming property on errors; rare in practice.
12951298
12961299 Maps httpx2 exceptions raised during the request OR body consumption to
1297- httpware exceptions via _httpx2_exception_mapper .
1300+ httpware exceptions.
12981301 """
12991302 kwargs = _assemble_request_kwargs (
13001303 params = params ,
@@ -1314,15 +1317,22 @@ async def stream( # noqa: PLR0913 — mirrors httpx2 per-method signatures; kwa
13141317 if cap is None
13151318 else _stream_capped_async (self ._httpx2_client , method , url , kwargs , cap )
13161319 )
1317- async with _httpx2_exception_mapper (self ._httpx2_client ), opened as response :
1318- if HTTPStatus .BAD_REQUEST <= response .status_code < 600 : # noqa: PLR2004 — 600 is the synthetic upper bound for 5xx
1319- if cap is None :
1320- await response .aread () # pre-read body so exc.response.content works
1321- _raise_on_status_error (response )
1322- else :
1323- # Bound the error pre-read; raises ResponseTooLargeError when over cap.
1324- _raise_on_status_error (await _read_capped_async (response , cap , response .request ))
1325- yield response
1320+ try :
1321+ async with opened as response :
1322+ if HTTPStatus .BAD_REQUEST <= response .status_code < 600 : # noqa: PLR2004 — 600 is the synthetic upper bound for 5xx
1323+ if cap is None :
1324+ await response .aread () # pre-read body so exc.response.content works
1325+ _raise_on_status_error (response )
1326+ else :
1327+ # Bound the error pre-read; raises ResponseTooLargeError when over cap.
1328+ _raise_on_status_error (await _read_capped_async (response , cap , response .request ))
1329+ yield response
1330+ except MAPPED_HTTPX2_ERRORS as exc :
1331+ raise map_httpx2_exception (exc ) from exc
1332+ except RuntimeError as exc :
1333+ if self ._httpx2_client .is_closed :
1334+ raise TransportError (str (exc )) from exc
1335+ raise
13261336
13271337 async def __aenter__ (self ) -> typing .Self :
13281338 """Enter the async context manager; return self."""
@@ -1393,12 +1403,18 @@ def __init__(
13931403
13941404 def _terminal (self , request : httpx2 .Request ) -> httpx2 .Response :
13951405 cap = self ._max_response_body_bytes
1396- with _httpx2_exception_mapper_sync ( self . _httpx2_client ) :
1406+ try :
13971407 if cap is None :
13981408 response = self ._httpx2_client .send (request )
13991409 else :
14001410 streaming = _send_capped (self ._httpx2_client , request , cap )
14011411 response = _read_capped_and_close (streaming , cap )
1412+ except MAPPED_HTTPX2_ERRORS as exc :
1413+ raise map_httpx2_exception (exc ) from exc
1414+ except RuntimeError as exc :
1415+ if self ._httpx2_client .is_closed :
1416+ raise TransportError (str (exc )) from exc
1417+ raise
14021418 _raise_on_status_error (response )
14031419 return response
14041420
@@ -2260,7 +2276,7 @@ def stream( # noqa: PLR0913 — mirrors httpx2 per-method signatures; kwargs-fo
22602276 body is pre-read so exc.response.content is accessible.
22612277
22622278 Maps httpx2 exceptions raised during the request OR body consumption to
2263- httpware exceptions via _httpx2_exception_mapper_sync .
2279+ httpware exceptions.
22642280 """
22652281 kwargs = _assemble_request_kwargs (
22662282 params = params ,
@@ -2280,12 +2296,19 @@ def stream( # noqa: PLR0913 — mirrors httpx2 per-method signatures; kwargs-fo
22802296 if cap is None
22812297 else _stream_capped (self ._httpx2_client , method , url , kwargs , cap )
22822298 )
2283- with _httpx2_exception_mapper_sync (self ._httpx2_client ), opened as response :
2284- if HTTPStatus .BAD_REQUEST <= response .status_code < 600 : # noqa: PLR2004 — 600 is the synthetic upper bound for 5xx
2285- if cap is None :
2286- response .read () # pre-read body so exc.response.content works
2287- _raise_on_status_error (response )
2288- else :
2289- # Bound the error pre-read; raises ResponseTooLargeError when over cap.
2290- _raise_on_status_error (_read_capped (response , cap , response .request ))
2291- yield response
2299+ try :
2300+ with opened as response :
2301+ if HTTPStatus .BAD_REQUEST <= response .status_code < 600 : # noqa: PLR2004 — 600 is the synthetic upper bound for 5xx
2302+ if cap is None :
2303+ response .read () # pre-read body so exc.response.content works
2304+ _raise_on_status_error (response )
2305+ else :
2306+ # Bound the error pre-read; raises ResponseTooLargeError when over cap.
2307+ _raise_on_status_error (_read_capped (response , cap , response .request ))
2308+ yield response
2309+ except MAPPED_HTTPX2_ERRORS as exc :
2310+ raise map_httpx2_exception (exc ) from exc
2311+ except RuntimeError as exc :
2312+ if self ._httpx2_client .is_closed :
2313+ raise TransportError (str (exc )) from exc
2314+ raise
0 commit comments