def error_handler(cls, func):
"""Decorator that registers an error handler function for the schema.
The function receives the :class:`Schema` instance, a dictionary of errors,
and the serialized object (if serializing data) or data dictionary (if
deserializing data) as arguments.
Example: ::
class UserSchema(Schema):
email = fields.Email()
@UserSchema.error_handler
def handle_errors(schema, errors, obj):
raise ValueError('An error occurred while marshalling {}'.format(obj))
user = User(email='invalid')
UserSchema().dump(user) # => raises ValueError
UserSchema().load({'email': 'bademail'}) # raises ValueError
.. versionadded:: 0.7.0
.. deprecated:: 2.0.0
Set the ``error_handler`` class Meta option instead.
"""
warnings.warn(
'Schema.error_handler is deprecated. Set the error_handler class Meta option '
'instead.', category=DeprecationWarning
)
cls.__error_handler__ = func
return func
评论列表
文章目录