sile / jsone

Erlang JSON library
MIT License
291 stars 72 forks source link

Better errors than badarg #58

Open eproxus opened 3 years ago

eproxus commented 3 years ago

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).

sile commented 3 years ago

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.

eproxus commented 3 years ago

@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?

sile commented 3 years ago

Thanks for your investigation. It sound good 👍