Coverage for docs_src / custom_request_and_route / tutorial002_py310.py: 100%
20 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1from collections.abc import Callable 1abc
3from fastapi import Body, FastAPI, HTTPException, Request, Response 1abc
4from fastapi.exceptions import RequestValidationError 1abc
5from fastapi.routing import APIRoute 1abc
8class ValidationErrorLoggingRoute(APIRoute): 1abc
9 def get_route_handler(self) -> Callable: 1abc
10 original_route_handler = super().get_route_handler() 1abc
12 async def custom_route_handler(request: Request) -> Response: 1abc
13 try: 1gdheif
14 return await original_route_handler(request) 1gdheif
15 except RequestValidationError as exc: 1def
16 body = await request.body() 1def
17 detail = {"errors": exc.errors(), "body": body.decode()} 1def
18 raise HTTPException(status_code=422, detail=detail) 1def
20 return custom_route_handler 1abc
23app = FastAPI() 1abc
24app.router.route_class = ValidationErrorLoggingRoute 1abc
27@app.post("/") 1abc
28async def sum_numbers(numbers: list[int] = Body()): 1abc
29 return sum(numbers) 1ghi