Skip to content
Myr

Make `raise_for_status` chainable (#2776)

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

* merge upstream * lint * Update test_async_client.py * update docs * add example * Update docs/quickstart.md Co-authored-by: Tom Christie <tom@tomchristie.com> * Update CHANGELOG.md Co-authored-by: Tom Christie <tom@tomchristie.com> * Update docs/quickstart.md Co-authored-by: Tom Christie <tom@tomchristie.com> ---------

authored by Trim21 · committed by GitHub · parent 55b8669 · Browse files · 6 files+14 added−6 removed
This commit

State

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

Trailers

  • Co-authored-by: Tom Christie <tom@tomchristie.com>

Diff stats

6 files+14 added−6 removed

  • tests:+2 added−2 removed
  • other:+12 added−4 removed

6 files+14 added−6 removed

modified:docs/api.md+1 added-1 removed
modified:docs/quickstart.md+8 added-1 removed
286 unmodified lines
287httpx._exceptions.HTTPStatusError: 404 Client Error: Not Found for url: https://httpbin.org/status/404288For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404289```290291Any successful response codes will simply return `None` rather than raising an exception.291Any successful response codes will return the `Response` instance rather than raising an exception.292293```pycon294>>> r.raise_for_status()295```296297The method returns the response instance, allowing you to use it inline. For example:298299```pycon300>>> r = httpx.get('...').raise_for_status()301>>> data = httpx.get('...').raise_for_status().json()302```303304## Response Headers305306The response headers are available as a dictionary-like interface.307
235 unmodified lines
modified:httpx/_models.py+2 added-2 removed
709 unmodified lines
710 )711 and "Location" in self.headers712 )713714 def raise_for_status(self) -> None:714 def raise_for_status(self) -> "Response":715 """716 Raise the `HTTPStatusError` if one occurred.717 """718 request = self._request
3 unmodified lines
722 "instance has not been set on this response."723 )724725 if self.is_success:726 return726 return self727728 if self.has_redirect_location:729 message = (730 "{error_type} '{0.status_code} {0.reason_phrase}' for url '{0.url}'\n"
479 unmodified lines
modified:tests/client/test_async_client.py+1 added-1 removed
120 unmodified lines
121 with pytest.raises(httpx.HTTPStatusError) as exc_info:122 response.raise_for_status()123 assert exc_info.value.response == response124 else:125 assert response.raise_for_status() is None # type: ignore125 assert response.raise_for_status() is response126127128@pytest.mark.anyio129async def test_options(server):
244 unmodified lines
modified:tests/client/test_client.py+1 added-1 removed
139 unmodified lines
140 response.raise_for_status()141 assert exc_info.value.response == response142 assert exc_info.value.request.url.path == f"/status/{status_code}"143 else:144 assert response.raise_for_status() is None # type: ignore144 assert response.raise_for_status() is response145146147def test_options(server):148 with httpx.Client() as client:
312 unmodified lines
modified:CHANGELOG.md+1 added
7 unmodified lines
89### Added1011* Add `socket_options` argument to `httpx.HTTPTransport` and `httpx.AsyncHTTPTransport` classes. (#2716)12* The `Response.raise_for_status()` method now returns the response instance. For example: `data = httpx.get('...').raise_for_status().json()`. (#2776)1314### Fixed1516* Return `500` error response instead of exceptions when `raise_app_exceptions=False` is set on `ASGITransport`. (#2669)
1013 unmodified lines