From 19d78539298a4c61b05b8b62e164e81fdf45b4bc Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Fri, 11 Sep 2026 21:31:46 +0200 Subject: [PATCH] src: print exception thrown during primordial initialization Otherwise there's little information for debugging if the initialization fails due to a bug in local development. This should generally only happen during the build process, when the snapshot is built. Signed-off-by: Joyee Cheung --- src/node_realm.cc | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/node_realm.cc b/src/node_realm.cc index d2459d4eeb33..ae57010f93fa 100644 --- a/src/node_realm.cc +++ b/src/node_realm.cc @@ -19,6 +19,7 @@ using v8::MaybeLocal; using v8::Object; using v8::SnapshotCreator; using v8::String; +using v8::TryCatch; using v8::Value; Realm::Realm(Environment* env, v8::Local context, Kind kind) @@ -64,11 +65,22 @@ void Realm::CreateProperties() { Local ctx = context(); // Store primordials setup by the per-context script in the environment. - Local per_context_bindings = - GetPerContextExports(ctx, env_->isolate_data()).ToLocalChecked(); - Local primordials = - per_context_bindings->Get(ctx, env_->primordials_string()) - .ToLocalChecked(); + TryCatch try_catch(isolate_); + Local per_context_bindings; + Local primordials; + if (!GetPerContextExports(ctx, env_->isolate_data()) + .ToLocal(&per_context_bindings) || + !per_context_bindings->Get(ctx, env_->primordials_string()) + .ToLocal(&primordials)) { + // In general, this should only throw exceptions during local development + // when there's a temporary bug in the scripts. Print the exception here to + // facilitate debugging. + if (try_catch.HasCaught() && !try_catch.HasTerminated()) { + PrintCaughtException(isolate_, ctx, try_catch); + } + env_->Exit(ExitCode::kBootstrapFailure); + return; + } CHECK(primordials->IsObject()); set_primordials(primordials.As());