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
@@ -390,6 +387,8 @@ def _build_request(
390387 client : httpx2 .Client | httpx2 .AsyncClient , method : str , url : httpx2 .URL | str , kwargs : dict [str , typing .Any ]
391388) -> httpx2 .Request :
392389 """Build via `client`, keeping the URL's own query bytes ahead of any params, which httpx2 would replace."""
390+ if (isinstance (url , str ) and "?" not in url ) or (kwargs .get ("params" ) is None and not client .params ):
391+ return client .build_request (method , url , ** kwargs )
393392 parsed = httpx2 .URL (url )
394393 if not parsed .query :
395394 return client .build_request (method , url , ** kwargs )
@@ -442,12 +441,18 @@ def __init__(
442441
443442 async def _terminal (self , request : httpx2 .Request ) -> httpx2 .Response :
444443 cap = self ._max_response_body_bytes
445- async with _httpx2_exception_mapper ( self . _httpx2_client ) :
444+ try :
446445 if cap is None :
447446 response = await self ._httpx2_client .send (request )
448447 else :
449448 streaming = await _send_capped_async (self ._httpx2_client , request , cap )
450449 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
451456 _raise_on_status_error (response )
452457 return response
453458
@@ -1292,7 +1297,7 @@ async def stream( # noqa: PLR0913 — mirrors httpx2 per-method signatures; kwa
12921297 accessible. You lose the streaming property on errors; rare in practice.
12931298
12941299 Maps httpx2 exceptions raised during the request OR body consumption to
1295- httpware exceptions via _httpx2_exception_mapper .
1300+ httpware exceptions.
12961301 """
12971302 kwargs = _assemble_request_kwargs (
12981303 params = params ,
@@ -1312,15 +1317,22 @@ async def stream( # noqa: PLR0913 — mirrors httpx2 per-method signatures; kwa
13121317 if cap is None
13131318 else _stream_capped_async (self ._httpx2_client , method , url , kwargs , cap )
13141319 )
1315- async with _httpx2_exception_mapper (self ._httpx2_client ), opened as response :
1316- if HTTPStatus .BAD_REQUEST <= response .status_code < 600 : # noqa: PLR2004 — 600 is the synthetic upper bound for 5xx
1317- if cap is None :
1318- await response .aread () # pre-read body so exc.response.content works
1319- _raise_on_status_error (response )
1320- else :
1321- # Bound the error pre-read; raises ResponseTooLargeError when over cap.
1322- _raise_on_status_error (await _read_capped_async (response , cap , response .request ))
1323- 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
13241336
13251337 async def __aenter__ (self ) -> typing .Self :
13261338 """Enter the async context manager; return self."""
@@ -1391,12 +1403,18 @@ def __init__(
13911403
13921404 def _terminal (self , request : httpx2 .Request ) -> httpx2 .Response :
13931405 cap = self ._max_response_body_bytes
1394- with _httpx2_exception_mapper_sync ( self . _httpx2_client ) :
1406+ try :
13951407 if cap is None :
13961408 response = self ._httpx2_client .send (request )
13971409 else :
13981410 streaming = _send_capped (self ._httpx2_client , request , cap )
13991411 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
14001418 _raise_on_status_error (response )
14011419 return response
14021420
@@ -2258,7 +2276,7 @@ def stream( # noqa: PLR0913 — mirrors httpx2 per-method signatures; kwargs-fo
22582276 body is pre-read so exc.response.content is accessible.
22592277
22602278 Maps httpx2 exceptions raised during the request OR body consumption to
2261- httpware exceptions via _httpx2_exception_mapper_sync .
2279+ httpware exceptions.
22622280 """
22632281 kwargs = _assemble_request_kwargs (
22642282 params = params ,
@@ -2278,12 +2296,19 @@ def stream( # noqa: PLR0913 — mirrors httpx2 per-method signatures; kwargs-fo
22782296 if cap is None
22792297 else _stream_capped (self ._httpx2_client , method , url , kwargs , cap )
22802298 )
2281- with _httpx2_exception_mapper_sync (self ._httpx2_client ), opened as response :
2282- if HTTPStatus .BAD_REQUEST <= response .status_code < 600 : # noqa: PLR2004 — 600 is the synthetic upper bound for 5xx
2283- if cap is None :
2284- response .read () # pre-read body so exc.response.content works
2285- _raise_on_status_error (response )
2286- else :
2287- # Bound the error pre-read; raises ResponseTooLargeError when over cap.
2288- _raise_on_status_error (_read_capped (response , cap , response .request ))
2289- 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