Browse Source

Merge 69f91528b1 into 1d434dec47

pull/13241/merge
Password 4 days ago
committed by GitHub
parent
commit
6f1a7c6857
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 18
      fastapi/applications.py

18
fastapi/applications.py

@ -4547,9 +4547,17 @@ class FastAPI(Starlette):
"""
),
],
*extra_exc_classes_or_status_codes: Annotated[
Union[int, Type[Exception]],
Doc(
"""
Extra Exception classes or status codes this would handle.
"""
),
],
) -> Callable[[DecoratedCallable], DecoratedCallable]:
"""
Add an exception handler to the app.
Add one or more exception handler to the app.
Read more about it in the
[FastAPI docs for Handling Errors](https://fastapi.tiangolo.com/tutorial/handling-errors/).
@ -4565,11 +4573,13 @@ class FastAPI(Starlette):
def __init__(self, name: str):
self.name = name
class AnotherUnicornException(Exception)
def __init__(self, name: str):
self.name = name
app = FastAPI()
@app.exception_handler(UnicornException)
@app.exception_handler(UnicornException, AnotherUnicornException)
async def unicorn_exception_handler(request: Request, exc: UnicornException):
return JSONResponse(
status_code=418,
@ -4580,6 +4590,8 @@ class FastAPI(Starlette):
def decorator(func: DecoratedCallable) -> DecoratedCallable:
self.add_exception_handler(exc_class_or_status_code, func)
for extra_exc_class_or_status_code in extra_exc_classes_or_status_codes:
self.add_exception_handler(extra_exc_class_or_status_code, func)
return func
return decorator

Loading…
Cancel
Save