From: Aviram Hassan <41201924+aviramha@users.noreply.github.com> Date: Sat, 13 Jun 2020 12:33:27 +0000 (+0300) Subject: ✨ Add better JSON decode error handling, improve feedback for client after invalid... X-Git-Tag: 0.57.0~30 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b13a4baf32514d7197ca555a563e91ec8422d9d7;p=thirdparty%2Ffastapi%2Ffastapi.git ✨ Add better JSON decode error handling, improve feedback for client after invalid JSON requests (#1354) * Request body error, raise RequestValidationError instead of HTTPException in case JSON decode failure * add missing test case for body general exception --- diff --git a/fastapi/routing.py b/fastapi/routing.py index b4560a8a4c..16eb7ab0b4 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -1,5 +1,6 @@ import asyncio import inspect +import json from typing import Any, Callable, Dict, List, Optional, Sequence, Set, Type, Union from fastapi import params @@ -177,6 +178,8 @@ def get_request_handler( body_bytes = await request.body() if body_bytes: body = await request.json() + except json.JSONDecodeError as e: + raise RequestValidationError([ErrorWrapper(e, ("body", e.pos))], body=e.doc) except Exception as e: raise HTTPException( status_code=400, detail="There was an error parsing the body" diff --git a/tests/test_tutorial/test_body/test_tutorial001.py b/tests/test_tutorial/test_body/test_tutorial001.py index 293981a09d..806e712dc3 100644 --- a/tests/test_tutorial/test_body/test_tutorial001.py +++ b/tests/test_tutorial/test_body/test_tutorial001.py @@ -1,3 +1,5 @@ +from unittest.mock import patch + import pytest from fastapi.testclient import TestClient @@ -176,5 +178,24 @@ def test_post_body(path, body, expected_status, expected_response): def test_post_broken_body(): response = client.post("/items/", data={"name": "Foo", "price": 50.5}) - assert response.status_code == 400, response.text + assert response.status_code == 422, response.text + assert response.json() == { + "detail": [ + { + "ctx": { + "colno": 1, + "doc": "name=Foo&price=50.5", + "lineno": 1, + "msg": "Expecting value", + "pos": 0, + }, + "loc": ["body", 0], + "msg": "Expecting value: line 1 column 1 (char 0)", + "type": "value_error.jsondecode", + } + ] + } + with patch("json.loads", side_effect=Exception): + response = client.post("/items/", json={"test": "test2"}) + assert response.status_code == 400, response.text assert response.json() == {"detail": "There was an error parsing the body"}