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
Provenance
- Commit
27b0dbc- Tree
26a0f3d- Author
François Voron <fvoron@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
a89d4ad
6 files changed+23 added−9 removed
modified:docs/quickstart.md+4 added-3 removed
265 unmodified lines266 >>> not_found.status_code267 404268 >>> not_found.raise_for_status()269 Traceback (most recent call last):270- File "/Users/tomchristie/GitHub/encode/httpcore/httpx/models.py", line 776, in raise_for_status271- raise HTTPError(message)272-httpx.HTTPError: 404 Not Found270+ File "/Users/tomchristie/GitHub/encode/httpcore/httpx/models.py", line 837, in raise_for_status271+ raise HTTPStatusError(message, response=self)272+httpx._exceptions.HTTPStatusError: 404 Client Error: Not Found for url: https://httpbin.org/status/404273+For more information check: https://httpstatuses.com/404274 ```275 276 Any successful response codes will simply return `None` rather than raising an exception.277 196 unmodified linesmodified:httpx/__init__.py+2 added
8 unmodified lines9 ConnectTimeout,10 CookieConflict,11 DecodingError,12 HTTPError,13+ HTTPStatusError,14 InvalidURL,15 NetworkError,16 NotRedirectResponse,17 PoolTimeout,48 unmodified lines66 "ConnectTimeout",67 "CookieConflict",68 "DecodingError",69 "HTTPError",70+ "HTTPStatusError",71 "InvalidURL",72 "NetworkError",73 "NotRedirectResponse",74 "PoolTimeout",27 unmodified linesmodified:httpx/_exceptions.py+11 added
119 unmodified lines120 Decoding of the response failed.121 """122 123 124+class HTTPStatusError(HTTPError):125+ """126+ Response sent an error HTTP status.127+ """128+129+ def __init__(self, *args: typing.Any, response: "Response") -> None:130+ super().__init__(*args)131+ self._request = response.request132+ self.response = response133+134+135 # Redirect exceptions...136 137 138 class RedirectError(HTTPError):116 unmodified linesmodified:httpx/_models.py+4 added-4 removed
21 unmodified lines22 TextDecoder,23 )24 from ._exceptions import (25 CookieConflict,26- HTTPError,26+ HTTPStatusError,27 InvalidURL,28 NotRedirectResponse,29 RequestNotRead,30 ResponseClosed,793 unmodified lines824 return StatusCode.is_redirect(self.status_code) and "location" in self.headers825 826 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 )834 835 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)841 842 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 linesmodified:tests/client/test_async_client.py+1 added-1 removed
96 unmodified lines97 "GET", server.url.copy_with(path=f"/status/{status_code}")98 )99 100 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: ignore52 unmodified linesmodified:tests/client/test_client.py+1 added-1 removed
104 unmodified lines105 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