Coverage for docs_src / custom_request_and_route / tutorial002_an_py310.py: 100%
21 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
2from typing import Annotated 1abc
4from fastapi import Body, FastAPI, HTTPException, Request, Response 1abc
5from fastapi.exceptions import RequestValidationError 1abc
6from fastapi.routing import APIRoute 1abc
9class ValidationErrorLoggingRoute(APIRoute): 1abc
10 def get_route_handler(self) -> Callable: 1abc
11 original_route_handler = super().get_route_handler() 1abc
13 async def custom_route_handler(request: Request) -> Response: 1abc
14 try: 1gdheif
15 return await original_route_handler(request) 1gdheif
16 except RequestValidationError as exc: 1def
17 body = await request.body() 1def
18 detail = {"errors": exc.errors(), "body": body.decode()} 1def
19 raise HTTPException(status_code=422, detail=detail) 1def
21 return custom_route_handler 1abc
24app = FastAPI() 1abc
25app.router.route_class = ValidationErrorLoggingRoute 1abc
28@app.post("/") 1abc
29async def sum_numbers(numbers: Annotated[list[int], Body()]): 1abc
30 return sum(numbers) 1ghi