@@ -24,7 +24,7 @@ A custom handler can then be defined in your application.
2424import fastapi
2525from rfc9457 import error_class_to_type
2626from fastapi_problem.error import Problem
27- from fastapi_problem.handler import ExceptionHandler, add_exception_handler
27+ from fastapi_problem.handler import ExceptionHandler, add_exception_handler, new_exception_handler
2828from starlette.requests import Request
2929
3030from third_party.error import CustomBaseError
@@ -39,12 +39,12 @@ def my_custom_handler(eh: ExceptionHandler, request: Request, exc: CustomBaseErr
3939 )
4040
4141app = fastapi.FastAPI()
42- add_exception_handler(
43- app,
42+ eh = new_exception_handler(
4443 handlers = {
4544 CustomBaseError: my_custom_handler,
4645 },
4746)
47+ add_exception_handler(app, eh)
4848```
4949
5050Any instance of CustomBaseError, or any subclasses, that reach the exception
@@ -63,7 +63,7 @@ previously defined, but rather than passing it to handlers, use the
6363``` python
6464import fastapi
6565from fastapi_problem.error import Problem
66- from fastapi_problem.handler import ExceptionHandler, add_exception_handler
66+ from fastapi_problem.handler import ExceptionHandler, add_exception_handler, new_exception_handler
6767from starlette.exceptions import HTTPException
6868from starlette.requests import Request
6969
@@ -73,10 +73,10 @@ def my_custom_handler(eh: ExceptionHandler, request: Request, exc: HTTPException
7373
7474
7575app = fastapi.FastAPI()
76- add_exception_handler(
77- app,
76+ eh = new_excep, new_exception_handler(
7877 http_exception_handler = my_custom_handler,
7978)
79+ add_exception_handler(app, eh)
8080```
8181
8282### Optional handling
@@ -90,7 +90,7 @@ will be pass to the next defined handler.
9090import fastapi
9191from rfc9457 import error_class_to_type
9292from fastapi_problem.error import Problem
93- from fastapi_problem.handler import ExceptionHandler, add_exception_handler
93+ from fastapi_problem.handler import ExceptionHandler, add_exception_handler, new_exception_handler
9494from starlette.requests import Request
9595
9696def no_response_handler (eh : ExceptionHandler, request : Request, exc : RuntimeError ) -> Problem | None :
@@ -112,13 +112,13 @@ def base_handler(eh: ExceptionHandler, request: Request, exc: Exception) -> Prob
112112 )
113113
114114app = fastapi.FastAPI()
115- add_exception_handler(
116- app,
115+ eh = new_exception_handler(
117116 handlers = {
118117 RuntimeError : no_response_handler,
119118 Exception : base_handler,
120119 },
121120)
121+ add_exception_handler(app, eh)
122122```
123123
124124At the time of writing there was (is?) a
0 commit comments