From 9ec030b1e7f5cff13a972778169fc171d1a8670e Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 16 Sep 2026 20:29:39 +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 PR-URL: https://github.com/nodejs/node/pull/65991 Reviewed-By: Matteo Collina Reviewed-By: Chengzhong Wu Reviewed-By: James M Snell --- 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 91bb530dfd9..167264f797f 100644 --- a/src/node_realm.cc +++ b/src/node_realm.cc @@ -17,6 +17,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) @@ -47,11 +48,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());