Skip to content
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

Pretty errors when deserializing objects #206

Open
eblis opened this issue Apr 8, 2020 · 4 comments
Open

Pretty errors when deserializing objects #206

eblis opened this issue Apr 8, 2020 · 4 comments

Comments

@eblis
Copy link

eblis commented Apr 8, 2020

It would be nice if a "pretty" exception was raised when a particular json cannot be mapped to a @dataclass-json object, instead of raising KeyError exceptions.

Right now if the json string cannot be mapped a KeyError exception is raised, saying that a particular field isn't available, but this field might be from a child of a child of the root object and it might be hard to determine which field it's taking about just by the name

@lidatong
Copy link
Owner

yep, this would be useful -- i think this is a good opportunity to take a stab at an initial error hierarchy (deserialization, serialization, etc.), as KeyError is just one potential overly-general error, setting the base to a feature branch for now rather than master

@Mtihc
Copy link

Mtihc commented May 22, 2020

I'm not sure how this library works internally. But I'd just like to add that, not only from_json, but also schema().loads raises a KeyError.

from typing import Optional
from dataclasses import dataclass
from dataclasses_json import DataClassJsonMixin

@dataclass
class MyModel(DataClassJsonMixin):
    required_value: str
    optional_value: Optional[str] = None

MyModel.from_json('{}') # KeyError
MyModel.schema().loads('{}') # KeyError

I would expect that MyModel.schema().loads('{}') raises:

marshmallow.exceptions.ValidationError: {'required_value': ['Missing data for required field.']}

I wanted to mention this because I see you speaking of error hierarchies. My first thought was: "what hierarchy? I just want the marshmallow.exceptions.ValidationError!"

@tuukkamustonen
Copy link

I would expect that MyModel.schema().loads('{}') raises:

marshmallow.exceptions.ValidationError: {'required_value': ['Missing data for required field.']}

This is really something you'd expect the library to do out of box.

@george-zubrienko
Copy link
Collaborator

Target of v1 API #442 - take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants