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

Errors when a type mismatches on to_json are not helpful #21

Open
lordnull opened this issue Apr 17, 2016 · 0 comments
Open

Errors when a type mismatches on to_json are not helpful #21

lordnull opened this issue Apr 17, 2016 · 0 comments

Comments

@lordnull
Copy link
Owner

When rec2json cannot convert a term to a json representation, it throws a exception of type {badarg, field_name(), field_value(), field_types()}. This is correct as far as adhering to erlang error output structure, but ends up writing something like the following to the console:

** exception error: bad argument: 
                    {products,
                        [{product,undefined,undefined,undefined,undefined,
                             undefined,undefined,undefined,undefined,
                             undefined,undefined,undefined,undefined,
                             undefined,undefined}],
                        {specific,
                            [undefined,
                             {list,{specific,[{product,product,[]}]}}]}}
     in function  rec2json:'-to_json/2-fun-1-'/3 (src/rec2json.erl, line 145)
     in call from lists:foldl/3 (lists.erl, line 1262)
     in call from rec2json:to_json/2 (src/rec2json.erl, line 135)

Sure, if you stare at that and the rec2json source code, or you know the rec2json error format above, it's slightly helpful, otherwise it's a mismash of wtf. Often, when this happens, it is not meant to be handled and recovered, but fixed by a developer. This means developer understanding of the error is more important that efficient consumption by a computer. Given that, the format should change:

erlang:error({badarg, {error_details, #{
    field_name => field_name(),
    field_value => field_value(),
    allow_any => boolean(),
    expected_types => [field_type()]}}})

With a completely faked error, the console output becomes:

** exception error: bad argument: 
                    {error_details,#{allow_any => false,
                                     expected_types => [undefined],
                                     field_name => name,
                                     filed_value => value}}

Use of a map because it looks nicer than a prophets, though if pre-18 is required, that can be waved.

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

No branches or pull requests

1 participant