Skip to content
Myr

exceptions.md

This file

Path

docs/exceptions.md

172 lines · 3.5 KiB · Markdown

Last changed by

Update exceptions.md (#1432)Gerhard van Andelbefore adoption3c89b91

History of this file

Take it

Document exceptions (#1138)Tom Christiebefore adoption360b63d

lines 1–13why, one step back
1# Exceptions
23## Request and Response exceptions45The most important exception classes in HTTPX are `RequestError` and `HTTPStatusError`.67The `RequestError` class is a superclass that encompasses any exception that occurs8while issuing an HTTP request. These exceptions include a `.request` attribute.910```python11try:12 response = httpx.get("https://www.example.com/")13except httpx.RequestError as exc:

Update exceptions.md (#1432)Gerhard van Andelbefore adoption3c89b91

line 14why, one step back
14 print(f"An error occurred while requesting {exc.request.url!r}.")

Document exceptions (#1138)Tom Christiebefore adoption360b63d

lines 15–47why, one step back
15```
1617The `HTTPStatusError` class is raised by `response.raise_for_status()` on 4xx and 5xx responses.18These exceptions include both a `.request` and a `.response` attribute.1920```python21response = httpx.get("https://www.example.com/")22try:23 response.raise_for_status()24except httpx.HTTPStatusError as exc:25 print(f"Error response {exc.response.status_code} while requesting {exc.request.url!r}.")26```2728There is also a base class `HTTPError` that includes both of these categories, and can be used29to catch either failed requests, or 4xx and 5xx responses.3031You can either use this base class to catch both categories...3233```python34try:35 response = httpx.get("https://www.example.com/")36 response.raise_for_status()37except httpx.HTTPError as exc:38 print(f"Error while requesting {exc.request.url!r}.")39```4041Or handle each case explicitly...4243```python44try:45 response = httpx.get("https://www.example.com/")46 response.raise_for_status()47except httpx.RequestError as exc:

Update exceptions.md (#1432)Gerhard van Andelbefore adoption3c89b91

line 48why, one step back
48 print(f"An error occurred while requesting {exc.request.url!r}.")

Document exceptions (#1138)Tom Christiebefore adoption360b63d

lines 49–77why, one step back
49except httpx.HTTPStatusError as exc:
50 print(f"Error response {exc.response.status_code} while requesting {exc.request.url!r}.")51```5253---5455## The exception hierarchy5657* HTTPError58 * RequestError59 * TransportError60 * TimeoutException61 * ConnectTimeout62 * ReadTimeout63 * WriteTimeout64 * PoolTimeout65 * NetworkError66 * ConnectError67 * ReadError68 * WriteError69 * CloseError70 * ProtocolError71 * LocalProtocolError72 * RemoteProtocolError73 * ProxyError74 * UnsupportedProtocol75 * DecodingError76 * TooManyRedirects77 * HTTPStatusError

Include invalid url exception in docs (#1166)Tom Christiebefore adoption557ad70

line 78why, one step back
78* InvalidURL

Document exceptions (#1138)Tom Christiebefore adoption360b63d

lines 79–150why, one step back
79* CookieConflict
80* StreamError81 * StreamConsumed82 * ResponseNotRead83 * RequestNotRead84 * ResponseClosed8586---8788## Exception classes8990::: httpx.HTTPError91 :docstring:9293::: httpx.RequestError94 :docstring:9596::: httpx.TransportError97 :docstring:9899::: httpx.TimeoutException100 :docstring:101102::: httpx.ConnectTimeout103 :docstring:104105::: httpx.ReadTimeout106 :docstring:107108::: httpx.WriteTimeout109 :docstring:110111::: httpx.PoolTimeout112 :docstring:113114::: httpx.NetworkError115 :docstring:116117::: httpx.ConnectError118 :docstring:119120::: httpx.ReadError121 :docstring:122123::: httpx.WriteError124 :docstring:125126::: httpx.CloseError127 :docstring:128129::: httpx.ProtocolError130 :docstring:131132::: httpx.LocalProtocolError133 :docstring:134135::: httpx.RemoteProtocolError136 :docstring:137138::: httpx.ProxyError139 :docstring:140141::: httpx.UnsupportedProtocol142 :docstring:143144::: httpx.DecodingError145 :docstring:146147::: httpx.TooManyRedirects148 :docstring:149150::: httpx.HTTPStatusError

Include invalid url exception in docs (#1166)Tom Christiebefore adoption557ad70

lines 151–153why, one step back
151 :docstring:
152153::: httpx.InvalidURL

Document exceptions (#1138)Tom Christiebefore adoption360b63d

lines 154–172why, one step back
154 :docstring:
155156::: httpx.CookieConflict157 :docstring:158159::: httpx.StreamError160 :docstring:161162::: httpx.StreamConsumed163 :docstring:164165::: httpx.ResponseNotRead166 :docstring:167168::: httpx.RequestNotRead169 :docstring:170171::: httpx.ResponseClosed172 :docstring: