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

Make error handler and accessor methods #284

sloria opened this Issue Sep 22, 2015 · 4 comments


None yet
3 participants

sloria commented Sep 22, 2015

In 2.0.0rc2, error_handler and accessor are class Meta options. I propose making them instance methods instead.


  • error_handler and accessor already behave like instance methods; they take the Schema instance as their first argument
  • Defining a user-defined base Schema class is a common pattern with marshmallow. Inheritance of instance methods is more straightforward than class Meta options.

Proposed API

from marshmallow import Schema, missing

class CustomError(Exception):

class MySchema(Schema):

    def handle_errors(self, error, obj):
        raise CustomError(error.messages)

    def get_value(self, attr, data):
        return data.get(attr, missing)

@sloria sloria added this to the 2.0.0 (final) milestone Sep 22, 2015


This comment has been minimized.


sloria commented Sep 22, 2015

Another proposed change: handle_errors will receive the ValidationError instance rather than the dictionary of errors. This will allow for other information to be stored on the error.

Example use case:

class CustomValidationError(ValidationError):
    def __init__(self, messages, code, **kwargs):
        self.code = code
        ValidationError.__init__(self, messages, **kwargs)

def validate_password(val):
    if len(val) <= 8:
        raise CustomValidationError('Must be > 8 characters', code=422)

from webframework import HTTPError

class MySchema(Schema):

    def handle_errors(self, error, obj):
        raise HTTPError(error.messages, code=error.code)

This comment has been minimized.


philtay commented Sep 23, 2015


sloria added a commit that referenced this issue Sep 23, 2015

Replace error_handler and accessor with handle_errors and get_attribute
See #284

- update docs and changelog
- update Upgrading guide

@sloria sloria referenced this issue Sep 24, 2015


"Type" validation? #6


This comment has been minimized.


sloria commented Sep 24, 2015


Supporting the use case above, with custom ValidationErrors that take additional arguments won't work quite yet; it's tricky to get it right with marshmallow's error bundling (which will reraise ValidationError)--we'll save this for a future release.

@sloria sloria closed this Sep 24, 2015


This comment has been minimized.

MrLokans commented Jul 26, 2018

What is possible to do is to set all of the meta-data you need into ValidationError kwargs field, basically somewhat like this:

class HTTPValidationError(ValidationError):
    def __init__(self, message, code=400, **kwargs):
        ValidationError.__init__(self, message, **kwargs)
        self.kwargs['code'] = code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment