Skip to content

Commit

Permalink
harmonize JavaScriptException to look more line a .NET exception
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Dec 26, 2020
1 parent 5c956ed commit 7337c4a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 34 deletions.
7 changes: 6 additions & 1 deletion Jint.Tests.Ecma/EcmaTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,12 @@ protected void RunTestCode(string code, bool negative)

//NOTE: The Date tests in test262 assume the local timezone is Pacific Standard Time
var pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var engine = new Engine(cfg => cfg.LocalTimeZone(pacificTimeZone));
var engine = new Engine(cfg => cfg
.LocalTimeZone(pacificTimeZone)
#if DEBUG
.CollectStackTrace()
#endif
);

// loading driver
if (staSource == null)
Expand Down
6 changes: 3 additions & 3 deletions Jint.Tests/Runtime/ErrorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void CanProduceCorrectStackTrace()
Assert.Equal(17, e.Location.Start.Column);
Assert.Equal("custom.js", e.Location.Source);

var stack = e.CallStack;
var stack = e.StackTrace;
EqualIgnoringNewLineDifferences(@" at a (v) custom.js:2:18
at b (7) custom.js:6:9
at main.js:1:9", stack);
Expand Down Expand Up @@ -126,7 +126,7 @@ public void CallStackBuildingShouldSkipResolvingFromEngine()
at recursive (folderInstance.parent) <anonymous>:8:32
at recursive (folderInstance.parent) <anonymous>:8:32
at recursive (folder) <anonymous>:8:32
at <anonymous>:12:17", javaScriptException.CallStack);
at <anonymous>:12:17", javaScriptException.StackTrace);

var expected = new List<string>
{
Expand All @@ -151,7 +151,7 @@ public void StackTraceCollectedOnThreeLevels()

var ex = Assert.Throws<JavaScriptException>(() => engine.Execute(script));

const string expected = @"TypeError: Cannot read property 'yyy' of undefined
const string expected = @"Jint.Runtime.JavaScriptException: Cannot read property 'yyy' of undefined
at a (v) <anonymous>:2:18
at b (7) <anonymous>:6:12
at <anonymous>:9:9";
Expand Down
2 changes: 1 addition & 1 deletion Jint/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ internal Engine Execute(Script script, bool resetState)
var result = list.Execute();
if (result.Type == CompletionType.Throw)
{
var ex = new JavaScriptException(result.GetValueOrDefault()).SetCallstack(this, result.Location, true);
var ex = new JavaScriptException(result.GetValueOrDefault()).SetCallstack(this, result.Location);
throw ex;
}

Expand Down
4 changes: 2 additions & 2 deletions Jint/Runtime/CallStack/JintCallStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public override string ToString()
return string.Join("->", _stack.Select(cse => cse.ToString()).Reverse());
}

internal string BuildCallStackString(Location location, bool root = false)
internal string BuildCallStackString(Location location)
{
static void AppendLocation(
StringBuilder sb,
Expand Down Expand Up @@ -115,7 +115,7 @@ internal string BuildCallStackString(Location location, bool root = false)
using var sb = StringBuilderPool.Rent();

// stack is one frame behind when we start to process it from expression level
// the actual place it happened first
// the actual expression/statement that caused the problem
var index = _stack._size - 1;
var element = index >= 0 ? _stack[index] : (CallStackElement?) null;
var shortDescription = element?.ToString() ?? "";
Expand Down
1 change: 1 addition & 0 deletions Jint/Runtime/CommonProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ internal static class CommonProperties
internal static readonly JsString Enumerable = new JsString("enumerable");
internal static readonly JsString Configurable = new JsString("configurable");
internal static readonly JsString Stack = new JsString("stack");
internal static readonly JsString Message = new JsString("message");
}
}
41 changes: 14 additions & 27 deletions Jint/Runtime/JavaScriptException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,47 +30,43 @@ public JavaScriptException(ErrorConstructor errorConstructor, string message)
}

public JavaScriptException(JsValue error)
: base(GetErrorMessage(error))
{
Error = error;
}

internal JavaScriptException SetCallstack(Engine engine, Location location, bool root = false)
internal JavaScriptException SetCallstack(Engine engine, Location location)
{
Location = location;
CallStack = engine.CallStack.BuildCallStackString(location);
var value = engine.CallStack.BuildCallStackString(location);
_callStack = value;
if (Error.IsObject())
{
Error.AsObject()
.FastAddProperty(CommonProperties.Stack, new JsString(value), false, false, false);
}

return this;
}

private static string GetErrorMessage(JsValue error)
private string? GetErrorMessage()
{
if (error is ObjectInstance oi)
if (Error is ObjectInstance oi)
{
return oi.Get("message", oi).ToString();
return oi.Get(CommonProperties.Message).ToString();
}

return error.ToString();
return null;
}

public JsValue Error { get; }

public override string ToString()
{
var str = TypeConverter.ToString(Error);
var callStack = CallStack;
if (!string.IsNullOrWhiteSpace(callStack))
{
str += Environment.NewLine + callStack;
}
return str;
}
public override string Message => GetErrorMessage() ?? TypeConverter.ToString(Error);

/// <summary>
/// Returns the call stack of the exception. Requires that engine was built using
/// <see cref="Options.CollectStackTrace"/>.
/// </summary>
public string? CallStack
public override string? StackTrace
{
get
{
Expand All @@ -90,15 +86,6 @@ public override string ToString()
? null
: callstack.AsString();
}
set
{
_callStack = value;
if (value != null && Error.IsObject())
{
Error.AsObject()
.FastAddProperty(CommonProperties.Stack, new JsString(value), false, false, false);
}
}
}

public Location Location { get; private set; }
Expand Down

0 comments on commit 7337c4a

Please sign in to comment.