Skip to content

Commit fb356b3

Browse files
committed
src: refactor out FormatErrorMessage for error formatting
PR-URL: #51999 Fixes: #42868 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
1 parent 075c95f commit fb356b3

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/node_errors.cc

+12-2
Original file line numberDiff line numberDiff line change
@@ -303,17 +303,27 @@ std::string FormatCaughtException(Isolate* isolate,
303303
Local<Value> err,
304304
Local<Message> message,
305305
bool add_source_line = true) {
306-
std::string result;
307306
node::Utf8Value reason(isolate,
308307
err->ToDetailString(context)
309308
.FromMaybe(Local<String>()));
309+
std::string reason_str = reason.ToString();
310+
return FormatErrorMessage(
311+
isolate, context, reason_str, message, add_source_line);
312+
}
313+
314+
std::string FormatErrorMessage(Isolate* isolate,
315+
Local<Context> context,
316+
const std::string& reason,
317+
Local<Message> message,
318+
bool add_source_line) {
319+
std::string result;
310320
if (add_source_line) {
311321
bool added_exception_line = false;
312322
std::string source =
313323
GetErrorSource(isolate, context, message, &added_exception_line);
314324
result = source + '\n';
315325
}
316-
result += reason.ToString() + '\n';
326+
result += reason + '\n';
317327

318328
Local<v8::StackTrace> stack = message->GetStackTrace();
319329
if (!stack.IsEmpty()) result += FormatStackTrace(isolate, stack);

src/node_internals.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,11 @@ void PrintCaughtException(v8::Isolate* isolate,
9898
std::string FormatCaughtException(v8::Isolate* isolate,
9999
v8::Local<v8::Context> context,
100100
const v8::TryCatch& try_catch);
101-
101+
std::string FormatErrorMessage(v8::Isolate* isolate,
102+
v8::Local<v8::Context> context,
103+
const std::string& reason,
104+
v8::Local<v8::Message> message,
105+
bool add_source_line = true);
102106
void ResetStdio(); // Safe to call more than once and from signal handlers.
103107
#ifdef __POSIX__
104108
void SignalExit(int signal, siginfo_t* info, void* ucontext);

0 commit comments

Comments
 (0)