exception_handler() — fastapi Function Reference
Architecture documentation for the exception_handler() function in applications.py from the fastapi codebase.
Entity Profile
Dependency Diagram
graph TD a6fa15d9_f44d_3cc7_31d5_290623927d05["exception_handler()"] 587454b3_6db6_011f_9fa4_3e4e2e09b72d["FastAPI"] a6fa15d9_f44d_3cc7_31d5_290623927d05 -->|defined in| 587454b3_6db6_011f_9fa4_3e4e2e09b72d bef02968_6cae_fc82_e854_8b053afd9655["__init__()"] bef02968_6cae_fc82_e854_8b053afd9655 -->|calls| a6fa15d9_f44d_3cc7_31d5_290623927d05 style a6fa15d9_f44d_3cc7_31d5_290623927d05 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
fastapi/applications.py lines 4627–4672
def exception_handler(
self,
exc_class_or_status_code: Annotated[
Union[int, type[Exception]],
Doc(
"""
The Exception class this would handle, or a status code.
"""
),
],
) -> Callable[[DecoratedCallable], DecoratedCallable]:
"""
Add an exception handler to the app.
Read more about it in the
[FastAPI docs for Handling Errors](https://fastapi.tiangolo.com/tutorial/handling-errors/).
## Example
```python
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
class UnicornException(Exception):
def __init__(self, name: str):
self.name = name
app = FastAPI()
@app.exception_handler(UnicornException)
async def unicorn_exception_handler(request: Request, exc: UnicornException):
return JSONResponse(
status_code=418,
content={"message": f"Oops! {exc.name} did something. There goes a rainbow..."},
)
```
"""
def decorator(func: DecoratedCallable) -> DecoratedCallable:
self.add_exception_handler(exc_class_or_status_code, func)
return func
return decorator
Domain
Subdomains
Defined In
Called By
Source
Frequently Asked Questions
What does exception_handler() do?
exception_handler() is a function in the fastapi codebase, defined in fastapi/applications.py.
Where is exception_handler() defined?
exception_handler() is defined in fastapi/applications.py at line 4627.
What calls exception_handler()?
exception_handler() is called by 1 function(s): __init__.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free