Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/NodeApi/JSError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,22 +290,22 @@ public static void ThrowError(string code, string message)
=> JSValue.GetCurrentRuntime(out napi_env env).ThrowError(env, code, message);

public static void ThrowTypeError(string message)
=> JSValue.GetCurrentRuntime(out napi_env env).ThrowError(env, code: null, message);
=> JSValue.GetCurrentRuntime(out napi_env env).ThrowTypeError(env, code: null, message);

public static void ThrowTypeError(string code, string message)
=> JSValue.GetCurrentRuntime(out napi_env env).ThrowError(env, code, message);
=> JSValue.GetCurrentRuntime(out napi_env env).ThrowTypeError(env, code, message);

public static void ThrowRangeError(string message)
=> JSValue.GetCurrentRuntime(out napi_env env).ThrowError(env, code: null, message);
=> JSValue.GetCurrentRuntime(out napi_env env).ThrowRangeError(env, code: null, message);

public static void ThrowRangeError(string code, string message)
=> JSValue.GetCurrentRuntime(out napi_env env).ThrowError(env, code, message);
=> JSValue.GetCurrentRuntime(out napi_env env).ThrowRangeError(env, code, message);

public static void ThrowSyntaxError(string message)
=> JSValue.GetCurrentRuntime(out napi_env env).ThrowError(env, code: null, message);
=> JSValue.GetCurrentRuntime(out napi_env env).ThrowSyntaxError(env, code: null, message);

public static void ThrowSyntaxError(string code, string message)
=> JSValue.GetCurrentRuntime(out napi_env env).ThrowError(env, code, message);
=> JSValue.GetCurrentRuntime(out napi_env env).ThrowSyntaxError(env, code, message);

/// <summary>
/// Gets a JS error stack trace that also includes a .NET stack trace,
Expand Down
55 changes: 55 additions & 0 deletions test/NodejsEmbeddingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,61 @@ public void ThrowErrorFromCallback(string message)
});
}

[Theory]
[InlineData("Error", null)]
[InlineData("Error", "ERR_TEST")]
[InlineData("TypeError", null)]
[InlineData("TypeError", "ERR_TEST")]
[InlineData("RangeError", null)]
[InlineData("RangeError", "ERR_TEST")]
[InlineData("SyntaxError", null)]
[InlineData("SyntaxError", "ERR_TEST")]
public void ThrowTypedError(string errorType, string? code)
{
using NodeEmbeddingThreadRuntime nodejs = CreateNodeEmbeddingThreadRuntime();

nodejs.Run(() =>
{
JSValue.Global.SetProperty("throwError", JSValue.CreateFunction("throwError", (_) =>
{
Action<string> throwWithoutCode = errorType switch
{
"Error" => JSError.ThrowError,
"TypeError" => JSError.ThrowTypeError,
"RangeError" => JSError.ThrowRangeError,
"SyntaxError" => JSError.ThrowSyntaxError,
_ => throw new ArgumentOutOfRangeException(nameof(errorType)),
};
Action<string, string> throwWithCode = errorType switch
{
"Error" => JSError.ThrowError,
"TypeError" => JSError.ThrowTypeError,
"RangeError" => JSError.ThrowRangeError,
"SyntaxError" => JSError.ThrowSyntaxError,
_ => throw new ArgumentOutOfRangeException(nameof(errorType)),
};

if (code == null) throwWithoutCode("test");
else throwWithCode(code, "test");
return JSValue.Undefined;
}));

JSValue error = JSValue.RunScript("""
(() => {
try { throwError(); } catch (e) { return e; }
return null;
})()
""");
Assert.True(error.IsObject());
Assert.Equal(errorType, (string)error["name"]);
Assert.True((bool)JSValue.RunScript(
$"(e) => e instanceof {errorType}").Call(JSValue.Undefined, error));
Assert.Equal("test", (string)error["message"]);
if (code == null) Assert.True(error["code"].IsUndefined());
else Assert.Equal(code, (string)error["code"]);
});
}

[Fact]
public async Task WorkerIsMainThread()
{
Expand Down