From bdb1ee64800e03699d41265c5158c99e3a390ec7 Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Thu, 21 Jan 2021 09:48:04 +0000 Subject: [PATCH] fix(core): Handle prepareStackTrace() throws (#9211) Fixes #9206 --- cli/tests/082_prepare_stack_trace_throw.js | 6 ++++++ cli/tests/082_prepare_stack_trace_throw.js.out | 2 ++ cli/tests/integration_tests.rs | 6 ++++++ core/error.rs | 6 ++---- 4 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 cli/tests/082_prepare_stack_trace_throw.js create mode 100644 cli/tests/082_prepare_stack_trace_throw.js.out diff --git a/cli/tests/082_prepare_stack_trace_throw.js b/cli/tests/082_prepare_stack_trace_throw.js new file mode 100644 index 0000000000..8137bfdc89 --- /dev/null +++ b/cli/tests/082_prepare_stack_trace_throw.js @@ -0,0 +1,6 @@ +Error.prepareStackTrace = () => { + console.trace(); + throw new Error("foo"); +}; + +new Error("bar").stack; diff --git a/cli/tests/082_prepare_stack_trace_throw.js.out b/cli/tests/082_prepare_stack_trace_throw.js.out new file mode 100644 index 0000000000..751b7c9717 --- /dev/null +++ b/cli/tests/082_prepare_stack_trace_throw.js.out @@ -0,0 +1,2 @@ +[WILDCARD]error: Uncaught Error: foo +[WILDCARD] diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index 428d1ff305..16d3a210c3 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -2685,6 +2685,12 @@ itest!(_081_location_relative_fetch_redirect { http_server: true, }); +itest!(_082_prepare_stack_trace_throw { + args: "run 082_prepare_stack_trace_throw.js", + output: "082_prepare_stack_trace_throw.js.out", + exit_code: 1, +}); + itest!(js_import_detect { args: "run --quiet --reload js_import_detect.ts", output: "js_import_detect.ts.out", diff --git a/core/error.rs b/core/error.rs index db5c8afd68..b68c9df8c7 100644 --- a/core/error.rs +++ b/core/error.rs @@ -193,11 +193,9 @@ impl JsError { // Access error.stack to ensure that prepareStackTrace() has been called. // This should populate error.__callSiteEvals. + let stack = get_property(scope, exception, "stack"); let stack: Option> = - get_property(scope, exception, "stack") - .unwrap() - .try_into() - .ok(); + stack.and_then(|s| s.try_into().ok()); let stack = stack.map(|s| s.to_rust_string_lossy(scope)); // Read an array of structured frames from error.__callSiteEvals.