Skip to content

Commit

Permalink
Don't call CatchClrExceptions for JavaScriptExceptions (#1502)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Mar 18, 2023
1 parent 98c7f0b commit 6a838cb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
25 changes: 25 additions & 0 deletions Jint.Tests/Runtime/InteropTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2071,6 +2071,31 @@ public void ShouldCatchAllClrExceptions()
Assert.Equal(engine.Invoke("throwException2").AsString(), exceptionMessage);
}

[Fact]
public void ShouldNotCatchClrFromApply()
{
var engine = new Engine(options =>
{
options.CatchClrExceptions(e =>
{
Assert.Fail("was called");
return true;
});
});

engine.Execute(@"
function throwError() {
throw new Error();
}
// doesn't cause ExceptionDelegateHandler call
try { throwError(); } catch {}
// does cause ExceptionDelegateHandler call
try { throwError.apply(); } catch {}
");
}

private class MemberExceptionTest
{
public MemberExceptionTest(bool throwOnCreate)
Expand Down
8 changes: 4 additions & 4 deletions Jint/Runtime/Interop/ClrFunctionInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ protected internal override JsValue Call(JsValue thisObject, JsValue[] arguments
{
return _func(thisObject, arguments);
}
catch (Exception exc)
catch (Exception e) when (e is not JavaScriptException)
{
if (_engine.Options.Interop.ExceptionHandler(exc))
if (_engine.Options.Interop.ExceptionHandler(e))
{
ExceptionHelper.ThrowJavaScriptException(_realm.Intrinsics.Error, exc.Message);
ExceptionHelper.ThrowJavaScriptException(_realm.Intrinsics.Error, e.Message);
}
else
{
ExceptionDispatchInfo.Capture(exc).Throw();
ExceptionDispatchInfo.Capture(e).Throw();
}

return Undefined;
Expand Down

0 comments on commit 6a838cb

Please sign in to comment.