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

[FEATURE] More detail exception with name of attribute #543

Open
namezys opened this issue Sep 6, 2024 · 0 comments
Open

[FEATURE] More detail exception with name of attribute #543

namezys opened this issue Sep 6, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@namezys
Copy link

namezys commented Sep 6, 2024

Description

If everything is OK (valid JSON that matches schema), everything works well.

However, debugging becomes complicated if something does not match. Sometimes, it's possible to guess what's wrong, but explicit detail would be good.

Here is a simple example that I used to test the package:

class SourceSystem(enum.Enum):
    CVX = "CVXA"


@dataclass_json(letter_case=LetterCase.CAMEL, undefined=Undefined.EXCLUDE)
@dataclass
class Match(DataClassJsonMixin):
    source_sub_system: SourceSystem


@dataclass_json(letter_case=LetterCase.CAMEL, undefined=Undefined.EXCLUDE)
@dataclass
class Match(DataClassJsonMixin):
    source_sub_system: SourceSystem


p = Match.from_dict({ "sourceSubSystem": "CVX"})

Behavior is simple and understandable:

Traceback (most recent call last):
  File "match.py", line 112, in <module>
    p = Match.from_dict(D)
        ^^^^^^^^^^^^^^^^^^
  File "dataclasses_json/api.py", line 70, in from_dict
    return _decode_dataclass(cls, kvs, infer_missing)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "dataclasses_json/core.py", line 233, in _decode_dataclass
    init_kwargs[field.name] = _decode_generic(field_type,
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "dataclasses_json/core.py", line 296, in _decode_generic
    res = type_(value)
          ^^^^^^^^^^^^
  File "enum.py", line 757, in __call__
    return cls.__new__(cls, value)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "enum.py", line 1171, in __new__
    raise ve_exc
ValueError: 'CVX' is not a valid SourceSystem

However, there needs to be more information on what field is parsing during this exception.
It can significantly simplify debugging and bug analysis, especially if JSON is received from an external source and you can't control schema like reverse engineering.

Possible solution

Wrap exception during serializer into some Validate Exception that includes caused exception and field name.

Also, it's possible to use chain exception, but it can be unsupported by bug collection tools.
As a more advanced solution, exceptions can be collected and raised as an exception group.

Alternatives

Try to add a field name to the existing exception instance.

Context

No response

@namezys namezys added the enhancement New feature or request label Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant