Skip to content

Commit d88ec2b

Browse files
committed
GH-22: Document the error handling section
1 parent d022184 commit d88ec2b

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

docs/references/tutorials.md

+19-3
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,27 @@ the [user context](/integration/integration#user-context) will be available for
131131

132132
## Error handling
133133

134-
::: info NOTE
134+
The exceptions that possibly can occur when using the library are reraised as `HTTPException` with the appropriate
135+
status code and a message describing the actual error cause. So they can be handled in a natural way by following the
136+
FastAPI [docs](https://fastapi.tiangolo.com/tutorial/handling-errors/) on handling errors and using the exceptions from
137+
the `fastapi_oauth2.exceptions` module.
135138

136-
This section is under development.
139+
```python
140+
from fastapi_oauth2.exceptions import OAuth2AuthenticationError
137141

138-
:::
142+
@app.exception_handler(OAuth2AuthenticationError)
143+
async def error_handler(request: Request, exc: OAuth2AuthenticationError):
144+
return RedirectResponse(url="/login", status_code=303)
145+
```
146+
147+
The complete list of exceptions is the following.
148+
149+
- `OAuth2Error` - Base exception for all errors raised by the FastAPI OAuth2 library.
150+
- `OAuth2AuthenticationError` - An exception is raised when the authentication fails.
151+
- `OAuth2InvalidRequestError` - An exception is raised when the request is invalid.
152+
153+
The request is considered invalid when one of mandatory parameters such as `state` or `code` is missing or the request
154+
did not succeed.
139155

140156
<style>
141157
.info, .details {

0 commit comments

Comments
 (0)