Skip to content

Commit

Permalink
Call error_handler when calling Schema#validate
Browse files Browse the repository at this point in the history
fix #121
  • Loading branch information
sloria committed Jan 1, 2015
1 parent 08a8cd6 commit 8bb1127
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions marshmallow/schema.py
Expand Up @@ -488,8 +488,6 @@ def load(self, data, many=None):
.. versionadded:: 1.0.0
"""
result, errors = self._do_load(data, many, postprocess=True)
if self._unmarshal.errors and callable(self.__error_handler__):
self.__error_handler__(self._unmarshal.errors, data)
return UnmarshalResult(data=result, errors=errors)

def loads(self, json_data, many=None, *args, **kwargs):
Expand Down Expand Up @@ -598,6 +596,8 @@ def _do_load(self, data, many=None, postprocess=True):
dict_class=self.dict_class
)
errors = self._unmarshal.errors
if errors and callable(self.__error_handler__):
self.__error_handler__(errors, data)
return result, errors

def _update_fields(self, obj=None, many=False):
Expand Down
8 changes: 8 additions & 0 deletions tests/test_schema.py
Expand Up @@ -775,6 +775,14 @@ def handle_errors(serializer, errors, data):
with pytest.raises(CustomError):
MySchema().load({'email': 'invalid'})

def test_validate_with_custom_error_handler(self):
@MySchema.error_handler
def handle_errors(schema, errors, data):
raise CustomError('Something bad happened')

with pytest.raises(CustomError):
MySchema().validate({'email': 'invalid'})

def test_multiple_serializers_with_same_error_handler(self, user):

@MySchema.error_handler
Expand Down

0 comments on commit 8bb1127

Please sign in to comment.