Debian Codegraph @

docs/examples/onboarding/flask/exception_handlers.py

raw
1 from litestar import Litestar, Request, Response, get
2 from litestar.exceptions import HTTPException
3
4
5 def handle_http_exception(request: Request, exception: HTTPException) -> Response[dict[str, str]]:
6 return Response(
7 {"detail": exception.detail},
8 status_code=exception.status_code,
9 )
10
11
12 @get("/", sync_to_thread=False)
13 def index() -> None:
14 raise HTTPException(status_code=400, detail="this did not work")
15
16
17 app = Litestar(
18 route_handlers=[index],
19 exception_handlers={HTTPException: handle_http_exception},
20 )