|
6 | 6 | #ifdef NODE_REPORT |
7 | 7 | #include "node_report.h" |
8 | 8 | #endif |
| 9 | +#include "node_v8_platform-inl.h" |
9 | 10 |
|
10 | 11 | namespace node { |
11 | 12 |
|
@@ -171,21 +172,27 @@ void PrintStackTrace(Isolate* isolate, Local<StackTrace> stack) { |
171 | 172 | fflush(stderr); |
172 | 173 | } |
173 | 174 |
|
174 | | -void PrintCaughtException(Isolate* isolate, |
175 | | - Local<Context> context, |
176 | | - const v8::TryCatch& try_catch) { |
177 | | - CHECK(try_catch.HasCaught()); |
178 | | - Local<Value> err = try_catch.Exception(); |
179 | | - Local<Message> message = try_catch.Message(); |
180 | | - Local<v8::StackTrace> stack = message->GetStackTrace(); |
| 175 | +void PrintException(Isolate* isolate, |
| 176 | + Local<Context> context, |
| 177 | + Local<Value> err, |
| 178 | + Local<Message> message) { |
181 | 179 | node::Utf8Value reason(isolate, |
182 | 180 | err->ToDetailString(context).ToLocalChecked()); |
183 | 181 | bool added_exception_line = false; |
184 | 182 | std::string source = |
185 | 183 | GetErrorSource(isolate, context, message, &added_exception_line); |
186 | 184 | fprintf(stderr, "%s\n", source.c_str()); |
187 | 185 | fprintf(stderr, "%s\n", *reason); |
188 | | - PrintStackTrace(isolate, stack); |
| 186 | + |
| 187 | + Local<v8::StackTrace> stack = message->GetStackTrace(); |
| 188 | + if (!stack.IsEmpty()) PrintStackTrace(isolate, stack); |
| 189 | +} |
| 190 | + |
| 191 | +void PrintCaughtException(Isolate* isolate, |
| 192 | + Local<Context> context, |
| 193 | + const v8::TryCatch& try_catch) { |
| 194 | + CHECK(try_catch.HasCaught()); |
| 195 | + PrintException(isolate, context, try_catch.Exception(), try_catch.Message()); |
189 | 196 | } |
190 | 197 |
|
191 | 198 | void AppendExceptionLine(Environment* env, |
@@ -777,8 +784,20 @@ void FatalException(Isolate* isolate, |
777 | 784 | CHECK(!error.IsEmpty()); |
778 | 785 | HandleScope scope(isolate); |
779 | 786 |
|
780 | | - Environment* env = Environment::GetCurrent(isolate); |
781 | | - CHECK_NOT_NULL(env); // TODO(addaleax): Handle nullptr here. |
| 787 | + CHECK(isolate->InContext()); |
| 788 | + Local<Context> context = isolate->GetCurrentContext(); |
| 789 | + Environment* env = Environment::GetCurrent(context); |
| 790 | + if (env == nullptr) { |
| 791 | + // This means that the exception happens before Environment is assigned |
| 792 | + // to the context e.g. when there is a SyntaxError in a per-context |
| 793 | + // script - which usually indicates that there is a bug because no JS |
| 794 | + // error is supposed to be thrown at this point. |
| 795 | + // Since we don't have access to Environment here, there is not |
| 796 | + // much we can do, so we just print whatever is useful and crash. |
| 797 | + PrintException(isolate, context, error, message); |
| 798 | + Abort(); |
| 799 | + } |
| 800 | + |
782 | 801 | Local<Object> process_object = env->process_object(); |
783 | 802 | Local<String> fatal_exception_string = env->fatal_exception_string(); |
784 | 803 | Local<Value> fatal_exception_function = |
|
0 commit comments