-
-
Notifications
You must be signed in to change notification settings - Fork 620
/
exceptions.py
48 lines (40 loc) · 1.71 KB
/
exceptions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# -*- coding: utf-8 -*-
"""Exception classes for marshmallow-related errors."""
from marshmallow.compat import basestring
class MarshmallowError(Exception):
"""Base class for all marshmallow-related errors."""
pass
class ValidationError(MarshmallowError):
"""Raised when validation fails on a field. Validators and custom fields should
raise this exception.
:param message: An error message (string), list of error messages (as strings),
or dict of error messages.
:param list field_names: Field names to store the error on.
If `None`, the error is stored in its default location.
:param list fields: `Field` objects to which the error applies.
"""
def __init__(self, message, field_names=None, fields=None, data=None, **kwargs):
if not isinstance(message, dict) and not isinstance(message, list):
messages = [message]
else:
messages = message
#: String, list of strings, or dictionary of error messages.
#: If a `dict`, the keys will be field names and the values will be error
#: messages.
self.messages = messages
#: List of field objects which failed validation.
self.fields = fields
if isinstance(field_names, basestring):
#: List of field_names which failed validation.
self.field_names = [field_names]
else: # fields is a list or None
self.field_names = field_names or []
# Store nested data
self.data = data
self.kwargs = kwargs
MarshmallowError.__init__(self, message)
class RegistryError(NameError):
"""Raised when an invalid operation is performed on the serializer
class registry.
"""
pass