Skip to content

Commit

Permalink
src: refactor out FormatErrorMessage for error formatting
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
joyeecheung authored and marco-ippolito committed May 2, 2024
1 parent 5fcc1d3 commit e216192
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/node_errors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -303,17 +303,27 @@ std::string FormatCaughtException(Isolate* isolate,
Local<Value> err,
Local<Message> message,
bool add_source_line = true) {
std::string result;
node::Utf8Value reason(isolate,
err->ToDetailString(context)
.FromMaybe(Local<String>()));
std::string reason_str = reason.ToString();
return FormatErrorMessage(
isolate, context, reason_str, message, add_source_line);
}

std::string FormatErrorMessage(Isolate* isolate,
Local<Context> context,
const std::string& reason,
Local<Message> 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<v8::StackTrace> stack = message->GetStackTrace();
if (!stack.IsEmpty()) result += FormatStackTrace(isolate, stack);
Expand Down
6 changes: 5 additions & 1 deletion src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ void PrintCaughtException(v8::Isolate* isolate,
std::string FormatCaughtException(v8::Isolate* isolate,
v8::Local<v8::Context> context,
const v8::TryCatch& try_catch);

std::string FormatErrorMessage(v8::Isolate* isolate,
v8::Local<v8::Context> context,
const std::string& reason,
v8::Local<v8::Message> 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);
Expand Down

0 comments on commit e216192

Please sign in to comment.