Open kmaork opened 4 years ago
I quite like this proposal, thanks! I'm not very enthusiastic about breaking backwards compatibility, though. How much work do you think it would be to make the interface backwards-compatible? Would you be willing/available to give this a shot?
I guess its still a thing, sad to see we didnt get a proper errors handling yet. Wasted my time trying to get a useful error out the current implementation.
I wouldn't mind if the new error-class has different, but it might help avoid complex work on backwards compatiblity.
Problem
In my opinion, the way
SchemaError
s are produced can be improved. There are a few problems with the current implementation:SchemaError
objects do not contain a reference to the data that validation failed on, which could be useful to users catching the exception.SchemaError
s in a context in which aSchemaError
is handled, which causes a long traceback containingwhile handling this exception, another exception has occured: ...
. This stack trace doesn't contain any useful information because we collect theautos
anderrors
from the handledSchemaError
exceptions anyway.SchemaError
's interface is hard to understand at first (mainly because ofautos
anderrors
) for anyone wanting to implement a custom validator that can throw aSchemaError
'.Possible solution
I would like to offer a new implementation of
SchemaError
that will hopefully solve all of the above. Pseudocode:This solves the problems above:
autos
anderrors
anymore, and let python's exception chaining do the job instead.Drawbacks
Some users might have
raise SchemaError(...)
in their code. Such code will break if we change the interface ofSchemaError.__init__
. We could:autos
anderrors
arguments and have theinvalid_data
argument default to None.SchemaError
. That way,SchemaError
could still be raised, and catchingSchemaError
s will catch the new exception type as well.