exceptions.md
This file
Path
docs/exceptions.md
176 lines · 3.6 KiB · Markdown
Last changed by
Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177# Exceptions
## Request and Response exceptions
The most important exception classes in HTTPX are `RequestError` and `HTTPStatusError`.
The `RequestError` class is a superclass that encompasses any exception that occurswhile issuing an HTTP request. These exceptions include a `.request` attribute.
```pythontry: response = httpx.get("https://www.example.com/")except httpx.RequestError as exc: print(f"An error occured while requesting {exc.request.url!r}.")```
The `HTTPStatusError` class is raised by `response.raise_for_status()` on 4xx and 5xx responses.These exceptions include both a `.request` and a `.response` attribute.
```pythonresponse = httpx.get("https://www.example.com/")try: response.raise_for_status()except httpx.HTTPStatusError as exc: print(f"Error response {exc.response.status_code} while requesting {exc.request.url!r}.")```
There is also a base class `HTTPError` that includes both of these categories, and can be usedto catch either failed requests, or 4xx and 5xx responses.
You can either use this base class to catch both categories...
```pythontry: response = httpx.get("https://www.example.com/") response.raise_for_status()except httpx.HTTPError as exc: print(f"Error while requesting {exc.request.url!r}.")```
Or handle each case explicitly...
```pythontry: response = httpx.get("https://www.example.com/") response.raise_for_status()except httpx.RequestError as exc: print(f"An error occured while requesting {exc.request.url!r}.")except httpx.HTTPStatusError as exc: print(f"Error response {exc.response.status_code} while requesting {exc.request.url!r}.")```
---
## The exception hierarchy
* HTTPError * RequestError * TransportError * TimeoutException * ConnectTimeout * ReadTimeout * WriteTimeout * PoolTimeout * NetworkError * ConnectError * ReadError * WriteError * CloseError * ProtocolError * LocalProtocolError * RemoteProtocolError * ProxyError * UnsupportedProtocol * DecodingError * TooManyRedirects * HTTPStatusError* InvalidURL* NotRedirectResponse* CookieConflict* StreamError * StreamConsumed * ResponseNotRead * RequestNotRead * ResponseClosed
---
## Exception classes
::: httpx.HTTPError :docstring:
::: httpx.RequestError :docstring:
::: httpx.TransportError :docstring:
::: httpx.TimeoutException :docstring:
::: httpx.ConnectTimeout :docstring:
::: httpx.ReadTimeout :docstring:
::: httpx.WriteTimeout :docstring:
::: httpx.PoolTimeout :docstring:
::: httpx.NetworkError :docstring:
::: httpx.ConnectError :docstring:
::: httpx.ReadError :docstring:
::: httpx.WriteError :docstring:
::: httpx.CloseError :docstring:
::: httpx.ProtocolError :docstring:
::: httpx.LocalProtocolError :docstring:
::: httpx.RemoteProtocolError :docstring:
::: httpx.ProxyError :docstring:
::: httpx.UnsupportedProtocol :docstring:
::: httpx.DecodingError :docstring:
::: httpx.TooManyRedirects :docstring:
::: httpx.HTTPStatusError :docstring:
::: httpx.InvalidURL :docstring:
::: httpx.NotRedirectResponse :docstring:
::: httpx.CookieConflict :docstring:
::: httpx.StreamError :docstring:
::: httpx.StreamConsumed :docstring:
::: httpx.ResponseNotRead :docstring:
::: httpx.RequestNotRead :docstring:
::: httpx.ResponseClosed :docstring: