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
Provenance
- Commit
360b63d- Tree
1ddf1a1- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
876e722
3 files changed+186 added−5 removed
added:docs/exceptions.md+176 added
1+# Exceptions2+3+## Request and Response exceptions4+5+The most important exception classes in HTTPX are `RequestError` and `HTTPStatusError`.6+7+The `RequestError` class is a superclass that encompasses any exception that occurs8+while issuing an HTTP request. These exceptions include a `.request` attribute.9+10+```python11+try:12+ response = httpx.get("https://www.example.com/")13+except httpx.RequestError as exc:14+ print(f"An error occured while requesting {exc.request.url!r}.")15+```16+17+The `HTTPStatusError` class is raised by `response.raise_for_status()` on 4xx and 5xx responses.18+These exceptions include both a `.request` and a `.response` attribute.19+20+```python21+response = httpx.get("https://www.example.com/")22+try:23+ response.raise_for_status()24+except httpx.HTTPStatusError as exc:25+ print(f"Error response {exc.response.status_code} while requesting {exc.request.url!r}.")26+```27+28+There is also a base class `HTTPError` that includes both of these categories, and can be used29+to catch either failed requests, or 4xx and 5xx responses.30+31+You can either use this base class to catch both categories...32+33+```python34+try:35+ response = httpx.get("https://www.example.com/")36+ response.raise_for_status()37+except httpx.HTTPError as exc:38+ print(f"Error while requesting {exc.request.url!r}.")39+```40+41+Or handle each case explicitly...42+43+```python44+try:45+ response = httpx.get("https://www.example.com/")46+ response.raise_for_status()47+except httpx.RequestError as exc:48+ print(f"An error occured while requesting {exc.request.url!r}.")49+except httpx.HTTPStatusError as exc:50+ print(f"Error response {exc.response.status_code} while requesting {exc.request.url!r}.")51+```52+53+---54+55+## The exception hierarchy56+57+* 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+ * ResponseClosed86+87+---88+89+## Exception classes90+91+::: httpx.HTTPError92+ :docstring:93+94+::: httpx.RequestError95+ :docstring:96+97+::: httpx.TransportError98+ :docstring:99+100+::: httpx.TimeoutException101+ :docstring:102+103+::: httpx.ConnectTimeout104+ :docstring:105+106+::: httpx.ReadTimeout107+ :docstring:108+109+::: httpx.WriteTimeout110+ :docstring:111+112+::: httpx.PoolTimeout113+ :docstring:114+115+::: httpx.NetworkError116+ :docstring:117+118+::: httpx.ConnectError119+ :docstring:120+121+::: httpx.ReadError122+ :docstring:123+124+::: httpx.WriteError125+ :docstring:126+127+::: httpx.CloseError128+ :docstring:129+130+::: httpx.ProtocolError131+ :docstring:132+133+::: httpx.LocalProtocolError134+ :docstring:135+136+::: httpx.RemoteProtocolError137+ :docstring:138+139+::: httpx.ProxyError140+ :docstring:141+142+::: httpx.UnsupportedProtocol143+ :docstring:144+145+::: httpx.DecodingError146+ :docstring:147+148+::: httpx.TooManyRedirects149+ :docstring:150+151+::: httpx.RequestBodyUnavailable152+ :docstring:153+154+::: httpx.HTTPStatusError155+ :docstring:156+157+::: httpx.NotRedirectResponse158+ :docstring:159+160+::: httpx.CookieConflict161+ :docstring:162+163+::: httpx.StreamError164+ :docstring:165+166+::: httpx.StreamConsumed167+ :docstring:168+169+::: httpx.ResponseNotRead170+ :docstring:171+172+::: httpx.RequestNotRead173+ :docstring:174+175+::: httpx.ResponseClosed176+ :docstring:modified:httpx/_exceptions.py+9 added-5 removed
43 unmodified lines44 """45 Base class for `RequestError` and `HTTPStatusError`.46 47 Useful for `try...except` blocks when issuing a request,48- and then calling .raise_for_status().48+ and then calling `.raise_for_status()`.49 50 For example:51 52+ ```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 """60 61 def __init__(self, message: str, *, request: "Request") -> None:62 super().__init__(message)10 unmodified lines73 74 75 class 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.78+79+ All of these exceptions also have an equivelent mapping in `httpcore`.80 """81 82 83 # Timeout exceptions...70 unmodified lines154 155 156 class ProxyError(TransportError):157 """154- An error occurred while proxying a request.158+ An error occurred while establishing a proxy connection.159 """160 161 162 class UnsupportedProtocol(TransportError):32 unmodified lines195 196 197 class DecodingError(RequestError):198 """195- Decoding of the response failed.199+ Decoding of the response failed, due to a malformed encoding.200 """201 202 203 class TooManyRedirects(RequestError):13 unmodified lines217 218 219 class HTTPStatusError(HTTPError):220 """217- Response sent an error HTTP status.221+ The response had an error HTTP status of 4xx or 5xx.222 223 May be raised when calling `response.raise_for_status()`224 """225 151 unmodified linesmodified:mkdocs.yml+1 added
16 unmodified lines17181920212223242510 unmodified lines16 unmodified lines - HTTP/2 Support: 'http2.md' - Environment Variables: 'environment_variables.md' - Requests Compatibility: 'compatibility.md' - Developer Interface: 'api.md' - Exceptions: 'exceptions.md' - Third Party Packages: 'third-party-packages.md' - Contributing: 'contributing.md'
markdown_extensions:10 unmodified lines