Skip to content

Commit 0f280af

Browse files
authored
map_exceptions in request.aclose() (#1465)
* map_exceptions in request.aclose()
1 parent bd4caa8 commit 0f280af

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

httpx/_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,8 @@ async def _send_single_request(
15001500
async def on_close(response: Response) -> None:
15011501
response.elapsed = datetime.timedelta(seconds=await timer.async_elapsed())
15021502
if hasattr(stream, "aclose"):
1503-
await stream.aclose()
1503+
with map_exceptions(HTTPCORE_EXC_MAP, request=request):
1504+
await stream.aclose()
15041505

15051506
response = Response(
15061507
status_code,

tests/client/test_async_client.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,25 @@ async def test_mounted_transport():
303303
assert response.json() == {"app": "mounted"}
304304

305305

306+
@pytest.mark.usefixtures("async_environment")
307+
async def test_response_aclose_map_exceptions():
308+
class BrokenStream:
309+
async def __aiter__(self):
310+
# so we're an AsyncIterator
311+
pass # pragma: nocover
312+
313+
async def aclose(self):
314+
raise httpcore.CloseError(OSError(104, "Connection reset by peer"))
315+
316+
def handle(request: httpx.Request) -> httpx.Response:
317+
return httpx.Response(200, stream=BrokenStream())
318+
319+
async with httpx.AsyncClient(transport=httpx.MockTransport(handle)) as client:
320+
async with client.stream("GET", "http://example.com") as response:
321+
with pytest.raises(httpx.CloseError):
322+
await response.aclose()
323+
324+
306325
@pytest.mark.usefixtures("async_environment")
307326
async def test_async_mock_transport():
308327
async def hello_world(request):

0 commit comments

Comments
 (0)