Skip to content

Commit ab44106

Browse files
joyeecheungdanielleadams
authored andcommitted
tools: use PrintCaughtException in the snapshot builder
This prints not only the error message but also the error source line and the stack trace wherever possible. PR-URL: #38745 Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Minwoo Jung <[email protected]>
1 parent 1f5baaa commit ab44106

File tree

1 file changed

+23
-35
lines changed

1 file changed

+23
-35
lines changed

tools/snapshot/snapshot_builder.cc

+23-35
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <sstream>
44
#include "debug_utils-inl.h"
55
#include "env-inl.h"
6+
#include "node_errors.h"
67
#include "node_external_reference.h"
78
#include "node_internals.h"
89
#include "node_main_instance.h"
@@ -15,10 +16,8 @@ using v8::Context;
1516
using v8::HandleScope;
1617
using v8::Isolate;
1718
using v8::Local;
18-
using v8::Object;
1919
using v8::SnapshotCreator;
2020
using v8::StartupData;
21-
using v8::String;
2221
using v8::TryCatch;
2322
using v8::Value;
2423

@@ -112,55 +111,44 @@ std::string SnapshotBuilder::Generate(
112111
creator.SetDefaultContext(Context::New(isolate));
113112
isolate_data_indexes = main_instance->isolate_data()->Serialize(&creator);
114113

115-
TryCatch bootstrapCatch(isolate);
116-
Local<Context> context = NewContext(isolate);
117-
if (bootstrapCatch.HasCaught()) {
118-
Local<Object> obj = bootstrapCatch.Exception()->ToObject(context)
119-
.ToLocalChecked();
120-
Local<Value> stack = obj->Get(
121-
context,
122-
FIXED_ONE_BYTE_STRING(isolate, "stack")).ToLocalChecked();
123-
if (stack->IsUndefined()) {
124-
Local<String> str = obj->Get(
125-
context,
126-
FIXED_ONE_BYTE_STRING(isolate, "name"))
127-
.ToLocalChecked()->ToString(context).ToLocalChecked();
128-
str = String::Concat(
129-
isolate,
130-
str,
131-
FIXED_ONE_BYTE_STRING(isolate, ": "));
132-
stack = String::Concat(
133-
isolate,
134-
str,
135-
obj->Get(
136-
context,
137-
FIXED_ONE_BYTE_STRING(isolate, "message"))
138-
.ToLocalChecked()->ToString(context).ToLocalChecked());
114+
// Run the per-context scripts
115+
Local<Context> context;
116+
{
117+
TryCatch bootstrapCatch(isolate);
118+
context = NewContext(isolate);
119+
if (bootstrapCatch.HasCaught()) {
120+
PrintCaughtException(isolate, context, bootstrapCatch);
121+
abort();
139122
}
140-
v8::String::Utf8Value utf8_value(isolate, stack);
141-
if (*utf8_value != nullptr) {
142-
std::string out(*utf8_value, utf8_value.length());
143-
fprintf(stderr, "Had Exception: %s\n", out.c_str());
144-
} else {
145-
fprintf(stderr, "Unknown JS Exception\n");
146-
}
147-
abort();
148123
}
149124
Context::Scope context_scope(context);
150125

126+
// Create the environment
151127
env = new Environment(main_instance->isolate_data(),
152128
context,
153129
args,
154130
exec_args,
155131
nullptr,
156132
node::EnvironmentFlags::kDefaultFlags,
157133
{});
158-
env->RunBootstrapping().ToLocalChecked();
134+
// Run scripts in lib/internal/bootstrap/
135+
{
136+
TryCatch bootstrapCatch(isolate);
137+
v8::MaybeLocal<Value> result = env->RunBootstrapping();
138+
if (bootstrapCatch.HasCaught()) {
139+
PrintCaughtException(isolate, context, bootstrapCatch);
140+
}
141+
result.ToLocalChecked();
142+
}
143+
159144
if (per_process::enabled_debug_list.enabled(DebugCategory::MKSNAPSHOT)) {
160145
env->PrintAllBaseObjects();
161146
printf("Environment = %p\n", env);
162147
}
148+
149+
// Serialize the native states
163150
env_info = env->Serialize(&creator);
151+
// Serialize the context
164152
size_t index = creator.AddContext(
165153
context, {SerializeNodeContextInternalFields, env});
166154
CHECK_EQ(index, NodeMainInstance::kNodeContextIndex);

0 commit comments

Comments
 (0)