Skip to content
Myr

Add Response.is_error (#574)

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

authored by Tom Christie · committed by GitHub · parent 9df76cc · Browse files · 4 files+11 added−3 removed
This commit

State

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

Diff stats

4 files+11 added−3 removed

  • tests:+1 added−0 removed
  • other:+10 added−3 removed

4 files+11 added−3 removed

modified:docs/compatibility.md+1 added
modified:httpx/models.py+5 added-3 removed
845 unmodified lines
846847 return self._decoder848849 @property850 def is_error(self) -> bool:851 return StatusCode.is_error(self.status_code)852853 @property854 def is_redirect(self) -> bool:855 return StatusCode.is_redirect(self.status_code) and "location" in self.headers856857 def raise_for_status(self) -> None:
6 unmodified lines
864 )865866 if StatusCode.is_client_error(self.status_code):867 message = message.format(self, error_type="Client Error")868 raise HTTPError(message, response=self)869 elif StatusCode.is_server_error(self.status_code):870 message = message.format(self, error_type="Server Error")866 else:867 message = ""868 if message:871 raise HTTPError(message, response=self)872873 def json(self, **kwargs: typing.Any) -> typing.Union[dict, list]:874 if self.charset_encoding is None and self.content and len(self.content) > 3:
274 unmodified lines
modified:httpx/status_codes.py+4 added
48 unmodified lines
49 StatusCode.PERMANENT_REDIRECT,50 )5152 @classmethod53 def is_error(cls, value: int) -> bool:54 return 400 <= value <= 5995556 @classmethod57 def is_client_error(cls, value: int) -> bool:58 return 400 <= value <= 4995960 @classmethod
79 unmodified lines
modified:tests/models/test_responses.py+1 added
21 unmodified lines
22 assert response.status_code == 20023 assert response.reason_phrase == "OK"24 assert response.text == "Hello, world!"25 assert response.elapsed == datetime.timedelta(0)26 assert not response.is_error272829def test_response_repr():30 response = httpx.Response(200, content=b"Hello, world!")
245 unmodified lines