mirror of https://github.com/tiangolo/fastapi.git
Allow passing a sequence of codes or exceptions to `exception_handler`
This commit is contained in:
parent
4ec5e0a90a
commit
6227bc3611
|
|
@ -4540,10 +4540,10 @@ class FastAPI(Starlette):
|
||||||
def exception_handler(
|
def exception_handler(
|
||||||
self,
|
self,
|
||||||
exc_class_or_status_code: Annotated[
|
exc_class_or_status_code: Annotated[
|
||||||
Union[int, Type[Exception]],
|
Union[int, Type[Exception], Sequence[int], Sequence[Type[Exception]]],
|
||||||
Doc(
|
Doc(
|
||||||
"""
|
"""
|
||||||
The Exception class this would handle, or a status code.
|
The Exception class, a status code or a sequence of them this would handle.
|
||||||
"""
|
"""
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|
@ -4579,6 +4579,10 @@ class FastAPI(Starlette):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def decorator(func: DecoratedCallable) -> DecoratedCallable:
|
def decorator(func: DecoratedCallable) -> DecoratedCallable:
|
||||||
|
if isinstance(exc_class_or_status_code, Sequence):
|
||||||
|
for exc_class_or_status_code_ in exc_class_or_status_code:
|
||||||
|
self.add_exception_handler(exc_class_or_status_code_, func)
|
||||||
|
else:
|
||||||
self.add_exception_handler(exc_class_or_status_code, func)
|
self.add_exception_handler(exc_class_or_status_code, func)
|
||||||
return func
|
return func
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue