process: move setup of process warnings into node.js
To clarify the side effects and conditions of the warning setup during bootstrap. PR-URL: https://github.com/nodejs/node/pull/25263 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
This commit is contained in:
parent
d28980c464
commit
0878b6172e
@ -107,7 +107,15 @@ function startup() {
|
|||||||
process.exit = wrapped.exit;
|
process.exit = wrapped.exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
NativeModule.require('internal/process/warning').setup();
|
const {
|
||||||
|
onWarning,
|
||||||
|
emitWarning
|
||||||
|
} = NativeModule.require('internal/process/warning');
|
||||||
|
if (!process.noProcessWarnings && process.env.NODE_NO_WARNINGS !== '1') {
|
||||||
|
process.on('warning', onWarning);
|
||||||
|
}
|
||||||
|
process.emitWarning = emitWarning;
|
||||||
|
|
||||||
const {
|
const {
|
||||||
nextTick,
|
nextTick,
|
||||||
runNextTicks
|
runNextTicks
|
||||||
|
@ -3,8 +3,6 @@
|
|||||||
const prefix = `(${process.release.name}:${process.pid}) `;
|
const prefix = `(${process.release.name}:${process.pid}) `;
|
||||||
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
|
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
|
||||||
|
|
||||||
exports.setup = setupProcessWarnings;
|
|
||||||
|
|
||||||
// Lazily loaded
|
// Lazily loaded
|
||||||
let fs;
|
let fs;
|
||||||
let fd;
|
let fd;
|
||||||
@ -51,83 +49,84 @@ function doEmitWarning(warning) {
|
|||||||
return () => process.emit('warning', warning);
|
return () => process.emit('warning', warning);
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupProcessWarnings() {
|
function onWarning(warning) {
|
||||||
if (!process.noProcessWarnings && process.env.NODE_NO_WARNINGS !== '1') {
|
if (!(warning instanceof Error)) return;
|
||||||
process.on('warning', (warning) => {
|
const isDeprecation = warning.name === 'DeprecationWarning';
|
||||||
if (!(warning instanceof Error)) return;
|
if (isDeprecation && process.noDeprecation) return;
|
||||||
const isDeprecation = warning.name === 'DeprecationWarning';
|
const trace = process.traceProcessWarnings ||
|
||||||
if (isDeprecation && process.noDeprecation) return;
|
(isDeprecation && process.traceDeprecation);
|
||||||
const trace = process.traceProcessWarnings ||
|
var msg = prefix;
|
||||||
(isDeprecation && process.traceDeprecation);
|
if (warning.code)
|
||||||
var msg = prefix;
|
msg += `[${warning.code}] `;
|
||||||
if (warning.code)
|
if (trace && warning.stack) {
|
||||||
msg += `[${warning.code}] `;
|
msg += `${warning.stack}`;
|
||||||
if (trace && warning.stack) {
|
} else {
|
||||||
msg += `${warning.stack}`;
|
const toString =
|
||||||
} else {
|
typeof warning.toString === 'function' ?
|
||||||
const toString =
|
warning.toString : Error.prototype.toString;
|
||||||
typeof warning.toString === 'function' ?
|
msg += `${toString.apply(warning)}`;
|
||||||
warning.toString : Error.prototype.toString;
|
|
||||||
msg += `${toString.apply(warning)}`;
|
|
||||||
}
|
|
||||||
if (typeof warning.detail === 'string') {
|
|
||||||
msg += `\n${warning.detail}`;
|
|
||||||
}
|
|
||||||
const warningFile = lazyOption();
|
|
||||||
if (warningFile) {
|
|
||||||
return writeToFile(msg);
|
|
||||||
}
|
|
||||||
writeOut(msg);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
if (typeof warning.detail === 'string') {
|
||||||
// process.emitWarning(error)
|
msg += `\n${warning.detail}`;
|
||||||
// process.emitWarning(str[, type[, code]][, ctor])
|
}
|
||||||
// process.emitWarning(str[, options])
|
const warningFile = lazyOption();
|
||||||
process.emitWarning = (warning, type, code, ctor, now) => {
|
if (warningFile) {
|
||||||
let detail;
|
return writeToFile(msg);
|
||||||
if (type !== null && typeof type === 'object' && !Array.isArray(type)) {
|
}
|
||||||
ctor = type.ctor;
|
writeOut(msg);
|
||||||
code = type.code;
|
|
||||||
if (typeof type.detail === 'string')
|
|
||||||
detail = type.detail;
|
|
||||||
type = type.type || 'Warning';
|
|
||||||
} else if (typeof type === 'function') {
|
|
||||||
ctor = type;
|
|
||||||
code = undefined;
|
|
||||||
type = 'Warning';
|
|
||||||
}
|
|
||||||
if (type !== undefined && typeof type !== 'string') {
|
|
||||||
throw new ERR_INVALID_ARG_TYPE('type', 'string', type);
|
|
||||||
}
|
|
||||||
if (typeof code === 'function') {
|
|
||||||
ctor = code;
|
|
||||||
code = undefined;
|
|
||||||
} else if (code !== undefined && typeof code !== 'string') {
|
|
||||||
throw new ERR_INVALID_ARG_TYPE('code', 'string', code);
|
|
||||||
}
|
|
||||||
if (typeof warning === 'string') {
|
|
||||||
// Improve error creation performance by skipping the error frames.
|
|
||||||
// They are added in the `captureStackTrace()` function below.
|
|
||||||
const tmpStackLimit = Error.stackTraceLimit;
|
|
||||||
Error.stackTraceLimit = 0;
|
|
||||||
// eslint-disable-next-line no-restricted-syntax
|
|
||||||
warning = new Error(warning);
|
|
||||||
Error.stackTraceLimit = tmpStackLimit;
|
|
||||||
warning.name = String(type || 'Warning');
|
|
||||||
if (code !== undefined) warning.code = code;
|
|
||||||
if (detail !== undefined) warning.detail = detail;
|
|
||||||
Error.captureStackTrace(warning, ctor || process.emitWarning);
|
|
||||||
} else if (!(warning instanceof Error)) {
|
|
||||||
throw new ERR_INVALID_ARG_TYPE('warning', ['Error', 'string'], warning);
|
|
||||||
}
|
|
||||||
if (warning.name === 'DeprecationWarning') {
|
|
||||||
if (process.noDeprecation)
|
|
||||||
return;
|
|
||||||
if (process.throwDeprecation)
|
|
||||||
throw warning;
|
|
||||||
}
|
|
||||||
if (now) process.emit('warning', warning);
|
|
||||||
else process.nextTick(doEmitWarning(warning));
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// process.emitWarning(error)
|
||||||
|
// process.emitWarning(str[, type[, code]][, ctor])
|
||||||
|
// process.emitWarning(str[, options])
|
||||||
|
function emitWarning(warning, type, code, ctor, now) {
|
||||||
|
let detail;
|
||||||
|
if (type !== null && typeof type === 'object' && !Array.isArray(type)) {
|
||||||
|
ctor = type.ctor;
|
||||||
|
code = type.code;
|
||||||
|
if (typeof type.detail === 'string')
|
||||||
|
detail = type.detail;
|
||||||
|
type = type.type || 'Warning';
|
||||||
|
} else if (typeof type === 'function') {
|
||||||
|
ctor = type;
|
||||||
|
code = undefined;
|
||||||
|
type = 'Warning';
|
||||||
|
}
|
||||||
|
if (type !== undefined && typeof type !== 'string') {
|
||||||
|
throw new ERR_INVALID_ARG_TYPE('type', 'string', type);
|
||||||
|
}
|
||||||
|
if (typeof code === 'function') {
|
||||||
|
ctor = code;
|
||||||
|
code = undefined;
|
||||||
|
} else if (code !== undefined && typeof code !== 'string') {
|
||||||
|
throw new ERR_INVALID_ARG_TYPE('code', 'string', code);
|
||||||
|
}
|
||||||
|
if (typeof warning === 'string') {
|
||||||
|
// Improve error creation performance by skipping the error frames.
|
||||||
|
// They are added in the `captureStackTrace()` function below.
|
||||||
|
const tmpStackLimit = Error.stackTraceLimit;
|
||||||
|
Error.stackTraceLimit = 0;
|
||||||
|
// eslint-disable-next-line no-restricted-syntax
|
||||||
|
warning = new Error(warning);
|
||||||
|
Error.stackTraceLimit = tmpStackLimit;
|
||||||
|
warning.name = String(type || 'Warning');
|
||||||
|
if (code !== undefined) warning.code = code;
|
||||||
|
if (detail !== undefined) warning.detail = detail;
|
||||||
|
Error.captureStackTrace(warning, ctor || process.emitWarning);
|
||||||
|
} else if (!(warning instanceof Error)) {
|
||||||
|
throw new ERR_INVALID_ARG_TYPE('warning', ['Error', 'string'], warning);
|
||||||
|
}
|
||||||
|
if (warning.name === 'DeprecationWarning') {
|
||||||
|
if (process.noDeprecation)
|
||||||
|
return;
|
||||||
|
if (process.throwDeprecation)
|
||||||
|
throw warning;
|
||||||
|
}
|
||||||
|
if (now) process.emit('warning', warning);
|
||||||
|
else process.nextTick(doEmitWarning(warning));
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
onWarning,
|
||||||
|
emitWarning
|
||||||
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user