httpx/docs/exceptions.md
Tom Christie 557ad70242
Include invalid url exception in docs (#1166)
* Advanced transport docs

* Include InvalidURL in exception docs

* Update docs/exceptions.md

Co-authored-by: Florimond Manca <florimond.manca@gmail.com>

Co-authored-by: Florimond Manca <florimond.manca@gmail.com>
2020-08-11 17:14:12 +01:00

3.7 KiB

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 occurs while issuing an HTTP request. These exceptions include a .request attribute.

try:
    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.

response = 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 used to catch either failed requests, or 4xx and 5xx responses.

You can either use this base class to catch both categories...

try:
    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...

try:
    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
      • RequestBodyUnavailable
    • 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.RequestBodyUnavailable :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: