@@ -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