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
Document exceptions (#1138)Tom Christiebefore adoption360b63d
lines 1–77why, 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 * HTTPStatusErrorInclude invalid url exception in docs (#1166)Tom Christiebefore adoption557ad70
line 78why, one step back78* InvalidURLDocument exceptions (#1138)Tom Christiebefore adoption360b63d
lines 79–151why, one step back79* NotRedirectResponse80* CookieConflict81* StreamError82 * StreamConsumed83 * ResponseNotRead84 * RequestNotRead85 * ResponseClosed8687---8889## Exception classes9091::: httpx.HTTPError92 :docstring:9394::: httpx.RequestError95 :docstring:9697::: httpx.TransportError98 :docstring:99100::: httpx.TimeoutException101 :docstring:102103::: httpx.ConnectTimeout104 :docstring:105106::: httpx.ReadTimeout107 :docstring:108109::: httpx.WriteTimeout110 :docstring:111112::: httpx.PoolTimeout113 :docstring:114115::: httpx.NetworkError116 :docstring:117118::: httpx.ConnectError119 :docstring:120121::: httpx.ReadError122 :docstring:123124::: httpx.WriteError125 :docstring:126127::: httpx.CloseError128 :docstring:129130::: httpx.ProtocolError131 :docstring:132133::: httpx.LocalProtocolError134 :docstring:135136::: httpx.RemoteProtocolError137 :docstring:138139::: httpx.ProxyError140 :docstring:141142::: httpx.UnsupportedProtocol143 :docstring:144145::: httpx.DecodingError146 :docstring:147148::: httpx.TooManyRedirects149 :docstring:150151::: httpx.HTTPStatusErrorInclude invalid url exception in docs (#1166)Tom Christiebefore adoption557ad70
lines 152–154why, one step back152 :docstring:153154::: httpx.InvalidURLDocument exceptions (#1138)Tom Christiebefore adoption360b63d
lines 155–176why, one step back155 :docstring:156157::: httpx.NotRedirectResponse158 :docstring:159160::: httpx.CookieConflict161 :docstring:162163::: httpx.StreamError164 :docstring:165166::: httpx.StreamConsumed167 :docstring:168169::: httpx.ResponseNotRead170 :docstring:171172::: httpx.RequestNotRead173 :docstring:174175::: httpx.ResponseClosed176 :docstring: