Uh oh!
There was an error while loading. Please reload this page.
Map httpcore exceptions for Response read methods - #1190
Conversation
florimondmanca
left a comment
There was a problem hiding this comment.
Interesting…!
The main reason I'm not immediately "OK let's go" on this is that ReadError & co only have a request=... parameter, but in this case we do have a response available that we could attach to the exception. Perhaps no need to sweat it since we have the response object available in the scope anyway when doing local exception handling, but just wanted to leave this heads up.
withhttpx.stream(...) asresponse:
try:
forlineinresponse.iter_lines():
...
excepthttpx.ReadErrorasexc:
print("Stream broken", response) # No need for `exc.response` anyway?Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Florimond Manca <florimond.manca@gmail.com>
lovelydinosaur
commented
Aug 19, 2020
@florimondmanca Well noted. There's already other exception classes that can fall into that category, which I'm totally okay with, because it's consistent & simple. As you say, the response will be available in the local scope anyways. |
lovelydinosaur
commented
Aug 19, 2020
There's also an alternate implementation where we wrap around the stream that we pass to the Lines 795 to 801 in d10b7cd Eg. something along these lines... ResponseStream(ContentStream):
def__int__(self, request, stream):
self._request=requestself._transport_stream=streamdef__iter__(self):
withmap_exceptions(HTTPCORE_EXC_MAP, request=self._request)
forchunkinself._transport_stream:
yieldchunkdefclose(self):
self._transport_stream.close()Then... response=Response(
...
stream=ResponseStream(request, stream)
)I don't see any reason for us to prefer that here & now, but mentioning it in case it's a useful reference point for anything in the future. |
Fix #1188
Add
map_exceptionstoResponse.iter_rawandResponse.aiter_raw, making sure allhttpcoreexceptions are mapped.And add a slow_stream response test to make sure
ReadTimeoutocurred while the client is reading the response body, not in the request time.