wyfo / apischema

JSON (de)serialization, GraphQL and JSON schema generation using Python typing.

Home Page:https://wyfo.github.io/apischema/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add location to serialization errors

wyfo opened this issue · comments

See #258 for the initial discussion.

In some cases, e.g. SQLalchemy, serialized objects cannot be easily statically typed checked. Serialization type checking errors should then be enough informative to quickly spot the invalid object.

However, normal use case remains unchecked serialization (for now). Error location tracing must thus have minimal impact on the implementation and the performance. Also, collecting all errors is not considered, as its impact would certainly be too much important.

Two possible implementations are considered first:

  • Add try catch around submethod calls, and add the current error path component to the error caught before reraising it.
  • Add an additional argument to every method for the error path component (with a None default value), and try catch only in the type checking methods.

Both of these methods impact the unchecked implementation, so both implementation should be tried and compared, especially in term of performance.

Also, serialization type checking error should have a custom class (why not deriving TypeError) to store the path and found/expected types (and why not the faulty object, as it could be nice to have for debugging, idk), with a custom __str__ method.