Skip to content
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

Catch block doesn't catch error thrown by JSON.parse #629

Merged
merged 1 commit into from
May 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions Jint.Tests/Runtime/EngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2756,5 +2756,16 @@ public void ShouldSupportDefaultsInFunctionParameters()
result = function.Invoke(3, JsValue.Undefined).ToString();
Assert.Equal("15", result);
}

[Fact]
public void ShouldReportErrorForInvalidJson()
{
var engine = new Engine();
var ex = Assert.Throws<JavaScriptException>(() => engine.Execute("JSON.parse('[01]')"));
Assert.Equal("Unexpected token '1'", ex.Message);

var voidCompletion = engine.Execute("try { JSON.parse('01') } catch (e) {}").GetCompletionValue();
Assert.Equal(JsValue.Undefined, voidCompletion);
}
}
}
9 changes: 4 additions & 5 deletions Jint/Native/Json/JsonParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private Token ScanNumericLiteral()
// decimal number starts with '0' such as '09' is illegal.
if (ch > 0 && IsDecimalDigit(ch))
{
ExceptionHelper.ThrowArgumentException(Messages.UnexpectedToken);
ExceptionHelper.ThrowSyntaxError(_engine, string.Format(Messages.UnexpectedToken, ch));
}
}

Expand Down Expand Up @@ -222,7 +222,7 @@ private Token ScanNumericLiteral()
}
else
{
ExceptionHelper.ThrowArgumentException(Messages.UnexpectedToken);
ExceptionHelper.ThrowSyntaxError(_engine, string.Format(Messages.UnexpectedToken, _source.CharCodeAt(_index)));
}
}

Expand Down Expand Up @@ -838,8 +838,7 @@ public JsValue Parse(string code, ParserOptions options)
JsValue jsv = ParseJsonValue();

Peek();
Tokens type = _lookahead.Type;
object value = _lookahead.Value;

if(_lookahead.Type != Tokens.EOF)
{
ExceptionHelper.ThrowSyntaxError(_engine, $"Unexpected {_lookahead.Type} {_lookahead.Value}");
Expand Down Expand Up @@ -881,7 +880,7 @@ class Token

static class Messages
{
public const string UnexpectedToken = "Unexpected token {0}";
public const string UnexpectedToken = "Unexpected token '{0}'";
public const string UnexpectedNumber = "Unexpected number";
public const string UnexpectedString = "Unexpected string";
public const string UnexpectedEOS = "Unexpected end of input";
Expand Down