From 5910278119acbe82d545aa8a8b20cb85c4ae314c Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Mon, 6 Feb 2023 23:37:33 +0100 Subject: [PATCH] bootstrap: print stack trace during environment creation failure https://github.com/nodejs/node/pull/45888 took the environment creation code out of the scope covered by the v8::TryCatch that we use to print early failures during environment creation. So e.g. when adding something that would fail in node.js, we get ``` node:internal/options:554: Uncaught Error: Should not query options before bootstrapping is done ``` This patch restores that by adding another v8::TryCatch for it: ``` node:internal/options:20 ({ options: optionsMap } = getCLIOptions()); ^ Error: Should not query options before bootstrapping is done at getCLIOptionsFromBinding (node:internal/options:20:32) at getOptionValue (node:internal/options:45:19) at node:internal/bootstrap/node:433:29 ``` --- src/api/embed_helpers.cc | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/api/embed_helpers.cc b/src/api/embed_helpers.cc index 3f8463f6ae518d..602deff835ed49 100644 --- a/src/api/embed_helpers.cc +++ b/src/api/embed_helpers.cc @@ -15,6 +15,7 @@ using v8::Maybe; using v8::Nothing; using v8::SealHandleScope; using v8::SnapshotCreator; +using v8::TryCatch; namespace node { @@ -129,12 +130,21 @@ CommonEnvironmentSetup::CommonEnvironmentSetup( { Locker locker(isolate); Isolate::Scope isolate_scope(isolate); + HandleScope handle_scope(isolate); + + TryCatch bootstrapCatch(isolate); + auto print_Exception = OnScopeLeave([&]() { + if (bootstrapCatch.HasCaught()) { + PrintCaughtException( + isolate, isolate->GetCurrentContext(), bootstrapCatch); + } + }); + impl_->isolate_data.reset(CreateIsolateData( isolate, loop, platform, impl_->allocator.get(), snapshot_data)); impl_->isolate_data->options()->build_snapshot = impl_->snapshot_creator.has_value(); - HandleScope handle_scope(isolate); if (snapshot_data) { impl_->env.reset(make_env(this)); if (impl_->env) {