Skip to content

Commit

Permalink
Improve json decode error messages
Browse files Browse the repository at this point in the history
When printing errors include the position that the error occured at
if included in the error.
  • Loading branch information
dgud committed Feb 24, 2025
1 parent e01e7b7 commit dcb1a9a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/stdlib/src/erl_stdlib_errors.erl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ format_error(_Reason, [{M,F,As,Info}|_]) ->
format_unicode_error(F, As);
io ->
format_io_error(F, As, Cause);
json ->
format_json_error(F, As, Cause);
_ ->
[]
end,
Expand Down Expand Up @@ -633,6 +635,11 @@ check_io_arguments([Type|TypeT], [Arg|ArgT], No) ->
check_io_arguments(TypeT, ArgT, No+1)]
end.

format_json_error(_F, _As, #{position := Position}) ->
[{general, io_lib:format("Occured at position ~w", [Position])}];
format_json_error(_, _, _) ->
[""].

format_ets_error(delete_object, Args, Cause) ->
format_object(Args, Cause);
format_ets_error(give_away, [_Tab,Pid,_Gift]=Args, Cause) ->
Expand Down
3 changes: 2 additions & 1 deletion lib/stdlib/src/json.erl
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,8 @@ invalid_byte(Bin, Skip) ->
error({invalid_byte, Byte}, none, error_info(Skip)).

error_info(Skip) ->
[{error_info, #{cause => #{position => Skip}}}].
[{error_info, #{cause => #{position => Skip},
module => erl_stdlib_errors}}].

%%
%% Format implementation
Expand Down

0 comments on commit dcb1a9a

Please sign in to comment.