Skip to content

Commit 8d9d823

Browse files
committedApr 13, 2021
bootstrap: mksnapshot should show JS error
PR-URL: #38174 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 7b2bad4 commit 8d9d823

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
 

‎tools/snapshot/snapshot_builder.cc

+41
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ using v8::Context;
1515
using v8::HandleScope;
1616
using v8::Isolate;
1717
using v8::Local;
18+
using v8::Object;
1819
using v8::SnapshotCreator;
1920
using v8::StartupData;
21+
using v8::String;
22+
using v8::TryCatch;
23+
using v8::Value;
2024

2125
template <typename T>
2226
void WriteVector(std::stringstream* ss, const T* vec, size_t size) {
@@ -79,6 +83,10 @@ std::string SnapshotBuilder::Generate(
7983
const std::vector<std::string> args,
8084
const std::vector<std::string> exec_args) {
8185
Isolate* isolate = Isolate::Allocate();
86+
isolate->SetCaptureStackTraceForUncaughtExceptions(
87+
true,
88+
10,
89+
v8::StackTrace::StackTraceOptions::kDetailed);
8290
per_process::v8_platform.Platform()->RegisterIsolate(isolate,
8391
uv_default_loop());
8492
std::unique_ptr<NodeMainInstance> main_instance;
@@ -104,7 +112,40 @@ std::string SnapshotBuilder::Generate(
104112
creator.SetDefaultContext(Context::New(isolate));
105113
isolate_data_indexes = main_instance->isolate_data()->Serialize(&creator);
106114

115+
TryCatch bootstrapCatch(isolate);
107116
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());
139+
}
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();
148+
}
108149
Context::Scope context_scope(context);
109150

110151
env = new Environment(main_instance->isolate_data(),

0 commit comments

Comments
 (0)
Please sign in to comment.