Repository: encode/httpx

Issue:
Add equivalent to requests.response `ok` property
Hey there, I'm wondering do we have some equivalent of `requests.response:ok` property in `httpx` library? If no, Is it planning to be added sometime in the future? thx!

Discussion:
tomchristie: We could have a `.is_success` property, but folks *might* expect that to be equivelent to `100 <= response.status_code <= 399`... Or am I being overly cautious there?
florimondmanca: Nice about `.is_error`.

What do we think about `.is_200_ok`?
tomchristie: Nah, that'd just be cleaner as `response.status_code == 200`

Having `response.is_error` is sensible, because it's the same check as `response.raise_for_status(...)`, but without raising an exception.

Patch:
diff --git a/docs/compatibility.md b/docs/compatibility.md
index a95c12c..d652a0a 100644
--- a/docs/compatibility.md
+++ b/docs/compatibility.md
@@ -14,6 +14,7 @@ to the API in our own documentation. The following exceptions apply:
 but also provide lower-cased versions for API compatibility with `requests`.
 * `stream=True`. - Streaming responses provide the `.stream()` and `.raw()` byte iterator interfaces, rather than the `.iter_content()` method and the `.raw` socket interface.
 * `.get`, `.delete`, `.head`, `.options` -  These methods do not support `files`, `data`, or `json` arguments. Use `.request` if you need to need to send data using these http methods.
+* We don't support `response.is_ok` since the naming is ambiguous there, and might incorrectly imply an equivalence to `response.status_code == codes.OK`. Instead we provide an `.is_error` property.
 
 ## Advanced Usage
 
diff --git a/httpx/models.py b/httpx/models.py
index e703e2d..5469efc 100644
--- a/httpx/models.py
+++ b/httpx/models.py
@@ -846,6 +846,10 @@ class Response:
 
         return self._decoder
 
+    @property
+    def is_error(self) -> bool:
+        return StatusCode.is_error(self.status_code)
+
     @property
     def is_redirect(self) -> bool:
         return StatusCode.is_redirect(self.status_code) and "location" in self.headers
@@ -861,11 +865,9 @@ class Response:
 
         if StatusCode.is_client_error(self.status_code):
             message = message.format(self, error_type="Client Error")
+            raise HTTPError(message, response=self)
         elif StatusCode.is_server_error(self.status_code):
             message = message.format(self, error_type="Server Error")
-        else:
-            message = ""
-        if message:
             raise HTTPError(message, response=self)
 
     def json(self, **kwargs: typing.Any) -> typing.Union[dict, list]:
diff --git a/httpx/status_codes.py b/httpx/status_codes.py
index 3be26ca..eda7c56 100644
--- a/httpx/status_codes.py
+++ b/httpx/status_codes.py
@@ -49,6 +49,10 @@ class StatusCode(IntEnum):
             StatusCode.PERMANENT_REDIRECT,
         )
 
+    @classmethod
+    def is_error(cls, value: int) -> bool:
+        return 400 <= value <= 599
+
     @classmethod
     def is_client_error(cls, value: int) -> bool:
         return 400 <= value <= 499


Test patch:
diff --git a/tests/models/test_responses.py b/tests/models/test_responses.py
index 012d2b7..e7be487 100644
--- a/tests/models/test_responses.py
+++ b/tests/models/test_responses.py
@@ -23,6 +23,7 @@ def test_response():
     assert response.reason_phrase == "OK"
     assert response.text == "Hello, world!"
     assert response.elapsed == datetime.timedelta(0)
+    assert not response.is_error
 
 
 def test_response_repr():