diff --git a/cli/tests/integration/run_tests.rs b/cli/tests/integration/run_tests.rs index 09c09d103c..d2f11499bb 100644 --- a/cli/tests/integration/run_tests.rs +++ b/cli/tests/integration/run_tests.rs @@ -2779,3 +2779,9 @@ itest!(spawn_stdout_inherit { args: "run --quiet --unstable -A spawn_stdout_inherit.ts", output: "spawn_stdout_inherit.ts.out", }); + +itest!(error_name_non_string { + args: "run --quiet error_name_non_string.js", + output: "error_name_non_string.js.out", + exit_code: 1, +}); diff --git a/cli/tests/testdata/error_name_non_string.js b/cli/tests/testdata/error_name_non_string.js new file mode 100644 index 0000000000..ae96099276 --- /dev/null +++ b/cli/tests/testdata/error_name_non_string.js @@ -0,0 +1,8 @@ +class ErrorNameNonString extends Error { + constructor() { + super(); + this.name = 42; + } +} + +throw new ErrorNameNonString(); diff --git a/cli/tests/testdata/error_name_non_string.js.out b/cli/tests/testdata/error_name_non_string.js.out new file mode 100644 index 0000000000..a77f336e91 --- /dev/null +++ b/cli/tests/testdata/error_name_non_string.js.out @@ -0,0 +1,4 @@ +error: Uncaught Error +throw new ErrorNameNonString(); + ^ + at file:///[WILDCARD]/error_name_non_string.js:[WILDCARD] diff --git a/core/error.rs b/core/error.rs index 045dd5a155..2ba0538029 100644 --- a/core/error.rs +++ b/core/error.rs @@ -164,7 +164,7 @@ fn get_property<'a>( object.get(scope, key.into()) } -#[derive(serde::Deserialize)] +#[derive(Default, serde::Deserialize)] pub(crate) struct NativeJsError { pub name: Option, pub message: Option, @@ -196,7 +196,7 @@ impl JsError { let exception: v8::Local = exception.try_into().unwrap(); let cause = get_property(scope, exception, "cause"); let e: NativeJsError = - serde_v8::from_v8(scope, exception.into()).unwrap(); + serde_v8::from_v8(scope, exception.into()).unwrap_or_default(); // Get the message by formatting error.name and error.message. let name = e.name.clone().unwrap_or_else(|| "Error".to_string()); let message_prop = e.message.clone().unwrap_or_else(|| "".to_string());