Skip to content

Commit

Permalink
Add context from exceptions caught in _deserialize to the ValidationE…
Browse files Browse the repository at this point in the history
…rror
  • Loading branch information
Pradeep Damodara authored and Bachmann1234 committed Jan 22, 2019
1 parent f844808 commit 6b4ecb0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
25 changes: 15 additions & 10 deletions marshmallow_polyfield/polyfield.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import abc
from six import with_metaclass
from six import raise_from, with_metaclass

from marshmallow import Schema, ValidationError
from marshmallow.fields import Field
Expand All @@ -23,19 +23,24 @@ def _deserialize(self, value, attr, parent):
deserializer = deserializer()
if not isinstance(deserializer, (Field, Schema)):
raise TypeError('Invalid deserializer type')
except Exception:
except Exception as err:
class_type = None
if deserializer:
class_type = str(type(deserializer))

raise ValidationError(
"Unable to use schema. Ensure there is a deserialization_schema_selector"
" and then it returns a field or a schema when the function is passed in "
"{value_passed}. This is the class I got. "
"Make sure it is a field or a schema: {class_type}".format(
value_passed=v,
class_type=class_type
)
raise_from(
ValidationError(
"Unable to use schema. Error: {err}\n"
"Ensure there is a deserialization_schema_selector"
" and then it returns a field or a schema when the function is passed in "
"{value_passed}. This is the class I got. "
"Make sure it is a field or a schema: {class_type}".format(
err=err,
value_passed=v,
class_type=class_type
)
),
err
)

# Will raise ValidationError if any problems
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def read(fname):

setup(
name='marshmallow-polyfield',
version=5.3,
version=5.4,
description='An unofficial extension to Marshmallow to allow for polymorphic fields',
long_description=read('README.rst'),
author='Matt Bachmann',
Expand Down

0 comments on commit 6b4ecb0

Please sign in to comment.