Rework docs structure (#2308)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Florimond Manca · committed by GitHub · parent 3aa4410 · Browse files · 3 files·+74 added−62 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+74 added−62 removed
- other:+74 added−62 removed
Provenance
- Commit
3a82176- Tree
dc44c04- Author
Florimond Manca <florimond.manca@protonmail.com>- Committer
GitHub <noreply@github.com>- Parent
3aa4410
3 files changed+74 added−62 removed
modified:docs/exceptions.md+2 added-50 removed
1233455678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535478910114 unmodified linesExpand all# Exceptions
## Request and Response exceptionsThis page lists exceptions that may be raised when using HTTPX.
The most important exception classes in HTTPX are `RequestError` and `HTTPStatusError`.For an overview of how to work with HTTPX exceptions, see [Exceptions (Quickstart)](quickstart.md#exceptions).
The `RequestError` class is a superclass that encompasses any exception that occurswhile issuing an HTTP request. These exceptions include a `.request` attribute.
```pythontry: response = httpx.get("https://www.example.com/")except httpx.RequestError as exc: print(f"An error occurred while requesting {exc.request.url!r}.")```
The `HTTPStatusError` class is raised by `response.raise_for_status()` on responses which are not a 2xx success code.These exceptions include both a `.request` and a `.response` attribute.
```pythonresponse = httpx.get("https://www.example.com/")try: response.raise_for_status()except httpx.HTTPStatusError as exc: print(f"Error response {exc.response.status_code} while requesting {exc.request.url!r}.")```
There is also a base class `HTTPError` that includes both of these categories, and can be usedto catch either failed requests, or 4xx and 5xx responses.
You can either use this base class to catch both categories...
```pythontry: response = httpx.get("https://www.example.com/") response.raise_for_status()except httpx.HTTPError as exc: print(f"Error while requesting {exc.request.url!r}.")```
Or handle each case explicitly...
```pythontry: response = httpx.get("https://www.example.com/") response.raise_for_status()except httpx.RequestError as exc: print(f"An error occurred while requesting {exc.request.url!r}.")except httpx.HTTPStatusError as exc: print(f"Error response {exc.response.status_code} while requesting {exc.request.url!r}.")```
---
## The exception hierarchy
* HTTPError * RequestError114 unmodified linesExpand allmodified:docs/quickstart.md+54 added
477 unmodified lines478 >>> auth = httpx.DigestAuth("my_user", "password123")479 >>> httpx.get("https://example.com", auth=auth)480 <Response [200 OK]>481 ```482+483+## Exceptions484+485+HTTPX will raise exceptions if an error occurs.486+487+The most important exception classes in HTTPX are `RequestError` and `HTTPStatusError`.488+489+The `RequestError` class is a superclass that encompasses any exception that occurs490+while issuing an HTTP request. These exceptions include a `.request` attribute.491+492+```python493+try:494+ response = httpx.get("https://www.example.com/")495+except httpx.RequestError as exc:496+ print(f"An error occurred while requesting {exc.request.url!r}.")497+```498+499+The `HTTPStatusError` class is raised by `response.raise_for_status()` on responses which are not a 2xx success code.500+These exceptions include both a `.request` and a `.response` attribute.501+502+```python503+response = httpx.get("https://www.example.com/")504+try:505+ response.raise_for_status()506+except httpx.HTTPStatusError as exc:507+ print(f"Error response {exc.response.status_code} while requesting {exc.request.url!r}.")508+```509+510+There is also a base class `HTTPError` that includes both of these categories, and can be used511+to catch either failed requests, or 4xx and 5xx responses.512+513+You can either use this base class to catch both categories...514+515+```python516+try:517+ response = httpx.get("https://www.example.com/")518+ response.raise_for_status()519+except httpx.HTTPError as exc:520+ print(f"Error while requesting {exc.request.url!r}.")521+```522+523+Or handle each case explicitly...524+525+```python526+try:527+ response = httpx.get("https://www.example.com/")528+ response.raise_for_status()529+except httpx.RequestError as exc:530+ print(f"An error occurred while requesting {exc.request.url!r}.")531+except httpx.HTTPStatusError as exc:532+ print(f"Error response {exc.response.status_code} while requesting {exc.request.url!r}.")533+```534+535+For a full list of available exceptions, see [Exceptions (API Reference)](exceptions.md).modified:mkdocs.yml+18 added-12 removed
2 unmodified lines345678910111213141514151617181920212223242516171819202122232425262728293031323334359 unmodified lines2 unmodified linessite_url: https://www.python-httpx.org/
theme: name: 'material' features: - navigation.sections
repo_name: encode/httpxrepo_url: https://github.com/encode/httpx/edit_uri: ""
nav: - Introduction: 'index.md' - QuickStart: 'quickstart.md' - Advanced Usage: 'advanced.md' - Async Support: 'async.md' - HTTP/2 Support: 'http2.md' - Environment Variables: 'environment_variables.md' - Requests Compatibility: 'compatibility.md' - Developer Interface: 'api.md' - Exceptions: 'exceptions.md' - Troubleshooting: 'troubleshooting.md' - Third Party Packages: 'third_party_packages.md' - Contributing: 'contributing.md' - Code of Conduct: 'code_of_conduct.md' - Usage: - QuickStart: 'quickstart.md' - Advanced Usage: 'advanced.md' - Guides: - Async Support: 'async.md' - HTTP/2 Support: 'http2.md' - Requests Compatibility: 'compatibility.md' - Troubleshooting: 'troubleshooting.md' - API Reference: - Developer Interface: 'api.md' - Exceptions: 'exceptions.md' - Environment Variables: 'environment_variables.md' - Community: - Third Party Packages: 'third_party_packages.md' - Contributing: 'contributing.md' - Code of Conduct: 'code_of_conduct.md'
markdown_extensions: - admonition - codehilite:9 unmodified lines