@@ -82,10 +82,11 @@ typedef struct tm TIME_TYPE;
8282#endif
8383
8484// Internal/static function declarations
85- static void WriteNodeReport (Isolate* isolate, DumpEvent event, const char * message, const char * location, char * filename, std::ostream &out, TIME_TYPE* time);
85+ static void WriteNodeReport (Isolate* isolate, DumpEvent event, const char * message, const char * location, char * filename, std::ostream &out, MaybeLocal<Value> error, TIME_TYPE* time);
8686static void PrintCommandLine (std::ostream& out);
8787static void PrintVersionInformation (std::ostream& out);
8888static void PrintJavaScriptStack (std::ostream& out, Isolate* isolate, DumpEvent event, const char * location);
89+ static void PrintJavaScriptErrorStack (std::ostream& out, Isolate* isolate, MaybeLocal<Value> error);
8990static void PrintStackFromStackTrace (std::ostream& out, Isolate* isolate, DumpEvent event);
9091static void PrintStackFrame (std::ostream& out, Isolate* isolate, Local<StackFrame> frame, int index, void * pc);
9192static void PrintNativeStack (std::ostream& out);
@@ -378,8 +379,9 @@ void SetCommandLine() {
378379 * const char* message
379380 * const char* location
380381 * char* name - in/out - returns the report filename
382+ * MaybeLocal<Value> error - JavaScript Error object.
381383 ******************************************************************************/
382- void TriggerNodeReport (Isolate* isolate, DumpEvent event, const char * message, const char * location, char * name) {
384+ void TriggerNodeReport (Isolate* isolate, DumpEvent event, const char * message, const char * location, char * name, MaybeLocal<Value> error ) {
383385 // Recursion check for report in progress, bail out
384386 if (report_active) return ;
385387 report_active = true ;
@@ -460,7 +462,7 @@ void TriggerNodeReport(Isolate* isolate, DumpEvent event, const char* message, c
460462 // Pass our stream about by reference, not by copying it.
461463 std::ostream &out = outfile.is_open () ? outfile : *outstream;
462464
463- WriteNodeReport (isolate, event, message, location, filename, out, &tm_struct);
465+ WriteNodeReport (isolate, event, message, location, filename, out, error, &tm_struct);
464466
465467 // Do not close stdout/stderr, only close files we opened.
466468 if (outfile.is_open ()) {
@@ -474,7 +476,7 @@ void TriggerNodeReport(Isolate* isolate, DumpEvent event, const char* message, c
474476
475477}
476478
477- void GetNodeReport (Isolate* isolate, DumpEvent event, const char * message, const char * location, std::ostream& out) {
479+ void GetNodeReport (Isolate* isolate, DumpEvent event, const char * message, const char * location, MaybeLocal<Value> error, std::ostream& out) {
478480 // Obtain the current time and the pid (platform dependent)
479481 TIME_TYPE tm_struct;
480482#ifdef _WIN32
@@ -484,7 +486,7 @@ void GetNodeReport(Isolate* isolate, DumpEvent event, const char* message, const
484486 gettimeofday (&time_val, nullptr );
485487 localtime_r (&time_val.tv_sec , &tm_struct);
486488#endif
487- WriteNodeReport (isolate, event, message, location, nullptr , out, &tm_struct);
489+ WriteNodeReport (isolate, event, message, location, nullptr , out, error, &tm_struct);
488490}
489491
490492static void walkHandle (uv_handle_t * h, void * arg) {
@@ -531,7 +533,7 @@ static void walkHandle(uv_handle_t* h, void* arg) {
531533 *out << buf;
532534}
533535
534- static void WriteNodeReport (Isolate* isolate, DumpEvent event, const char * message, const char * location, char * filename, std::ostream &out, TIME_TYPE* tm_struct) {
536+ static void WriteNodeReport (Isolate* isolate, DumpEvent event, const char * message, const char * location, char * filename, std::ostream &out, MaybeLocal<Value> error, TIME_TYPE* tm_struct) {
535537
536538#ifdef _WIN32
537539 DWORD pid = GetCurrentProcessId ();
@@ -589,6 +591,11 @@ static void WriteNodeReport(Isolate* isolate, DumpEvent event, const char* messa
589591 PrintNativeStack (out);
590592 out << std::flush;
591593
594+ // Print the stack trace and message from the Error object.
595+ // (If one was provided.)
596+ PrintJavaScriptErrorStack (out, isolate, error);
597+ out << std::flush;
598+
592599 // Print V8 Heap and Garbage Collector information
593600 PrintGCStatistics (out, isolate);
594601 out << std::flush;
@@ -791,6 +798,29 @@ static void PrintJavaScriptStack(std::ostream& out, Isolate* isolate, DumpEvent
791798#endif
792799}
793800
801+ static void PrintJavaScriptErrorStack (std::ostream& out, Isolate* isolate, MaybeLocal<Value> error) {
802+ if (error.IsEmpty () || !error.ToLocalChecked ()->IsNativeError ()) {
803+ return ;
804+ }
805+
806+ out << " \n ================================================================================" ;
807+ out << " \n ==== JavaScript Exception Details ==============================================\n\n " ;
808+ Local<Message> message = v8::Exception::CreateMessage (isolate, error.ToLocalChecked ());
809+ Nan::Utf8String message_str (message->Get ());
810+
811+ out << *message_str << " \n\n " ;
812+
813+ Local<StackTrace> stack = v8::Exception::GetStackTrace (error.ToLocalChecked ());
814+ if (stack.IsEmpty ()) {
815+ out << " No stack trace available from Exception::GetStackTrace()\n " ;
816+ return ;
817+ }
818+ // Print the stack trace, samples are not available as the exception isn't from the current stack.
819+ for (int i = 0 ; i < stack->GetFrameCount (); i++) {
820+ PrintStackFrame (out, isolate, stack->GetFrame (i), i, nullptr );
821+ }
822+ }
823+
794824/* ******************************************************************************
795825 * Function to print stack using GetStackSample() and StackTrace::StackTrace()
796826 *
@@ -842,13 +872,14 @@ static void PrintStackFrame(std::ostream& out, Isolate* isolate, Local<StackFram
842872 char buf[64 ];
843873
844874 // First print the frame index and the instruction address
875+ if (pc != nullptr ) {
845876#ifdef _WIN32
846- snprintf ( buf, sizeof (buf), " %2d: [pc=0x%p] " , i, pc);
847- out << buf;
877+ snprintf ( buf, sizeof (buf), " %2d: [pc=0x%p] " , i, pc);
848878#else
849- snprintf ( buf, sizeof (buf), " %2d: [pc=%p] " , i, pc);
850- out << buf;
879+ snprintf ( buf, sizeof (buf), " %2d: [pc=%p] " , i, pc);
851880#endif
881+ out << buf;
882+ }
852883
853884 // Now print the JavaScript function name and source information
854885 if (frame->IsEval ()) {
0 commit comments