Skip to content

Commit dcb1a9a

Browse files
committed
Improve json decode error messages
When printing errors include the position that the error occured at if included in the error.
1 parent e01e7b7 commit dcb1a9a

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

lib/stdlib/src/erl_stdlib_errors.erl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ format_error(_Reason, [{M,F,As,Info}|_]) ->
4747
format_unicode_error(F, As);
4848
io ->
4949
format_io_error(F, As, Cause);
50+
json ->
51+
format_json_error(F, As, Cause);
5052
_ ->
5153
[]
5254
end,
@@ -633,6 +635,11 @@ check_io_arguments([Type|TypeT], [Arg|ArgT], No) ->
633635
check_io_arguments(TypeT, ArgT, No+1)]
634636
end.
635637

638+
format_json_error(_F, _As, #{position := Position}) ->
639+
[{general, io_lib:format("Occured at position ~w", [Position])}];
640+
format_json_error(_, _, _) ->
641+
[""].
642+
636643
format_ets_error(delete_object, Args, Cause) ->
637644
format_object(Args, Cause);
638645
format_ets_error(give_away, [_Tab,Pid,_Gift]=Args, Cause) ->

lib/stdlib/src/json.erl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,8 @@ invalid_byte(Bin, Skip) ->
541541
error({invalid_byte, Byte}, none, error_info(Skip)).
542542

543543
error_info(Skip) ->
544-
[{error_info, #{cause => #{position => Skip}}}].
544+
[{error_info, #{cause => #{position => Skip},
545+
module => erl_stdlib_errors}}].
545546

546547
%%
547548
%% Format implementation

0 commit comments

Comments
 (0)