Skip to content

Commit cece325

Browse files
committed
src: refactor out FormatMessage for error formatting
1 parent d4cd374 commit cece325

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/node_errors.cc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,17 +303,26 @@ 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 FormatMessage(isolate, context, reason_str, message, add_source_line);
311+
}
312+
313+
std::string FormatMessage(Isolate* isolate,
314+
Local<Context> context,
315+
const std::string& reason,
316+
Local<Message> message,
317+
bool add_source_line) {
318+
std::string result;
310319
if (add_source_line) {
311320
bool added_exception_line = false;
312321
std::string source =
313322
GetErrorSource(isolate, context, message, &added_exception_line);
314323
result = source + '\n';
315324
}
316-
result += reason.ToString() + '\n';
325+
result += reason + '\n';
317326

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

src/node_internals.h

Lines changed: 5 additions & 1 deletion
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 FormatMessage(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)