Skip to content

Commit 6e66a4a

Browse files
r-Larchclaude
andauthored
Throw the named error type from JSError.Throw*Error(string) (#511)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e428621 commit 6e66a4a

2 files changed

Lines changed: 61 additions & 6 deletions

File tree

‎src/NodeApi/JSError.cs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,22 +290,22 @@ public static void ThrowError(string code, string message)
290290
=> JSValue.GetCurrentRuntime(out napi_env env).ThrowError(env, code, message);
291291

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

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

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

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

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

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

310310
/// <summary>
311311
/// Gets a JS error stack trace that also includes a .NET stack trace,

‎test/NodejsEmbeddingTests.cs‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,61 @@ public void ThrowErrorFromCallback(string message)
288288
});
289289
}
290290

291+
[Theory]
292+
[InlineData("Error", null)]
293+
[InlineData("Error", "ERR_TEST")]
294+
[InlineData("TypeError", null)]
295+
[InlineData("TypeError", "ERR_TEST")]
296+
[InlineData("RangeError", null)]
297+
[InlineData("RangeError", "ERR_TEST")]
298+
[InlineData("SyntaxError", null)]
299+
[InlineData("SyntaxError", "ERR_TEST")]
300+
public void ThrowTypedError(string errorType, string? code)
301+
{
302+
using NodeEmbeddingThreadRuntime nodejs = CreateNodeEmbeddingThreadRuntime();
303+
304+
nodejs.Run(() =>
305+
{
306+
JSValue.Global.SetProperty("throwError", JSValue.CreateFunction("throwError", (_) =>
307+
{
308+
Action<string> throwWithoutCode = errorType switch
309+
{
310+
"Error" => JSError.ThrowError,
311+
"TypeError" => JSError.ThrowTypeError,
312+
"RangeError" => JSError.ThrowRangeError,
313+
"SyntaxError" => JSError.ThrowSyntaxError,
314+
_ => throw new ArgumentOutOfRangeException(nameof(errorType)),
315+
};
316+
Action<string, string> throwWithCode = errorType switch
317+
{
318+
"Error" => JSError.ThrowError,
319+
"TypeError" => JSError.ThrowTypeError,
320+
"RangeError" => JSError.ThrowRangeError,
321+
"SyntaxError" => JSError.ThrowSyntaxError,
322+
_ => throw new ArgumentOutOfRangeException(nameof(errorType)),
323+
};
324+
325+
if (code == null) throwWithoutCode("test");
326+
else throwWithCode(code, "test");
327+
return JSValue.Undefined;
328+
}));
329+
330+
JSValue error = JSValue.RunScript("""
331+
(() => {
332+
try { throwError(); } catch (e) { return e; }
333+
return null;
334+
})()
335+
""");
336+
Assert.True(error.IsObject());
337+
Assert.Equal(errorType, (string)error["name"]);
338+
Assert.True((bool)JSValue.RunScript(
339+
$"(e) => e instanceof {errorType}").Call(JSValue.Undefined, error));
340+
Assert.Equal("test", (string)error["message"]);
341+
if (code == null) Assert.True(error["code"].IsUndefined());
342+
else Assert.Equal(code, (string)error["code"]);
343+
});
344+
}
345+
291346
[Fact]
292347
public async Task WorkerIsMainThread()
293348
{

0 commit comments

Comments
 (0)