From 36d4f3b3bd51fe8c38ca4ab5c4f5623dd27e8dfc Mon Sep 17 00:00:00 2001 From: mr-mapache Date: Tue, 21 Jan 2025 11:51:18 +0000 Subject: [PATCH 1/3] Add support for handling several exceptions with the same handler --- fastapi/applications.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/fastapi/applications.py b/fastapi/applications.py index 6d427cdc2..c1dfd51c7 100644 --- a/fastapi/applications.py +++ b/fastapi/applications.py @@ -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 From 04a8195dbfc5d3ad280404bd9375092518dbab8c Mon Sep 17 00:00:00 2001 From: mr-mapache Date: Tue, 21 Jan 2025 12:03:47 +0000 Subject: [PATCH 2/3] Add extra arg to avoid breaking backward compability --- fastapi/applications.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/fastapi/applications.py b/fastapi/applications.py index c1dfd51c7..1a4a65660 100644 --- a/fastapi/applications.py +++ b/fastapi/applications.py @@ -4539,7 +4539,7 @@ class FastAPI(Starlette): def exception_handler( self, - *exc_classes_or_status_codes: Annotated[ + exc_class_or_status_code: Annotated[ Union[int, Type[Exception]], Doc( """ @@ -4547,9 +4547,18 @@ 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/). @@ -4579,9 +4588,11 @@ class FastAPI(Starlette): ) ``` """ - 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 + 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 From 30d050483550cb44d8a41126af8339614d51c98b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 21 Jan 2025 12:13:41 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20for?= =?UTF-8?q?mat=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi/applications.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/fastapi/applications.py b/fastapi/applications.py index 1a4a65660..709cf5bb9 100644 --- a/fastapi/applications.py +++ b/fastapi/applications.py @@ -4547,7 +4547,6 @@ class FastAPI(Starlette): """ ), ], - *extra_exc_classes_or_status_codes: Annotated[ Union[int, Type[Exception]], Doc( @@ -4588,11 +4587,11 @@ 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