From fb356b330570fb00c18452dfe2e557fb7d0825f9 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Tue, 5 Mar 2024 16:26:58 +0100 Subject: [PATCH] src: refactor out FormatErrorMessage for error formatting PR-URL: https://github.com/nodejs/node/pull/51999 Fixes: https://github.com/nodejs/node/issues/42868 Reviewed-By: Moshe Atlow Reviewed-By: Benjamin Gruenbaum Reviewed-By: Geoffrey Booth --- src/node_errors.cc | 14 ++++++++++++-- src/node_internals.h | 6 +++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/node_errors.cc b/src/node_errors.cc index 7ef6ea7f07998f..ff091fd20d915b 100644 --- a/src/node_errors.cc +++ b/src/node_errors.cc @@ -303,17 +303,27 @@ std::string FormatCaughtException(Isolate* isolate, Local err, Local message, bool add_source_line = true) { - std::string result; node::Utf8Value reason(isolate, err->ToDetailString(context) .FromMaybe(Local())); + std::string reason_str = reason.ToString(); + return FormatErrorMessage( + isolate, context, reason_str, message, add_source_line); +} + +std::string FormatErrorMessage(Isolate* isolate, + Local context, + const std::string& reason, + Local message, + bool add_source_line) { + std::string result; if (add_source_line) { bool added_exception_line = false; std::string source = GetErrorSource(isolate, context, message, &added_exception_line); result = source + '\n'; } - result += reason.ToString() + '\n'; + result += reason + '\n'; Local stack = message->GetStackTrace(); if (!stack.IsEmpty()) result += FormatStackTrace(isolate, stack); diff --git a/src/node_internals.h b/src/node_internals.h index eeb0fac3fa1aa9..5f0adcf8aaba93 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -98,7 +98,11 @@ void PrintCaughtException(v8::Isolate* isolate, std::string FormatCaughtException(v8::Isolate* isolate, v8::Local context, const v8::TryCatch& try_catch); - +std::string FormatErrorMessage(v8::Isolate* isolate, + v8::Local context, + const std::string& reason, + v8::Local message, + bool add_source_line = true); void ResetStdio(); // Safe to call more than once and from signal handlers. #ifdef __POSIX__ void SignalExit(int signal, siginfo_t* info, void* ucontext);