Skip to content

Commit

Permalink
map_exceptions in request.aclose() (#1465)
Browse files Browse the repository at this point in the history
* map_exceptions in request.aclose()
  • Loading branch information
adamhooper authored Feb 17, 2021
1 parent bd4caa8 commit 0f280af
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion httpx/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1500,7 +1500,8 @@ async def _send_single_request(
async def on_close(response: Response) -> None:
response.elapsed = datetime.timedelta(seconds=await timer.async_elapsed())
if hasattr(stream, "aclose"):
await stream.aclose()
with map_exceptions(HTTPCORE_EXC_MAP, request=request):
await stream.aclose()

response = Response(
status_code,
Expand Down
19 changes: 19 additions & 0 deletions tests/client/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,25 @@ async def test_mounted_transport():
assert response.json() == {"app": "mounted"}


@pytest.mark.usefixtures("async_environment")
async def test_response_aclose_map_exceptions():
class BrokenStream:
async def __aiter__(self):
# so we're an AsyncIterator
pass # pragma: nocover

async def aclose(self):
raise httpcore.CloseError(OSError(104, "Connection reset by peer"))

def handle(request: httpx.Request) -> httpx.Response:
return httpx.Response(200, stream=BrokenStream())

async with httpx.AsyncClient(transport=httpx.MockTransport(handle)) as client:
async with client.stream("GET", "http://example.com") as response:
with pytest.raises(httpx.CloseError):
await response.aclose()


@pytest.mark.usefixtures("async_environment")
async def test_async_mock_transport():
async def hello_world(request):
Expand Down

0 comments on commit 0f280af

Please sign in to comment.