Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

map_exceptions in request.aclose() #1465

Merged
merged 10 commits into from Feb 17, 2021

Conversation

adamhooper
Copy link
Contributor

Now, clients can catch an exception that occurs during close.

[closes #1463]

@adamhooper
Copy link
Contributor Author

sigh I'm having a hard time reproducing this error on py36. Is that strictly necessary? I feel the code fix is obviously correct and the unit test is spectacularly difficult to write.

@florimondmanca
Copy link
Member

florimondmanca commented Feb 10, 2021

@adamhooper Thanks!

For the unit test — I think we can leverage transport=.... I would suggest writing a custom transport that returns a dummy stream whose .aclose() method raises an httpcore exception, and make sure that an httpx one is raised on the client side. If that makes sense?

Copy link
Member

@florimondmanca florimondmanca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking very neat!

Just a minor suggestion to increase test coverage slightly?

Comment on lines +320 to +322
async with client.stream("GET", "http://example.com") as response:
with pytest.raises(httpx.CloseError):
await response.aclose()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I correct saying this would also reproduce for a regular client.request()? If so, should we update this to…?

Suggested change
async with client.stream("GET", "http://example.com") as response:
with pytest.raises(httpx.CloseError):
await response.aclose()
with pytest.raises(httpx.CloseError):
await client.request("GET", "http://example.com")
with pytest.raises(httpx.CloseError):
async with client.stream("GET", "http://example.com"):
pass # Close on exit.

(Also dropped the explicit response.aclose() since the expected behavior is that exiting the stream() async context manager would do that for us, and fail with the proper exception.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct. And the await client.request() test is a worthy addition.

You're the maintainer, so you are welcome to change to nix the aclose() in the test ... but first, I'll explain my motivation.

During debugging, I was surprised by #1464 -- a broken stream raises the "wrong" error on shutdown.

I imagine a reasonable __aexit__() might handle an errno=104 error -- by doing nothing and raising nothing. So I aimed for an explicit test that avoids __aexit__().

The change would be correct. But as a user, I find __axit__() behavior confusing; so I wrote a more-explicit test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I like the way @adamhooper has approached the test case here, too.

@tomchristie tomchristie mentioned this pull request Feb 17, 2021
3 tasks
Copy link
Member

@tomchristie tomchristie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch here, yup. Happy with this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

response.aclose() raises httpcore errors, not HTTPError
3 participants