jsone icon indicating copy to clipboard operation
jsone copied to clipboard

Better errors than badarg

Open eproxus opened this issue 3 years ago • 3 comments

Is there any plan to add better errors than just badarg? It's problematic when trying to encode/decode inside more complex code:

    try
        complex_code(Binary) % Code that calls jsone:decode somewhere
    catch
        error:badarg:ST ->
            case ST of
                [{jsone_decode,_,_,_}|_] ->
                        ?LOG_WARNING("Bad JSON: ~p", [Binary]);
                _ ->
                    erlang:raise(error, badarg, ST)
            end
    end.

It's impossible to know where the badarg error comes from without checking the stack trace.

I apologize if this have been considered already, since there might be a lot of code changes / possible overhead (e.g. if try-catch is used).

eproxus avatar Jun 03 '21 12:06 eproxus

Sorry for the late reply. Currently, there is no plan to improve the error handling code. However, I think that it's great to make it better if it's possible to do that without performance overhead. Unfortunately, I have no time to try that. But feel free to put a PR if you're interested in it.

sile avatar Jun 13 '21 09:06 sile

@sile I'd be happy to create a PR. After some initial research it seems that replacing the exception defined here should do the trick: https://github.com/sile/jsone/blob/master/src/jsone_decode.erl#L42

Can you confirm?

eproxus avatar Jun 14 '21 09:06 eproxus

Thanks for your investigation. It sound good 👍

sile avatar Jun 14 '21 22:06 sile