Skip to content

Commit

Permalink
✨ Add ResponseValidationError printable details, to show up in serv…
Browse files Browse the repository at this point in the history
…er error logs (#10078)
  • Loading branch information
tiangolo authored Aug 14, 2023
1 parent dafaf6a commit 5e8f7f1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
6 changes: 6 additions & 0 deletions fastapi/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,9 @@ class ResponseValidationError(ValidationException):
def __init__(self, errors: Sequence[Any], *, body: Any = None) -> None:
super().__init__(errors)
self.body = body

def __str__(self) -> str:
message = f"{len(self._errors)} validation errors:\n"
for err in self._errors:
message += f" {err}\n"
return message
18 changes: 12 additions & 6 deletions tests/test_response_model_as_return_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,15 @@ def test_response_model_no_annotation_return_exact_dict():


def test_response_model_no_annotation_return_invalid_dict():
with pytest.raises(ResponseValidationError):
with pytest.raises(ResponseValidationError) as excinfo:
client.get("/response_model-no_annotation-return_invalid_dict")
assert "missing" in str(excinfo.value)


def test_response_model_no_annotation_return_invalid_model():
with pytest.raises(ResponseValidationError):
with pytest.raises(ResponseValidationError) as excinfo:
client.get("/response_model-no_annotation-return_invalid_model")
assert "missing" in str(excinfo.value)


def test_response_model_no_annotation_return_dict_with_extra_data():
Expand Down Expand Up @@ -313,13 +315,15 @@ def test_no_response_model_annotation_return_exact_dict():


def test_no_response_model_annotation_return_invalid_dict():
with pytest.raises(ResponseValidationError):
with pytest.raises(ResponseValidationError) as excinfo:
client.get("/no_response_model-annotation-return_invalid_dict")
assert "missing" in str(excinfo.value)


def test_no_response_model_annotation_return_invalid_model():
with pytest.raises(ResponseValidationError):
with pytest.raises(ResponseValidationError) as excinfo:
client.get("/no_response_model-annotation-return_invalid_model")
assert "missing" in str(excinfo.value)


def test_no_response_model_annotation_return_dict_with_extra_data():
Expand Down Expand Up @@ -395,13 +399,15 @@ def test_response_model_model1_annotation_model2_return_exact_dict():


def test_response_model_model1_annotation_model2_return_invalid_dict():
with pytest.raises(ResponseValidationError):
with pytest.raises(ResponseValidationError) as excinfo:
client.get("/response_model_model1-annotation_model2-return_invalid_dict")
assert "missing" in str(excinfo.value)


def test_response_model_model1_annotation_model2_return_invalid_model():
with pytest.raises(ResponseValidationError):
with pytest.raises(ResponseValidationError) as excinfo:
client.get("/response_model_model1-annotation_model2-return_invalid_model")
assert "missing" in str(excinfo.value)


def test_response_model_model1_annotation_model2_return_dict_with_extra_data():
Expand Down

0 comments on commit 5e8f7f1

Please sign in to comment.