Hide navigation sidebar
Hide table of contents sidebar
Skip to content
Back to top
View this page
Toggle table of contents sidebar

# Custom error handling

By default, `Schema.load` will raise a `ValidationError` if passed invalid data.

You can specify a custom error-handling function for a `Schema` by overriding the `handle_error` method. The method receives the `ValidationError` and the original input data to be deserialized.

    import logging
    from marshmallow import Schema, fields

    class AppError(Exception):
        pass

    class UserSchema(Schema):
        email = fields.Email()

        def handle_error(self, exc, data, **kwargs):
            """Log and raise our custom exception when (de)serialization fails."""
            logging.error(exc.messages)
            raise AppError("An error occurred with input: {0}".format(data))

    schema = UserSchema()
    schema.load({"email": "invalid-email"})  # raises AppError