Commit a679edbb authored by Issack John's avatar Issack John Committed by V8 LUCI CQ

JSON.parse errors made user-friendly part 2

Part of the improve error messages initiative.

Based on a resource of JSON.parse() errors found at
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/JSON_bad_parse

added support for:
- 'Bad control character in string literal'
- 'Bad Unicode escape'

Previously JSON.parse('"a\bz"') would output:
SyntaxError: Unexpected token  in JSON at position 2
Now the output is:
SyntaxError: Bad control character in string literal in
JSON at position 2

Previously JSON.parse("[\"\\t\\u") would output:
SyntaxError: Unexpected end of JSON input
Now the output is:
SyntaxError: Bad Unicode escape in JSON at position 6

Bug: v8:6551
Change-Id: I3ba5450c41b8a388643a15bc58e4e3fc75855d13
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3652254Reviewed-by: 's avatarToon Verwaest <verwaest@chromium.org>
Commit-Queue: Issack John <issackjohn@microsoft.com>
Cr-Commit-Position: refs/heads/main@{#80642}
parent 1da4b373
......@@ -504,6 +504,9 @@ namespace internal {
"%") \
T(JsonParseBadEscapedCharacter, \
"Bad escaped character in JSON at position %") \
T(JsonParseBadControlCharacter, \
"Bad control character in string literal in JSON at position %") \
T(JsonParseBadUnicodeEscape, "Bad Unicode escape in JSON at position %") \
T(JsonParseNoNumberAfterMinusSign, \
"No number after minus sign in JSON at position %") \
T(JsonParseShortString, "\"%\" is not valid JSON") \
......
......@@ -1270,7 +1270,8 @@ JsonString JsonParser<Char>::ScanJsonString(bool needs_internalization) {
base::uc32 value = ScanUnicodeCharacter();
if (value == kInvalidUnicodeCharacter) {
AllowGarbageCollection allow_before_exception;
ReportUnexpectedCharacter(CurrentCharacter());
ReportUnexpectedToken(JsonToken::ILLEGAL,
MessageTemplate::kJsonParseBadUnicodeEscape);
return JsonString();
}
bits |= value;
......@@ -1294,7 +1295,8 @@ JsonString JsonParser<Char>::ScanJsonString(bool needs_internalization) {
DCHECK_LT(*cursor_, 0x20);
AllowGarbageCollection allow_before_exception;
ReportUnexpectedCharacter(*cursor_);
ReportUnexpectedToken(JsonToken::ILLEGAL,
MessageTemplate::kJsonParseBadControlCharacter);
break;
}
......
......@@ -24,7 +24,7 @@ var s = `{"a\u03A9 `;
TryParse(s, "Unterminated string in JSON at position 5");
var s = `{"a\nb":"b"}`;
TryParse(s, "Unexpected token '\n', \"{\"a\nb\":\"b\"}\" is not valid JSON");
TryParse(s, "Bad control character in string literal in JSON at position 3");
var s = `{"a\nb":"b\u03A9"}`;
TryParse(s, "Unexpected token '\n', \"{\"a\nb\":\"b\u03A9\"}\" is not valid JSON");
TryParse(s, "Bad control character in string literal in JSON at position 3");
......@@ -528,6 +528,16 @@ test(function() {
JSON.parse('{"b" : "\\a"}');
}, "Bad escaped character in JSON at position 9", SyntaxError);
// kJsonParseBadControlCharacter
test(function() {
JSON.parse('"a\bz"');
}, "Bad control character in string literal in JSON at position 2", SyntaxError);
// kJsonParseBadUnicodeEscape
test(function() {
JSON.parse("[\"\\t\\u");
}, "Bad Unicode escape in JSON at position 6", SyntaxError);
// kJsonParseNoNumberAfterMinusSign
test(function() {
JSON.parse('-');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment