Debian Codegraph @

docs/examples/onboarding/django/custom_exception_handler.py

raw
1 from litestar import Litestar, Request, Response, get
2
3
4 class ItemNotFoundError(Exception):
5 pass
6
7
8 def handle_item_not_found(request: Request, exception: ItemNotFoundError) -> Response[dict[str, str]]:
9 return Response({"detail": "item not found"}, status_code=404)
10
11
12 @get("/")
13 async def index() -> None:
14 raise ItemNotFoundError
15
16
17 app = Litestar(
18 route_handlers=[index],
19 exception_handlers={ItemNotFoundError: handle_item_not_found},
20 )