Skip to content
Myr

Map httpcore exceptions for `Response` read methods (#1190)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Map httpcore exceptions for `Response.iter_*` methods * Tweak * Change wording Co-authored-by: Florimond Manca <florimond.manca@gmail.com>

authored by Joe · committed by GitHub · parent d10b7cd · Browse files · 3 files+31 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Florimond Manca <florimond.manca@gmail.com>
  • Co-authored-by: Tom Christie <tom@tomchristie.com>

Diff stats

3 files+31 added−5 removed

  • tests:+23 added−1 removed
  • other:+8 added−4 removed

3 files+31 added−5 removed

modified:httpx/_models.py+8 added-4 removed
22 unmodified lines
23 MultiDecoder,24 TextDecoder,25)26from ._exceptions import (27 HTTPCORE_EXC_MAP,28 CookieConflict,29 HTTPStatusError,30 InvalidURL,31 NotRedirectResponse,32 RequestNotRead,33 ResponseClosed,34 ResponseNotRead,35 StreamConsumed,36 map_exceptions,37)38from ._status_codes import codes39from ._types import (40 CookieTypes,
891 unmodified lines
932 if self.is_closed:933 raise ResponseClosed()934935 self.is_stream_consumed = True936 with map_exceptions(HTTPCORE_EXC_MAP, request=self.request):934 for part in self._raw_stream:935 yield part937 for part in self._raw_stream:938 yield part939 self.close()940941 def next(self) -> "Response":942 """
66 unmodified lines
1009 if self.is_closed:1010 raise ResponseClosed()10111012 self.is_stream_consumed = True1013 with map_exceptions(HTTPCORE_EXC_MAP, request=self.request):1010 async for part in self._raw_stream:1011 yield part1014 async for part in self._raw_stream:1015 yield part1016 await self.aclose()10171018 async def anext(self) -> "Response":1019 """
196 unmodified lines
modified:tests/conftest.py+15 added
74 unmodified lines
75async def app(scope, receive, send):76 assert scope["type"] == "http"77 if scope["path"].startswith("/slow_response"):78 await slow_response(scope, receive, send)79 elif scope["path"].startswith("/slow_stream_response"):80 await slow_stream_response(scope, receive, send)81 elif scope["path"].startswith("/status"):82 await status_code(scope, receive, send)83 elif scope["path"].startswith("/echo_body"):84 await echo_body(scope, receive, send)
27 unmodified lines
112 )113 await send({"type": "http.response.body", "body": b"Hello, world!"})114115116async def slow_stream_response(scope, receive, send):117 await send(118 {119 "type": "http.response.start",120 "status": 200,121 "headers": [[b"content-type", b"text/plain"]],122 }123 )124125 await sleep(1)126 await send({"type": "http.response.body", "body": b"", "more_body": False})127128129async def status_code(scope, receive, send):130 status_code = int(scope["path"].replace("/status/", ""))131 await send(132 {
176 unmodified lines
modified:tests/test_exceptions.py+8 added-1 removed
22 unmodified lines
23 if not_mapped: # pragma: nocover24 pytest.fail(f"Unmapped httpcore exceptions: {not_mapped}")252627def test_httpcore_exception_mapping() -> None:27def test_httpcore_exception_mapping(server) -> None:28 """29 HTTPCore exception mapping works as expected.30 """3132 # Make sure we don't just map to `NetworkError`.33 with pytest.raises(httpx.ConnectError):34 httpx.get("http://doesnotexist")3536 # Make sure streaming methods also map exceptions.37 url = server.url.copy_with(path="/slow_stream_response")38 timeout = httpx.Timeout(None, read=0.1)39 with httpx.stream("GET", url, timeout=timeout) as stream:40 with pytest.raises(httpx.ReadTimeout):41 stream.read()4243 # Make sure it also works with custom transports.44 class MockTransport(httpcore.SyncHTTPTransport):45 def request(self, *args: Any, **kwargs: Any) -> Any:46 raise httpcore.ProtocolError()
22 unmodified lines