{"id":"6a8554dbe71908b3","repo":"tiangolo/fastapi","slug":"errors-exc-errors-body-body-decode-6a8554","errorCode":null,"errorMessage":"{\"errors\": exc.errors(), \"body\": body.decode()}","messagePattern":"\\{\"errors\": exc\\.errors\\(\\), \"body\": body\\.decode\\(\\)\\}","errorType":"http","errorClass":"HTTPException","httpStatus":422,"severity":"error","filePath":"docs_src/custom_request_and_route/tutorial002_py310.py","lineNumber":18,"sourceCode":"from collections.abc import Callable\n\nfrom fastapi import Body, FastAPI, HTTPException, Request, Response\nfrom fastapi.exceptions import RequestValidationError\nfrom fastapi.routing import APIRoute\n\n\nclass ValidationErrorLoggingRoute(APIRoute):\n    def get_route_handler(self) -> Callable:\n        original_route_handler = super().get_route_handler()\n\n        async def custom_route_handler(request: Request) -> Response:\n            try:\n                return await original_route_handler(request)\n            except RequestValidationError as exc:\n                body = await request.body()\n                detail = {\"errors\": exc.errors(), \"body\": body.decode()}\n                raise HTTPException(status_code=422, detail=detail)\n\n        return custom_route_handler\n\n\napp = FastAPI()\napp.router.route_class = ValidationErrorLoggingRoute\n\n\n@app.post(\"/\")\nasync def sum_numbers(numbers: list[int] = Body()):\n    return sum(numbers)\n","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/tiangolo/fastapi/blob/42a41db11f6882807ac3c057b942178d53b97438/docs_src/custom_request_and_route/tutorial002_py310.py#L1-L30","documentation":"Identical to error 12 but in the non-Annotated variant (numbers: list[int] = Body()). The custom ValidationErrorLoggingRoute catches RequestValidationError and returns HTTP 422 with {\"errors\": ..., \"body\": ...}. Same trigger and meaning.","triggerScenarios":"POST / to the py310 variant with a body that is not a valid list[int].","commonSituations":"Malformed JSON, wrong schema, non-integer list elements.","solutions":["POST a JSON list of integers.","Use the detail.errors[] to locate the validation failure.","Pre-validate the payload client-side."],"exampleFix":"# before\nclient.post(\"/\", json=\"1,2,3\")\n# after\nclient.post(\"/\", json=[1,2,3])","handlingStrategy":"try-catch","validationCode":"payload = [1, 2, 3]\nassert isinstance(payload, list) and all(isinstance(i, int) for i in payload)","typeGuard":"def is_int_list(body) -> bool:\n    return isinstance(body, list) and all(isinstance(i, int) for i in body)","tryCatchPattern":"resp = client.post(\"/\", json=payload)\nif resp.status_code == 422:\n    detail = resp.json()[\"detail\"]\n    for err in detail[\"errors\"]:\n        print(err[\"loc\"], err[\"type\"])","preventionTips":["Ensure the JSON body is a list of ints.","Set the correct Content-Type header.","Inspect errors[] to fix schema mismatches."],"tags":["fastapi","validation","pydantic","custom-route"],"analyzedSha":"42a41db11f6882807ac3c057b942178d53b97438","analyzedAt":"2026-08-04T19:23:32.007Z","schemaVersion":2}