| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 724f9d6 commit cc74821
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,6 +15,7 @@ using v8::Maybe; | |||
| 15 | 15 | using v8::Nothing; | |
| 16 | 16 | using v8::SealHandleScope; | |
| 17 | 17 | using v8::SnapshotCreator; | |
| 18 | + using v8::TryCatch; | ||
| 18 | 19 | ||
| 19 | 20 | namespace node { | |
| 20 | 21 | ||
@@ -129,12 +130,21 @@ CommonEnvironmentSetup::CommonEnvironmentSetup( | |||
| 129 | 130 | { | |
| 130 | 131 | Locker locker(isolate); | |
| 131 | 132 | Isolate::Scope isolate_scope(isolate); | |
| 133 | + HandleScope handle_scope(isolate); | ||
| 134 | + | ||
| 135 | + TryCatch bootstrapCatch(isolate); | ||
| 136 | + auto print_Exception = OnScopeLeave([&]() { | ||
| 137 | + if (bootstrapCatch.HasCaught()) { | ||
| 138 | + errors->push_back(FormatCaughtException( | ||
| 139 | + isolate, isolate->GetCurrentContext(), bootstrapCatch)); | ||
| 140 | + } | ||
| 141 | + }); | ||
| 142 | + | ||
| 132 | 143 | impl_->isolate_data.reset(CreateIsolateData( | |
| 133 | 144 | isolate, loop, platform, impl_->allocator.get(), snapshot_data)); | |
| 134 | 145 | impl_->isolate_data->options()->build_snapshot = | |
| 135 | 146 | impl_->snapshot_creator.has_value(); | |
| 136 | 147 | ||
| 137 | - HandleScope handle_scope(isolate); | ||
| 138 | 148 | if (snapshot_data) { | |
| 139 | 149 | impl_->env.reset(make_env(this)); | |
| 140 | 150 | if (impl_->env) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -185,7 +185,8 @@ static std::string GetErrorSource(Isolate* isolate, | |||
| 185 | 185 | return buf + std::string(underline_buf, off); | |
| 186 | 186 | } | |
| 187 | 187 | ||
| 188 | - void PrintStackTrace(Isolate* isolate, Local<StackTrace> stack) { | ||
| 188 | + static std::string FormatStackTrace(Isolate* isolate, Local<StackTrace> stack) { | ||
| 189 | + std::string result; | ||
| 189 | 190 | for (int i = 0; i < stack->GetFrameCount(); i++) { | |
| 190 | 191 | Local<StackFrame> stack_frame = stack->GetFrame(isolate, i); | |
| 191 | 192 | node::Utf8Value fn_name_s(isolate, stack_frame->GetFunctionName()); | |
@@ -195,53 +196,82 @@ void PrintStackTrace(Isolate* isolate, Local<StackTrace> stack) { | |||
| 195 | 196 | ||
| 196 | 197 | if (stack_frame->IsEval()) { | |
| 197 | 198 | if (stack_frame->GetScriptId() == Message::kNoScriptIdInfo) { | |
| 198 | - FPrintF(stderr, " at [eval]:%i:%i\n", line_number, column); | ||
| 199 | + result += SPrintF(" at [eval]:%i:%i\n", line_number, column); | ||
| 199 | 200 | } else { | |
| 200 | - FPrintF(stderr, | ||
| 201 | - " at [eval] (%s:%i:%i)\n", | ||
| 202 | - *script_name, | ||
| 203 | - line_number, | ||
| 204 | - column); | ||
| 201 | + std::vector<char> buf(script_name.length() + 64); | ||
| 202 | + snprintf(buf.data(), | ||
| 203 | + buf.size(), | ||
| 204 | + " at [eval] (%s:%i:%i)\n", | ||
| 205 | + *script_name, | ||
| 206 | + line_number, | ||
| 207 | + column); | ||
| 208 | + result += std::string(buf.data()); | ||
| 205 | 209 | } | |
| 206 | 210 | break; | |
| 207 | 211 | } | |
| 208 | 212 | ||
| 209 | 213 | if (fn_name_s.length() == 0) { | |
| 210 | - FPrintF(stderr, " at %s:%i:%i\n", script_name, line_number, column); | ||
| 214 | + std::vector<char> buf(script_name.length() + 64); | ||
| 215 | + snprintf(buf.data(), | ||
| 216 | + buf.size(), | ||
| 217 | + " at %s:%i:%i\n", | ||
| 218 | + *script_name, | ||
| 219 | + line_number, | ||
| 220 | + column); | ||
| 221 | + result += std::string(buf.data()); | ||
| 211 | 222 | } else { | |
| 212 | - FPrintF(stderr, | ||
| 213 | - " at %s (%s:%i:%i)\n", | ||
| 214 | - fn_name_s, | ||
| 215 | - script_name, | ||
| 216 | - line_number, | ||
| 217 | - column); | ||
| 223 | + std::vector<char> buf(fn_name_s.length() + script_name.length() + 64); | ||
| 224 | + snprintf(buf.data(), | ||
| 225 | + buf.size(), | ||
| 226 | + " at %s (%s:%i:%i)\n", | ||
| 227 | + *fn_name_s, | ||
| 228 | + *script_name, | ||
| 229 | + line_number, | ||
| 230 | + column); | ||
| 231 | + result += std::string(buf.data()); | ||
| 218 | 232 | } | |
| 219 | 233 | } | |
| 234 | + return result; | ||
| 235 | + } | ||
| 236 | + | ||
| 237 | + static void PrintToStderrAndFlush(const std::string& str) { | ||
| 238 | + FPrintF(stderr, "%s\n", str); | ||
| 220 | 239 | fflush(stderr); | |
| 221 | 240 | } | |
| 222 | 241 | ||
| 223 | - void PrintException(Isolate* isolate, | ||
| 224 | - Local<Context> context, | ||
| 225 | - Local<Value> err, | ||
| 226 | - Local<Message> message) { | ||
| 242 | + void PrintStackTrace(Isolate* isolate, Local<StackTrace> stack) { | ||
| 243 | + PrintToStderrAndFlush(FormatStackTrace(isolate, stack)); | ||
| 244 | + } | ||
| 245 | + | ||
| 246 | + std::string FormatCaughtException(Isolate* isolate, | ||
| 247 | + Local<Context> context, | ||
| 248 | + Local<Value> err, | ||
| 249 | + Local<Message> message) { | ||
| 227 | 250 | node::Utf8Value reason(isolate, | |
| 228 | 251 | err->ToDetailString(context) | |
| 229 | 252 | .FromMaybe(Local<String>())); | |
| 230 | 253 | bool added_exception_line = false; | |
| 231 | 254 | std::string source = | |
| 232 | 255 | GetErrorSource(isolate, context, message, &added_exception_line); | |
| 233 | - FPrintF(stderr, "%s\n", source); | ||
| 234 | - FPrintF(stderr, "%s\n", reason); | ||
| 256 | + std::string result = source + '\n' + reason.ToString() + '\n'; | ||
| 235 | 257 | ||
| 236 | 258 | Local<v8::StackTrace> stack = message->GetStackTrace(); | |
| 237 | - if (!stack.IsEmpty()) PrintStackTrace(isolate, stack); | ||
| 259 | + if (!stack.IsEmpty()) result += FormatStackTrace(isolate, stack); | ||
| 260 | + return result; | ||
| 261 | + } | ||
| 262 | + | ||
| 263 | + std::string FormatCaughtException(Isolate* isolate, | ||
| 264 | + Local<Context> context, | ||
| 265 | + const v8::TryCatch& try_catch) { | ||
| 266 | + CHECK(try_catch.HasCaught()); | ||
| 267 | + return FormatCaughtException( | ||
| 268 | + isolate, context, try_catch.Exception(), try_catch.Message()); | ||
| 238 | 269 | } | |
| 239 | 270 | ||
| 240 | 271 | void PrintCaughtException(Isolate* isolate, | |
| 241 | 272 | Local<Context> context, | |
| 242 | 273 | const v8::TryCatch& try_catch) { | |
| 243 | - CHECK(try_catch.HasCaught()); | ||
| 244 | - PrintException(isolate, context, try_catch.Exception(), try_catch.Message()); | ||
| 274 | + PrintToStderrAndFlush(FormatCaughtException(isolate, context, try_catch)); | ||
| 245 | 275 | } | |
| 246 | 276 | ||
| 247 | 277 | void AppendExceptionLine(Environment* env, | |
@@ -1089,7 +1119,8 @@ void TriggerUncaughtException(Isolate* isolate, | |||
| 1089 | 1119 | // error is supposed to be thrown at this point. | |
| 1090 | 1120 | // Since we don't have access to Environment here, there is not | |
| 1091 | 1121 | // much we can do, so we just print whatever is useful and crash. | |
| 1092 | - PrintException(isolate, context, error, message); | ||
| 1122 | + PrintToStderrAndFlush( | ||
| 1123 | + FormatCaughtException(isolate, context, error, message)); | ||
| 1093 | 1124 | Abort(); | |
| 1094 | 1125 | } | |
| 1095 | 1126 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -83,6 +83,9 @@ void PrintStackTrace(v8::Isolate* isolate, v8::Local<v8::StackTrace> stack); | |||
| 83 | 83 | void PrintCaughtException(v8::Isolate* isolate, | |
| 84 | 84 | v8::Local<v8::Context> context, | |
| 85 | 85 | const v8::TryCatch& try_catch); | |
| 86 | + std::string FormatCaughtException(v8::Isolate* isolate, | ||
| 87 | + v8::Local<v8::Context> context, | ||
| 88 | + const v8::TryCatch& try_catch); | ||
| 86 | 89 | ||
| 87 | 90 | void ResetStdio(); // Safe to call more than once and from signal handlers. | |
| 88 | 91 | #ifdef __POSIX__ | |
| Back | FazBrowse Home | New Git URL |
0 commit comments