Browse Source

Add `ResponseValidationError` printable details, to show up in server error logs (#10078)

pull/9538/merge
Sebastián Ramírez 2 years ago
committed by GitHub
parent
commit
5e8f7f13d7
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      fastapi/exceptions.py
  2. 18
      tests/test_response_model_as_return_annotation.py

6
fastapi/exceptions.py

@ -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
tests/test_response_model_as_return_annotation.py

@ -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():
@ -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():
@ -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():

Loading…
Cancel
Save