Skip to content

Commit 5b01772

Browse files
addaleaxBridgeAR
authored andcommitted
src: use basename(argv0) for --trace-uncaught suggestion
Refs: #32797 (comment) PR-URL: #32798 Reviewed-By: David Carlier <[email protected]> Reviewed-By: Richard Lau <[email protected]>
1 parent 3b590d4 commit 5b01772

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/node_errors.cc

+7-2
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,13 @@ static void ReportFatalException(Environment* env,
375375
}
376376

377377
if (!env->options()->trace_uncaught) {
378-
FPrintF(stderr, "(Use `node --trace-uncaught ...` to show "
379-
"where the exception was thrown)\n");
378+
std::string argv0;
379+
if (!env->argv().empty()) argv0 = env->argv()[0];
380+
if (argv0.empty()) argv0 = "node";
381+
FPrintF(stderr,
382+
"(Use `%s --trace-uncaught ...` to show where the exception "
383+
"was thrown)\n",
384+
fs::Basename(argv0, ".exe"));
380385
}
381386
}
382387

src/node_file.cc

+16
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,22 @@ constexpr char kPathSeparator = '/';
8282
const char* const kPathSeparator = "\\/";
8383
#endif
8484

85+
std::string Basename(const std::string& str, const std::string& extension) {
86+
std::string ret = str;
87+
88+
// Remove everything leading up to and including the final path separator.
89+
std::string::size_type pos = ret.find_last_of(kPathSeparator);
90+
if (pos != std::string::npos) ret = ret.substr(pos + 1);
91+
92+
// Strip away the extension, if any.
93+
if (ret.size() >= extension.size() &&
94+
ret.substr(ret.size() - extension.size()) == extension) {
95+
ret = ret.substr(0, ret.size() - extension.size());
96+
}
97+
98+
return ret;
99+
}
100+
85101
inline int64_t GetOffset(Local<Value> value) {
86102
return IsSafeJsInt(value) ? value.As<Integer>()->Value() : -1;
87103
}

src/node_internals.h

+4
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,10 @@ BaseObjectPtr<AsyncWrap> CreateHeapSnapshotStream(
394394
Environment* env, HeapSnapshotPointer&& snapshot);
395395
} // namespace heap
396396

397+
namespace fs {
398+
std::string Basename(const std::string& str, const std::string& extension);
399+
} // namespace fs
400+
397401
} // namespace node
398402

399403
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

0 commit comments

Comments
 (0)