Skip to content
Myr

Document exceptions (#1138)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Document exceptions * Update exceptions.md

authored by Tom Christie · committed by GitHub · parent 876e722 · Browse files · 3 files+186 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+186 added−5 removed

  • other:+186 added−5 removed

3 files+186 added−5 removed

added:docs/exceptions.md+176 added
1# 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 * HTTPStatusError79* 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.RequestBodyUnavailable152 :docstring:153154::: httpx.HTTPStatusError155 :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:
modified:httpx/_exceptions.py+9 added-5 removed
43 unmodified lines
44 """45 Base class for `RequestError` and `HTTPStatusError`.4647 Useful for `try...except` blocks when issuing a request,48 and then calling .raise_for_status().48 and then calling `.raise_for_status()`.4950 For example:5152 ```53 try:54 response = httpx.get("https://www.example.com")55 response.raise_for_status()56 except httpx.HTTPError as exc:57 print(f"HTTP Exception for {exc.request.url} - {exc.message}")58 ```59 """6061 def __init__(self, message: str, *, request: "Request") -> None:62 super().__init__(message)
10 unmodified lines
737475class TransportError(RequestError):76 """75 Base class for all exceptions that are mapped from the httpcore API.77 Base class for all exceptions that occur at the level of the Transport API.7879 All of these exceptions also have an equivelent mapping in `httpcore`.80 """818283# Timeout exceptions...
70 unmodified lines
154155156class ProxyError(TransportError):157 """154 An error occurred while proxying a request.158 An error occurred while establishing a proxy connection.159 """160161162class UnsupportedProtocol(TransportError):
32 unmodified lines
195196197class DecodingError(RequestError):198 """195 Decoding of the response failed.199 Decoding of the response failed, due to a malformed encoding.200 """201202203class TooManyRedirects(RequestError):
13 unmodified lines
217218219class HTTPStatusError(HTTPError):220 """217 Response sent an error HTTP status.221 The response had an error HTTP status of 4xx or 5xx.222223 May be raised when calling `response.raise_for_status()`224 """225
151 unmodified lines
modified:mkdocs.yml+1 added