process: add optional detail to process emitWarning · nodejs/node@dd20e68 · GitHub
Skip to content

Commit dd20e68

Browse files
committed
process: add optional detail to process emitWarning
Adds a new method signature variant for process.emitWarning() that accepts an options object. The options object may include a new `detail` option that allows additional detail text to be associated with the warning. By default, this additional text will be printed to stderr along with the warning, and included on the Warning Error object using the `.detail` property. e.g. ```js process.emitWarning('A message', { code: 'WARNING123', detail: 'This is additional detail' }); // Emits: // (node {pid}) [WARNING123] Warning: A message // This is additional detail ``` PR-URL: #12725 Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 23fc082 commit dd20e68

3 files changed

Lines changed: 107 additions & 36 deletions

File tree

doc/api/process.md

Lines changed: 51 additions & 5 deletions

lib/internal/process/warning.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,30 +90,37 @@ function setupProcessWarnings() {
9090
if (isDeprecation && process.noDeprecation) return;
9191
const trace = process.traceProcessWarnings ||
9292
(isDeprecation && process.traceDeprecation);
93+
let msg = `${prefix}`;
94+
if (warning.code)
95+
msg += `[${warning.code}] `;
9396
if (trace && warning.stack) {
94-
if (warning.code) {
95-
output(`${prefix}[${warning.code}] ${warning.stack}`);
96-
} else {
97-
output(`${prefix}${warning.stack}`);
98-
}
97+
msg += `${warning.stack}`;
9998
} else {
10099
const toString =
101100
typeof warning.toString === 'function' ?
102101
warning.toString : Error.prototype.toString;
103-
if (warning.code) {
104-
output(`${prefix}[${warning.code}] ${toString.apply(warning)}`);
105-
} else {
106-
output(`${prefix}${toString.apply(warning)}`);
107-
}
102+
msg += `${toString.apply(warning)}`;
108103
}
104+
if (typeof warning.detail === 'string') {
105+
msg += `\n${warning.detail}`;
106+
}
107+
output(msg);
109108
});
110109
}
111110

112111
// process.emitWarning(error)
113112
// process.emitWarning(str[, type[, code]][, ctor])
113+
// process.emitWarning(str[, options])
114114
process.emitWarning = function(warning, type, code, ctor) {
115115
const errors = lazyErrors();
116-
if (typeof type === 'function') {
116+
var detail;
117+
if (type !== null && typeof type === 'object' && !Array.isArray(type)) {
118+
ctor = type.ctor;
119+
code = type.code;
120+
if (typeof type.detail === 'string')
121+
detail = type.detail;
122+
type = type.type || 'Warning';
123+
} else if (typeof type === 'function') {
117124
ctor = type;
118125
code = undefined;
119126
type = 'Warning';
@@ -130,6 +137,7 @@ function setupProcessWarnings() {
130137
warning = new Error(warning);
131138
warning.name = String(type || 'Warning');
132139
if (code !== undefined) warning.code = code;
140+
if (detail !== undefined) warning.detail = detail;
133141
Error.captureStackTrace(warning, ctor || process.emitWarning);
134142
}
135143
if (!(warning instanceof Error)) {

test/parallel/test-process-emitwarning.js

Lines changed: 37 additions & 20 deletions

0 commit comments

Comments
 (0)