|
|
@ -4539,7 +4539,7 @@ class FastAPI(Starlette): |
|
|
|
|
|
|
|
def exception_handler( |
|
|
|
self, |
|
|
|
exc_class_or_status_code: Annotated[ |
|
|
|
*exc_classes_or_status_codes: Annotated[ |
|
|
|
Union[int, Type[Exception]], |
|
|
|
Doc( |
|
|
|
""" |
|
|
@ -4565,11 +4565,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, |
|
|
@ -4577,9 +4579,9 @@ class FastAPI(Starlette): |
|
|
|
) |
|
|
|
``` |
|
|
|
""" |
|
|
|
|
|
|
|
def decorator(func: DecoratedCallable) -> DecoratedCallable: |
|
|
|
self.add_exception_handler(exc_class_or_status_code, func) |
|
|
|
return func |
|
|
|
for exc_class_or_status_code in exc_classes_or_status_codes: |
|
|
|
def decorator(func: DecoratedCallable) -> DecoratedCallable: |
|
|
|
self.add_exception_handler(exc_class_or_status_code, func) |
|
|
|
return func |
|
|
|
|
|
|
|
return decorator |
|
|
|