Debian Codegraph @

docs/examples/plugins/problem_details/convert_http_exceptions.py

raw
1 from dataclasses import dataclass
2
3 from litestar import Litestar, post
4 from litestar.exceptions.http_exceptions import NotFoundException
5 from litestar.plugins.problem_details import ProblemDetailsConfig, ProblemDetailsPlugin
6
7
8 @dataclass
9 class PurchaseItem:
10 item_id: int
11 quantity: int
12
13
14 @post("/purchase")
15 async def purchase(data: PurchaseItem) -> None:
16 # Logic to check if the user has enough credit to buy the item.
17 # We assume the user does not have enough credit.
18
19 raise NotFoundException(detail="No item with the given ID was found", extra={"item_id": data.item_id})
20
21
22 problem_details_plugin = ProblemDetailsPlugin(ProblemDetailsConfig(enable_for_all_http_exceptions=True))
23 app = Litestar(route_handlers=[purchase], plugins=[problem_details_plugin])
24
25 # run: /purchase --header "Content-Type: application/json" --request POST --data '{"item_id": 1234, "quantity": 2}'