Skip to content

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

@lordnull

Description

@lordnull

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions