Skip to content

Commit

Permalink
src: use basename(argv0) for --trace-uncaught suggestion
Browse files Browse the repository at this point in the history
Refs: #32797 (comment)

PR-URL: #32798
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
  • Loading branch information
addaleax authored and codebytere committed Jun 7, 2020
1 parent 194789f commit edf75e4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/node_errors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,13 @@ static void ReportFatalException(Environment* env,
}

if (!env->options()->trace_uncaught) {
FPrintF(stderr, "(Use `node --trace-uncaught ...` to show "
"where the exception was thrown)\n");
std::string argv0;
if (!env->argv().empty()) argv0 = env->argv()[0];
if (argv0.empty()) argv0 = "node";
FPrintF(stderr,
"(Use `%s --trace-uncaught ...` to show where the exception "
"was thrown)\n",
fs::Basename(argv0, ".exe"));
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ constexpr char kPathSeparator = '/';
const char* const kPathSeparator = "\\/";
#endif

std::string Basename(const std::string& str, const std::string& extension) {
std::string ret = str;

// Remove everything leading up to and including the final path separator.
std::string::size_type pos = ret.find_last_of(kPathSeparator);
if (pos != std::string::npos) ret = ret.substr(pos + 1);

// Strip away the extension, if any.
if (ret.size() >= extension.size() &&
ret.substr(ret.size() - extension.size()) == extension) {
ret = ret.substr(0, ret.size() - extension.size());
}

return ret;
}

inline int64_t GetOffset(Local<Value> value) {
return value->IsNumber() ? value.As<Integer>()->Value() : -1;
}
Expand Down
4 changes: 4 additions & 0 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@ BaseObjectPtr<AsyncWrap> CreateHeapSnapshotStream(
Environment* env, HeapSnapshotPointer&& snapshot);
} // namespace heap

namespace fs {
std::string Basename(const std::string& str, const std::string& extension);
} // namespace fs

} // namespace node

#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
Expand Down

0 comments on commit edf75e4

Please sign in to comment.