Skip to content
Myr

Raise HTTPStatusError in raise_from_status (#1072)

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

authored by François Voron · committed by GitHub · parent a89d4ad · Browse files · 6 files+23 added−9 removed
This commit

State

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

Diff stats

6 files+23 added−9 removed

  • tests:+2 added−2 removed
  • other:+21 added−7 removed

6 files+23 added−9 removed

modified:docs/quickstart.md+4 added-3 removed
265 unmodified lines
266>>> not_found.status_code267404268>>> not_found.raise_for_status()269Traceback (most recent call last):270 File "/Users/tomchristie/GitHub/encode/httpcore/httpx/models.py", line 776, in raise_for_status271 raise HTTPError(message)272httpx.HTTPError: 404 Not Found270 File "/Users/tomchristie/GitHub/encode/httpcore/httpx/models.py", line 837, in raise_for_status271 raise HTTPStatusError(message, response=self)272httpx._exceptions.HTTPStatusError: 404 Client Error: Not Found for url: https://httpbin.org/status/404273For more information check: https://httpstatuses.com/404274```275276Any successful response codes will simply return `None` rather than raising an exception.277
196 unmodified lines
modified:httpx/__init__.py+2 added
8 unmodified lines
9 ConnectTimeout,10 CookieConflict,11 DecodingError,12 HTTPError,13 HTTPStatusError,14 InvalidURL,15 NetworkError,16 NotRedirectResponse,17 PoolTimeout,
48 unmodified lines
66 "ConnectTimeout",67 "CookieConflict",68 "DecodingError",69 "HTTPError",70 "HTTPStatusError",71 "InvalidURL",72 "NetworkError",73 "NotRedirectResponse",74 "PoolTimeout",
27 unmodified lines
modified:httpx/_exceptions.py+11 added
119 unmodified lines
120 Decoding of the response failed.121 """122123124class HTTPStatusError(HTTPError):125 """126 Response sent an error HTTP status.127 """128129 def __init__(self, *args: typing.Any, response: "Response") -> None:130 super().__init__(*args)131 self._request = response.request132 self.response = response133134135# Redirect exceptions...136137138class RedirectError(HTTPError):
116 unmodified lines
modified:httpx/_models.py+4 added-4 removed
21 unmodified lines
22 TextDecoder,23)24from ._exceptions import (25 CookieConflict,26 HTTPError,26 HTTPStatusError,27 InvalidURL,28 NotRedirectResponse,29 RequestNotRead,30 ResponseClosed,
793 unmodified lines
824 return StatusCode.is_redirect(self.status_code) and "location" in self.headers825826 def raise_for_status(self) -> None:827 """828 Raise the `HTTPError` if one occurred.828 Raise the `HTTPStatusError` if one occurred.829 """830 message = (831 "{0.status_code} {error_type}: {0.reason_phrase} for url: {0.url}\n"832 "For more information check: https://httpstatuses.com/{0.status_code}"833 )834835 if StatusCode.is_client_error(self.status_code):836 message = message.format(self, error_type="Client Error")837 raise HTTPError(message, response=self)837 raise HTTPStatusError(message, response=self)838 elif StatusCode.is_server_error(self.status_code):839 message = message.format(self, error_type="Server Error")840 raise HTTPError(message, response=self)840 raise HTTPStatusError(message, response=self)841842 def json(self, **kwargs: typing.Any) -> typing.Any:843 if self.charset_encoding is None and self.content and len(self.content) > 3:844 encoding = guess_json_utf(self.content)
350 unmodified lines
modified:tests/client/test_async_client.py+1 added-1 removed
96 unmodified lines
97 "GET", server.url.copy_with(path=f"/status/{status_code}")98 )99100 if 400 <= status_code < 600:101 with pytest.raises(httpx.HTTPError) as exc_info:101 with pytest.raises(httpx.HTTPStatusError) as exc_info:102 response.raise_for_status()103 assert exc_info.value.response == response104 else:105 assert response.raise_for_status() is None # type: ignore
52 unmodified lines
modified:tests/client/test_client.py+1 added-1 removed
104 unmodified lines
105 response = client.request(106 "GET", server.url.copy_with(path=f"/status/{status_code}")107 )108 if 400 <= status_code < 600:109 with pytest.raises(httpx.HTTPError) as exc_info:109 with pytest.raises(httpx.HTTPStatusError) as exc_info:110 response.raise_for_status()111 assert exc_info.value.response == response112 assert exc_info.value.request.url.path == f"/status/{status_code}"113 else:
51 unmodified lines