forked from sebastienros/jint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better error from scripts. (sebastienros#408)
- Will track the identifier that cause an undefined / null access - Will report the call stack of the error to the user
- Loading branch information
1 parent
340f0f4
commit caef6f2
Showing
9 changed files
with
178 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System; | ||
using Jint.Parser; | ||
using Jint.Runtime; | ||
using Xunit; | ||
|
||
namespace Jint.Tests.Runtime | ||
{ | ||
public class ErrorTests | ||
{ | ||
[Fact] | ||
public void CanReturnCorrectErrorMessageAndLocation1() | ||
{ | ||
var script = @" | ||
var a = {}; | ||
var b = a.user.name; | ||
"; | ||
|
||
var engine = new Engine(); | ||
var e = Assert.Throws<JavaScriptException>(() => engine.Execute(script)); | ||
Assert.Equal("user is undefined", e.Message); | ||
Assert.Equal(4, e.Location.Start.Line); | ||
Assert.Equal(8, e.Location.Start.Column); | ||
} | ||
|
||
[Fact] | ||
public void CanReturnCorrectErrorMessageAndLocation2() | ||
{ | ||
var script = @" | ||
test(); | ||
"; | ||
|
||
var engine = new Engine(); | ||
var e = Assert.Throws<JavaScriptException>(() => engine.Execute(script)); | ||
Assert.Equal("test is not defined", e.Message); | ||
Assert.Equal(2, e.Location.Start.Line); | ||
Assert.Equal(1, e.Location.Start.Column); | ||
} | ||
|
||
[Fact] | ||
public void CanProduceCorrectStackTrace() | ||
{ | ||
var engine = new Engine(options => options.LimitRecursion(1000)); | ||
|
||
engine.Execute(@"var a = function(v) { | ||
return v.xxx.yyy; | ||
} | ||
var b = function(v) { | ||
return a(v); | ||
}", new ParserOptions | ||
{ | ||
Source = "custom.js" | ||
}); | ||
|
||
var e = Assert.Throws<JavaScriptException>(() => engine.Execute("var x = b(7);", new ParserOptions { Source = "main.js"})); | ||
Assert.Equal("xxx is undefined", e.Message); | ||
Assert.Equal(2, e.Location.Start.Line); | ||
Assert.Equal(8, e.Location.Start.Column); | ||
Assert.Equal("custom.js", e.Location.Source); | ||
|
||
var stack = e.CallStack; | ||
Assert.Equal(@" at a(v) @ custom.js 8:6 | ||
at b(7) @ main.js 8:1 | ||
".Replace("\r\n", "\n"), stack.Replace("\r\n", "\n")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters