Kaynağa Gözat

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

Marko Lahma 6 yıl önce
ebeveyn
işleme
d87e7a66a0

+ 11 - 0
Jint.Tests/Runtime/EngineTests.cs

@@ -2756,5 +2756,16 @@ function output(x) {
             result = function.Invoke(3, JsValue.Undefined).ToString();
             result = function.Invoke(3, JsValue.Undefined).ToString();
             Assert.Equal("15", result);
             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);
+        }
     }
     }
 }
 }

+ 4 - 5
Jint/Native/Json/JsonParser.cs

@@ -183,7 +183,7 @@ namespace Jint.Native.Json
                     // decimal number starts with '0' such as '09' is illegal.
                     // decimal number starts with '0' such as '09' is illegal.
                     if (ch > 0 && IsDecimalDigit(ch))
                     if (ch > 0 && IsDecimalDigit(ch))
                     {
                     {
-                        ExceptionHelper.ThrowArgumentException(Messages.UnexpectedToken);
+                        ExceptionHelper.ThrowSyntaxError(_engine, string.Format(Messages.UnexpectedToken, ch));
                     }
                     }
                 }
                 }
 
 
@@ -222,7 +222,7 @@ namespace Jint.Native.Json
                 }
                 }
                 else
                 else
                 {
                 {
-                    ExceptionHelper.ThrowArgumentException(Messages.UnexpectedToken);
+                    ExceptionHelper.ThrowSyntaxError(_engine, string.Format(Messages.UnexpectedToken, _source.CharCodeAt(_index)));
                 }
                 }
             }
             }
 
 
@@ -838,8 +838,7 @@ namespace Jint.Native.Json
                 JsValue jsv = ParseJsonValue();
                 JsValue jsv = ParseJsonValue();
 
 
                 Peek();
                 Peek();
-                Tokens type = _lookahead.Type;
-                object value = _lookahead.Value;
+
                 if(_lookahead.Type != Tokens.EOF)
                 if(_lookahead.Type != Tokens.EOF)
                 {
                 {
                     ExceptionHelper.ThrowSyntaxError(_engine, $"Unexpected {_lookahead.Type} {_lookahead.Value}");
                     ExceptionHelper.ThrowSyntaxError(_engine, $"Unexpected {_lookahead.Type} {_lookahead.Value}");
@@ -881,7 +880,7 @@ namespace Jint.Native.Json
 
 
         static class Messages
         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 UnexpectedNumber = "Unexpected number";
             public const string UnexpectedString = "Unexpected string";
             public const string UnexpectedString = "Unexpected string";
             public const string UnexpectedEOS = "Unexpected end of input";
             public const string UnexpectedEOS = "Unexpected end of input";