exceptions.md
This file
Path
docs/exceptions.md
180 lines · 3.7 KiB · Markdown
Last changed by
Include invalid url exception in docs (#1166)Tom Christiebefore adoption557ad70
History of this fileTake it
Document exceptions (#1138)Tom Christiebefore adoption360b63d
lines 1–78why, one step back1# Exceptions23## 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:14 print(f"An error occured while requesting {exc.request.url!r}.")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:48 print(f"An error occured while requesting {exc.request.url!r}.")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 * RequestBodyUnavailable78 * HTTPStatusErrorInclude invalid url exception in docs (#1166)Tom Christiebefore adoption557ad70
line 79why, one step back79* InvalidURLDocument exceptions (#1138)Tom Christiebefore adoption360b63d
lines 80–155why, one step back80* NotRedirectResponse81* CookieConflict82* StreamError83 * StreamConsumed84 * ResponseNotRead85 * RequestNotRead86 * ResponseClosed8788---8990## Exception classes9192::: httpx.HTTPError93 :docstring:9495::: httpx.RequestError96 :docstring:9798::: httpx.TransportError99 :docstring:100101::: httpx.TimeoutException102 :docstring:103104::: httpx.ConnectTimeout105 :docstring:106107::: httpx.ReadTimeout108 :docstring:109110::: httpx.WriteTimeout111 :docstring:112113::: httpx.PoolTimeout114 :docstring:115116::: httpx.NetworkError117 :docstring:118119::: httpx.ConnectError120 :docstring:121122::: httpx.ReadError123 :docstring:124125::: httpx.WriteError126 :docstring:127128::: httpx.CloseError129 :docstring:130131::: httpx.ProtocolError132 :docstring:133134::: httpx.LocalProtocolError135 :docstring:136137::: httpx.RemoteProtocolError138 :docstring:139140::: httpx.ProxyError141 :docstring:142143::: httpx.UnsupportedProtocol144 :docstring:145146::: httpx.DecodingError147 :docstring:148149::: httpx.TooManyRedirects150 :docstring:151152::: httpx.RequestBodyUnavailable153 :docstring:154155::: httpx.HTTPStatusErrorInclude invalid url exception in docs (#1166)Tom Christiebefore adoption557ad70
lines 156–158why, one step back156 :docstring:157158::: httpx.InvalidURLDocument exceptions (#1138)Tom Christiebefore adoption360b63d
lines 159–180why, one step back159 :docstring:160161::: httpx.NotRedirectResponse162 :docstring:163164::: httpx.CookieConflict165 :docstring:166167::: httpx.StreamError168 :docstring:169170::: httpx.StreamConsumed171 :docstring:172173::: httpx.ResponseNotRead174 :docstring:175176::: httpx.RequestNotRead177 :docstring:178179::: httpx.ResponseClosed180 :docstring: