-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve json decode error messages #9484
base: master
Are you sure you want to change the base?
Conversation
CT Test Results 2 files 97 suites 1h 8m 39s ⏱️ Results for commit acf88b7. ♻️ This comment has been updated with latest results. To speed up review, make sure that you have read Contributing to Erlang/OTP and that all checks pass. See the TESTING and DEVELOPMENT HowTo guides for details about how to run test locally. Artifacts// Erlang/OTP Github Action Bot |
@@ -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])}]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the position a character? or a byte?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is a character in unicode?
I wrote position for a reason :-)
@michalmuskala
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
erlang:char/0
is a character in Erlang :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a byte offset
Test case? |
How do I write one, can point me to any examples? |
otp/lib/stdlib/test/maps_SUITE.erl Lines 943 to 1065 in d24c732
|
When printing errors include the position that the error occured at if included in the error.
dcb1a9a
to
acf88b7
Compare
format_json_error(_F, _As, #{position := Position}) -> | ||
[{general, io_lib:format("Occured at position ~w", [Position])}]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Occurred" is misspelled.
Instead of simply fixing the misspelling, I suggest that you generate a clearer message:
format_json_error(_F, _As, #{position := Position}) -> | |
[{general, io_lib:format("Occured at position ~w", [Position])}]; | |
format_json_error({invalid_byte, Byte}, #{position := Position}) -> | |
S = io_lib:format("invalid byte ~w at position ~w", [Byte, Position]), | |
[{general, S}]; |
(You will have to update the call to format_json_error
, too.)
This will result in some redundancy, but overall I think it's clearer:
1> json:decode(~'["valid string", not_valid]').
** exception error: {invalid_byte,111}
in function json:invalid_byte/2 (json.erl, line 541)
*** invalid byte 111 at position 18
in call from json:decode/1 (json.erl, line 879)
When printing errors include the position that the error occured at if included in the error.