mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-17 19:52:25 +03:00
Handle more edge cases.
Signed-off-by: James Tanner <tanner.jc@gmail.com>
This commit is contained in:
parent
1353bfa236
commit
586c0444aa
|
@ -177,9 +177,7 @@ class ValidationError(APIException):
|
||||||
"""Handle error messages with templates and placeholders."""
|
"""Handle error messages with templates and placeholders."""
|
||||||
try:
|
try:
|
||||||
return detail % params
|
return detail % params
|
||||||
except KeyError:
|
except (KeyError, ValueError, TypeError, IndexError, AttributeError):
|
||||||
return detail
|
|
||||||
except ValueError:
|
|
||||||
return detail
|
return detail
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -214,6 +214,22 @@ class TestValidationErrorWithDjangoStyle(TestCase):
|
||||||
with pytest.raises(ValidationError):
|
with pytest.raises(ValidationError):
|
||||||
raise ValidationError(errors)
|
raise ValidationError(errors)
|
||||||
|
|
||||||
|
def test_validation_error_without_params_digit(self):
|
||||||
|
"""Ensure that substitutable errors can be emitted with a digit placeholder."""
|
||||||
|
|
||||||
|
# mimic the logic in fields.Field.run_validators by saving the exception
|
||||||
|
# detail into a list which will then be the detail for a new ValidationError.
|
||||||
|
# this should not throw a TypeError on the date format placeholders ...
|
||||||
|
errors = []
|
||||||
|
try:
|
||||||
|
raise ValidationError(detail='%d')
|
||||||
|
except ValidationError as exc:
|
||||||
|
errors.extend(exc.detail)
|
||||||
|
|
||||||
|
# ensure it raises the correct exception type as an input to a new ValidationError
|
||||||
|
with pytest.raises(ValidationError):
|
||||||
|
raise ValidationError(errors)
|
||||||
|
|
||||||
def test_validation_error_without_params_date_formatters(self):
|
def test_validation_error_without_params_date_formatters(self):
|
||||||
"""Ensure that substitutable errors can be emitted with invalid template placeholders."""
|
"""Ensure that substitutable errors can be emitted with invalid template placeholders."""
|
||||||
|
|
||||||
|
@ -229,3 +245,43 @@ class TestValidationErrorWithDjangoStyle(TestCase):
|
||||||
# ensure it raises the correct exception type as an input to a new ValidationError
|
# ensure it raises the correct exception type as an input to a new ValidationError
|
||||||
with pytest.raises(ValidationError):
|
with pytest.raises(ValidationError):
|
||||||
raise ValidationError(errors)
|
raise ValidationError(errors)
|
||||||
|
|
||||||
|
def test_validation_error_with_param_that_has_attribute_error(self):
|
||||||
|
"""Ensure that substitutable errors can be emitted with a bad string repr."""
|
||||||
|
|
||||||
|
class FooBar:
|
||||||
|
def __str__(self):
|
||||||
|
raise AttributeError("i was poorly coded")
|
||||||
|
|
||||||
|
# mimic the logic in fields.Field.run_validators by saving the exception
|
||||||
|
# detail into a list which will then be the detail for a new ValidationError.
|
||||||
|
# this should not throw a ValueError on the date format placeholders ...
|
||||||
|
errors = []
|
||||||
|
try:
|
||||||
|
raise ValidationError(detail='%s', params=FooBar())
|
||||||
|
except ValidationError as exc:
|
||||||
|
errors.extend(exc.detail)
|
||||||
|
|
||||||
|
# ensure it raises the correct exception type as an input to a new ValidationError
|
||||||
|
with pytest.raises(ValidationError):
|
||||||
|
raise ValidationError(errors)
|
||||||
|
|
||||||
|
def test_validation_error_with_param_that_has_index_error(self):
|
||||||
|
"""Ensure that substitutable errors can be emitted with a bad string repr."""
|
||||||
|
|
||||||
|
class FooBar:
|
||||||
|
def __str__(self):
|
||||||
|
raise IndexError("i was poorly coded")
|
||||||
|
|
||||||
|
# mimic the logic in fields.Field.run_validators by saving the exception
|
||||||
|
# detail into a list which will then be the detail for a new ValidationError.
|
||||||
|
# this should not throw a ValueError on the date format placeholders ...
|
||||||
|
errors = []
|
||||||
|
try:
|
||||||
|
raise ValidationError(detail='%s', params=FooBar())
|
||||||
|
except ValidationError as exc:
|
||||||
|
errors.extend(exc.detail)
|
||||||
|
|
||||||
|
# ensure it raises the correct exception type as an input to a new ValidationError
|
||||||
|
with pytest.raises(ValidationError):
|
||||||
|
raise ValidationError(errors)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user