-
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
Conversation
CT Test Results 2 files 97 suites 1h 8m 4s ⏱️ Results for commit 1259bbb. ♻️ 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 |
lib/stdlib/src/erl_stdlib_errors.erl
Outdated
@@ -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
|
dcb1a9a
to
acf88b7
Compare
lib/stdlib/src/erl_stdlib_errors.erl
Outdated
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)
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.
Changed it to include the character as well, might help if it is an ASCII character.
1> json:decode(<<"["valid string", #not_valid">>).
** exception error: {invalid_byte,35}
in function json:invalid_byte/2 (json.erl, line 541)
*** Invalid byte 35 '#' at byte position 17
in call from json:decode/1 (json.erl, line 879)
2>
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 if it is a control character?
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.
Looks ok to me
11> json:decode(<<"[\"valid string\"", 8>>).
** exception error: {invalid_byte,8}
in function json:invalid_byte/2 (json.erl, line 541)
*** Invalid byte 8 '^(' at byte position 15
in call from json:decode/1 (json.erl, line 879)
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.
Some of the control characters looks fishy to me:
1> json:decode(<<"[\"valid string\"", 9>>).
** exception error: unexpected_end
in function json:decode/1 (json.erl, line 889)
2> json:decode(<<"[\"valid string\"", 10>>).
** exception error: unexpected_end
in function json:decode/1 (json.erl, line 889)
3> json:decode(<<"[\"valid string\"", 11>>).
** exception error: {invalid_byte,11}
in function json:invalid_byte/2 (json.erl, line 541)
*** Invalid byte 11 '^+' at byte position 15
in call from json:decode/1 (json.erl, line 879)
4> json:decode(<<"[\"valid string\"", 12>>).
** exception error: {invalid_byte,12}
in function json:invalid_byte/2 (json.erl, line 541)
*** Invalid byte 12 '^,' at byte position 15
in call from json:decode/1 (json.erl, line 879)
5> json:decode(<<"[\"valid string\"", 27>>).
** exception error: {invalid_byte,27}
in function json:invalid_byte/2 (json.erl, line 541)
*** Invalid byte 27 ' at byte position 15
in call from json:decode/1 (json.erl, line 879)
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.
In Jason we print the byte in hex notation, and as a string if it happens to be a printable character, see
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.
Good point and the byte is already included as a decimal integer in the original error reason.
1355d0b
to
ec7697f
Compare
When printing errors include the position that the error occured at if included in the error.
ec7697f
to
1259bbb
Compare
When printing errors include the position that the error occured at if included in the error.