process: refactor emitWarning
Remove a couple of obsolete checks by refactoring the code with else. Also remove the possibility of an undefined warning. That is neither documented nor does it result in a usable warning. PR-URL: https://github.com/nodejs/node/pull/20726 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
43ee4d692a
commit
49681e7414
@ -105,7 +105,7 @@ function setupProcessWarnings() {
|
|||||||
// process.emitWarning(str[, type[, code]][, ctor])
|
// process.emitWarning(str[, type[, code]][, ctor])
|
||||||
// process.emitWarning(str[, options])
|
// process.emitWarning(str[, options])
|
||||||
process.emitWarning = function(warning, type, code, ctor, now) {
|
process.emitWarning = function(warning, type, code, ctor, now) {
|
||||||
var detail;
|
let detail;
|
||||||
if (type !== null && typeof type === 'object' && !Array.isArray(type)) {
|
if (type !== null && typeof type === 'object' && !Array.isArray(type)) {
|
||||||
ctor = type.ctor;
|
ctor = type.ctor;
|
||||||
code = type.code;
|
code = type.code;
|
||||||
@ -117,23 +117,23 @@ function setupProcessWarnings() {
|
|||||||
code = undefined;
|
code = undefined;
|
||||||
type = 'Warning';
|
type = 'Warning';
|
||||||
}
|
}
|
||||||
|
if (type !== undefined && typeof type !== 'string') {
|
||||||
|
throw new ERR_INVALID_ARG_TYPE('type', 'string', type);
|
||||||
|
}
|
||||||
if (typeof code === 'function') {
|
if (typeof code === 'function') {
|
||||||
ctor = code;
|
ctor = code;
|
||||||
code = undefined;
|
code = undefined;
|
||||||
}
|
} else if (code !== undefined && typeof code !== 'string') {
|
||||||
if (code !== undefined && typeof code !== 'string')
|
|
||||||
throw new ERR_INVALID_ARG_TYPE('code', 'string', code);
|
throw new ERR_INVALID_ARG_TYPE('code', 'string', code);
|
||||||
if (type !== undefined && typeof type !== 'string')
|
}
|
||||||
throw new ERR_INVALID_ARG_TYPE('type', 'string', type);
|
if (typeof warning === 'string') {
|
||||||
if (warning === undefined || typeof warning === 'string') {
|
|
||||||
// eslint-disable-next-line no-restricted-syntax
|
// eslint-disable-next-line no-restricted-syntax
|
||||||
warning = new Error(warning);
|
warning = new Error(warning);
|
||||||
warning.name = String(type || 'Warning');
|
warning.name = String(type || 'Warning');
|
||||||
if (code !== undefined) warning.code = code;
|
if (code !== undefined) warning.code = code;
|
||||||
if (detail !== undefined) warning.detail = detail;
|
if (detail !== undefined) warning.detail = detail;
|
||||||
Error.captureStackTrace(warning, ctor || process.emitWarning);
|
Error.captureStackTrace(warning, ctor || process.emitWarning);
|
||||||
}
|
} else if (!(warning instanceof Error)) {
|
||||||
if (!(warning instanceof Error)) {
|
|
||||||
throw new ERR_INVALID_ARG_TYPE('warning', ['Error', 'string'], warning);
|
throw new ERR_INVALID_ARG_TYPE('warning', ['Error', 'string'], warning);
|
||||||
}
|
}
|
||||||
if (warning.name === 'DeprecationWarning') {
|
if (warning.name === 'DeprecationWarning') {
|
||||||
|
@ -43,8 +43,8 @@ class CustomWarning extends Error {
|
|||||||
[testMsg, { type: testType, code: testCode, detail: [] }],
|
[testMsg, { type: testType, code: testCode, detail: [] }],
|
||||||
[testMsg, { type: testType, code: testCode, detail: null }],
|
[testMsg, { type: testType, code: testCode, detail: null }],
|
||||||
[testMsg, { type: testType, code: testCode, detail: 1 }]
|
[testMsg, { type: testType, code: testCode, detail: 1 }]
|
||||||
].forEach((i) => {
|
].forEach((args) => {
|
||||||
process.emitWarning.apply(null, i);
|
process.emitWarning(...args);
|
||||||
});
|
});
|
||||||
|
|
||||||
const warningNoToString = new CustomWarning();
|
const warningNoToString = new CustomWarning();
|
||||||
@ -57,18 +57,25 @@ warningThrowToString.toString = function() {
|
|||||||
};
|
};
|
||||||
process.emitWarning(warningThrowToString);
|
process.emitWarning(warningThrowToString);
|
||||||
|
|
||||||
const expectedError =
|
|
||||||
common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError }, 11);
|
|
||||||
|
|
||||||
// TypeError is thrown on invalid input
|
// TypeError is thrown on invalid input
|
||||||
assert.throws(() => process.emitWarning(1), expectedError);
|
[
|
||||||
assert.throws(() => process.emitWarning({}), expectedError);
|
[1],
|
||||||
assert.throws(() => process.emitWarning(true), expectedError);
|
[{}],
|
||||||
assert.throws(() => process.emitWarning([]), expectedError);
|
[true],
|
||||||
assert.throws(() => process.emitWarning('', '', {}), expectedError);
|
[[]],
|
||||||
assert.throws(() => process.emitWarning('', 1), expectedError);
|
['', '', {}],
|
||||||
assert.throws(() => process.emitWarning('', '', 1), expectedError);
|
['', 1],
|
||||||
assert.throws(() => process.emitWarning('', true), expectedError);
|
['', '', 1],
|
||||||
assert.throws(() => process.emitWarning('', '', true), expectedError);
|
['', true],
|
||||||
assert.throws(() => process.emitWarning('', []), expectedError);
|
['', '', true],
|
||||||
assert.throws(() => process.emitWarning('', '', []), expectedError);
|
['', []],
|
||||||
|
['', '', []],
|
||||||
|
[],
|
||||||
|
[undefined, 'foo', 'bar'],
|
||||||
|
[undefined]
|
||||||
|
].forEach((args) => {
|
||||||
|
common.expectsError(
|
||||||
|
() => process.emitWarning(...args),
|
||||||
|
{ code: 'ERR_INVALID_ARG_TYPE', type: TypeError }
|
||||||
|
);
|
||||||
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user