mirror of
https://github.com/zebrajr/node.git
synced 2025-12-07 00:20:38 +01:00
This commit adds support for a NODE_NO_WARNINGS environment variable, which duplicates the functionality of the --no-warnings command line flag. Fixes: https://github.com/nodejs/node/issues/10802 PR-URL: https://github.com/nodejs/node/pull/10842 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: Italo A. Casas <me@italoacasas.com>
50 lines
1.6 KiB
JavaScript
50 lines
1.6 KiB
JavaScript
'use strict';
|
|
|
|
const prefix = `(${process.release.name}:${process.pid}) `;
|
|
|
|
exports.setup = setupProcessWarnings;
|
|
|
|
function setupProcessWarnings() {
|
|
if (!process.noProcessWarnings && process.env.NODE_NO_WARNINGS !== '1') {
|
|
process.on('warning', (warning) => {
|
|
if (!(warning instanceof Error)) return;
|
|
const isDeprecation = warning.name === 'DeprecationWarning';
|
|
if (isDeprecation && process.noDeprecation) return;
|
|
const trace = process.traceProcessWarnings ||
|
|
(isDeprecation && process.traceDeprecation);
|
|
if (trace && warning.stack) {
|
|
console.error(`${prefix}${warning.stack}`);
|
|
} else {
|
|
var toString = warning.toString;
|
|
if (typeof toString !== 'function')
|
|
toString = Error.prototype.toString;
|
|
console.error(`${prefix}${toString.apply(warning)}`);
|
|
}
|
|
});
|
|
}
|
|
|
|
// process.emitWarning(error)
|
|
// process.emitWarning(str[, name][, ctor])
|
|
process.emitWarning = function(warning, name, ctor) {
|
|
if (typeof name === 'function') {
|
|
ctor = name;
|
|
name = 'Warning';
|
|
}
|
|
if (warning === undefined || typeof warning === 'string') {
|
|
warning = new Error(warning);
|
|
warning.name = name || 'Warning';
|
|
Error.captureStackTrace(warning, ctor || process.emitWarning);
|
|
}
|
|
if (!(warning instanceof Error)) {
|
|
throw new TypeError('\'warning\' must be an Error object or string.');
|
|
}
|
|
if (warning.name === 'DeprecationWarning') {
|
|
if (process.noDeprecation)
|
|
return;
|
|
if (process.throwDeprecation)
|
|
throw warning;
|
|
}
|
|
process.nextTick(() => process.emit('warning', warning));
|
|
};
|
|
}
|