Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new exception ExtraItemsError #241

Merged
merged 1 commit into from
Jan 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Platform
Features
~~~~~~~~

- Add new exception ExtraItemsError. Used to pass to the caller a list of extra
field detected in cstruct.

- Add ``min_err`` and ``max_err`` arguments to ``Length``, thus allowing
customization of its error messages.

Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,4 @@ Contributors
- Nando Florestan, 2014/11/27
- Amos Latteier, 2014/11/30
- Jimmy Thrasibule, 2014/12/11
- Dmitry Bogun, 2015/09/10
21 changes: 16 additions & 5 deletions colander/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,18 @@ def __str__(self):
result of an execution of this exception's ``asdict`` method"""
return pprint.pformat(self.asdict())


class ExtraItemsError(Invalid):
"""
Exception used when schema object detect "extra" fields in cstruct during
deserialize.
"""

def __init__(self, node, extras, msg=_('Unrecognized items')):
super(ExtraItemsError, self).__init__(node, msg)
self.extras = extras

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: why plural ?



class All(object):
""" Composite validator which succeeds if none of its
subvalidators raises an :class:`colander.Invalid` exception"""
Expand Down Expand Up @@ -640,11 +652,10 @@ def _impl(self, node, value, callback):

if self.unknown == 'raise':
if value:
raise Invalid(
node,
_('Unrecognized keys in mapping: "${val}"',
mapping={'val':value})
)
raise ExtraItemsError(
node, value,
msg=_('Unrecognized keys in mapping: "${val}"',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Unknown looks more appropriate than Unrecognized

mapping={'val': value}))

elif self.unknown == 'preserve':
result.update(value)
Expand Down
9 changes: 9 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ Exceptions
from a widget as the value which should be redisplayed when an
error is shown.

.. autoclass:: ExtraItemsError

.. attribute:: extras

The ``dict`` with all detected extra field and their values.

Node that contain extra fields can be located by position of
this exception into exception tree hierarchy.

.. autoclass:: UnboundDeferredError


Expand Down