]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Raise HTTPStatusError in raise_from_status (#1072)
authorFrançois Voron <fvoron@gmail.com>
Mon, 20 Jul 2020 12:10:57 +0000 (14:10 +0200)
committerGitHub <noreply@github.com>
Mon, 20 Jul 2020 12:10:57 +0000 (13:10 +0100)
docs/quickstart.md
httpx/__init__.py
httpx/_exceptions.py
httpx/_models.py
tests/client/test_async_client.py
tests/client/test_client.py

index 67915780e01fe60b2f4c7bb59c95f79e75eb0acc..f19421558f3de548a153e876775d6e8fd841e9ad 100644 (file)
@@ -267,9 +267,10 @@ We can raise an exception for any Client or Server error responses (4xx or 5xx s
 404
 >>> not_found.raise_for_status()
 Traceback (most recent call last):
-  File "/Users/tomchristie/GitHub/encode/httpcore/httpx/models.py", line 776, in raise_for_status
-    raise HTTPError(message)
-httpx.HTTPError: 404 Not Found
+  File "/Users/tomchristie/GitHub/encode/httpcore/httpx/models.py", line 837, in raise_for_status
+    raise HTTPStatusError(message, response=self)
+httpx._exceptions.HTTPStatusError: 404 Client Error: Not Found for url: https://httpbin.org/status/404
+For more information check: https://httpstatuses.com/404
 ```
 
 Any successful response codes will simply return `None` rather than raising an exception.
index a45dd549dd58a028fc0f635631955f012766306f..b88c9a84a17c1b5f3af579efbaabde74960385c9 100644 (file)
@@ -10,6 +10,7 @@ from ._exceptions import (
     CookieConflict,
     DecodingError,
     HTTPError,
+    HTTPStatusError,
     InvalidURL,
     NetworkError,
     NotRedirectResponse,
@@ -66,6 +67,7 @@ __all__ = [
     "CookieConflict",
     "DecodingError",
     "HTTPError",
+    "HTTPStatusError",
     "InvalidURL",
     "NetworkError",
     "NotRedirectResponse",
index ae07ec56ad23b62c62aaf5873e272fa840d51f15..f36fafb64a60cb36ee6e3929b725681f9e113191 100644 (file)
@@ -121,6 +121,17 @@ class DecodingError(HTTPError):
     """
 
 
+class HTTPStatusError(HTTPError):
+    """
+    Response sent an error HTTP status.
+    """
+
+    def __init__(self, *args: typing.Any, response: "Response") -> None:
+        super().__init__(*args)
+        self._request = response.request
+        self.response = response
+
+
 # Redirect exceptions...
 
 
index 1ab162cd65d82f02db059bd5305761f4e48bbfeb..892a959d65bdbbd16383b1b715eca09ac0df918a 100644 (file)
@@ -23,7 +23,7 @@ from ._decoders import (
 )
 from ._exceptions import (
     CookieConflict,
-    HTTPError,
+    HTTPStatusError,
     InvalidURL,
     NotRedirectResponse,
     RequestNotRead,
@@ -825,7 +825,7 @@ class Response:
 
     def raise_for_status(self) -> None:
         """
-        Raise the `HTTPError` if one occurred.
+        Raise the `HTTPStatusError` if one occurred.
         """
         message = (
             "{0.status_code} {error_type}: {0.reason_phrase} for url: {0.url}\n"
@@ -834,10 +834,10 @@ class Response:
 
         if StatusCode.is_client_error(self.status_code):
             message = message.format(self, error_type="Client Error")
-            raise HTTPError(message, response=self)
+            raise HTTPStatusError(message, response=self)
         elif StatusCode.is_server_error(self.status_code):
             message = message.format(self, error_type="Server Error")
-            raise HTTPError(message, response=self)
+            raise HTTPStatusError(message, response=self)
 
     def json(self, **kwargs: typing.Any) -> typing.Any:
         if self.charset_encoding is None and self.content and len(self.content) > 3:
index 7cb537a0f385a281812bd093a081d3749de616cb..aabee25049b4e2f8c1beb9898fb2b10068689d67 100644 (file)
@@ -98,7 +98,7 @@ async def test_raise_for_status(server):
             )
 
             if 400 <= status_code < 600:
-                with pytest.raises(httpx.HTTPError) as exc_info:
+                with pytest.raises(httpx.HTTPStatusError) as exc_info:
                     response.raise_for_status()
                 assert exc_info.value.response == response
             else:
index 6157aa53d7c0c71ca72e1a761bec546e0d462ab5..40a590475126050d5640499aad3709801c9586a5 100644 (file)
@@ -106,7 +106,7 @@ def test_raise_for_status(server):
                 "GET", server.url.copy_with(path=f"/status/{status_code}")
             )
             if 400 <= status_code < 600:
-                with pytest.raises(httpx.HTTPError) as exc_info:
+                with pytest.raises(httpx.HTTPStatusError) as exc_info:
                     response.raise_for_status()
                 assert exc_info.value.response == response
                 assert exc_info.value.request.url.path == f"/status/{status_code}"