process: fix error handling
This makes sure the proper error is returned. Right now the error is not specific enough. PR-URL: https://github.com/nodejs/node/pull/19445 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
parent
333adf61eb
commit
058e7fb8e6
@ -7,6 +7,7 @@ const {
|
||||
ERR_CPU_USAGE,
|
||||
ERR_INVALID_ARG_TYPE,
|
||||
ERR_INVALID_ARRAY_LENGTH,
|
||||
ERR_INVALID_OPT_VALUE,
|
||||
ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET,
|
||||
ERR_UNKNOWN_SIGNAL
|
||||
}
|
||||
@ -41,11 +42,24 @@ function setup_cpuUsage() {
|
||||
// If a previous value was passed in, ensure it has the correct shape.
|
||||
if (prevValue) {
|
||||
if (!previousValueIsValid(prevValue.user)) {
|
||||
throw new ERR_INVALID_ARG_TYPE('preValue.user', 'number');
|
||||
if (typeof prevValue !== 'object')
|
||||
throw new ERR_INVALID_ARG_TYPE('prevValue', 'object', prevValue);
|
||||
|
||||
if (typeof prevValue.user !== 'number') {
|
||||
throw new ERR_INVALID_ARG_TYPE('prevValue.user',
|
||||
'number', prevValue.user);
|
||||
}
|
||||
throw new ERR_INVALID_OPT_VALUE.RangeError('prevValue.user',
|
||||
prevValue.user);
|
||||
}
|
||||
|
||||
if (!previousValueIsValid(prevValue.system)) {
|
||||
throw new ERR_INVALID_ARG_TYPE('preValue.system', 'number');
|
||||
if (typeof prevValue.system !== 'number') {
|
||||
throw new ERR_INVALID_ARG_TYPE('prevValue.system',
|
||||
'number', prevValue.system);
|
||||
}
|
||||
throw new ERR_INVALID_OPT_VALUE.RangeError('prevValue.system',
|
||||
prevValue.system);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
require('../common');
|
||||
const assert = require('assert');
|
||||
const common = require('../common');
|
||||
const result = process.cpuUsage();
|
||||
|
||||
// Validate the result of calling with no previous value argument.
|
||||
@ -31,65 +31,80 @@ for (let i = 0; i < 10; i++) {
|
||||
assert(diffUsage.user >= 0);
|
||||
assert(diffUsage.system >= 0);
|
||||
}
|
||||
const invalidUserArgument = common.expectsError({
|
||||
code: 'ERR_INVALID_ARG_TYPE',
|
||||
type: TypeError,
|
||||
message: 'The "preValue.user" property must be of type number'
|
||||
}, 8);
|
||||
|
||||
const invalidSystemArgument = common.expectsError({
|
||||
code: 'ERR_INVALID_ARG_TYPE',
|
||||
type: TypeError,
|
||||
message: 'The "preValue.system" property must be of type number'
|
||||
}, 2);
|
||||
|
||||
|
||||
// Ensure that an invalid shape for the previous value argument throws an error.
|
||||
assert.throws(() => {
|
||||
process.cpuUsage(1);
|
||||
}, invalidUserArgument);
|
||||
assert.throws(
|
||||
() => process.cpuUsage(1),
|
||||
{
|
||||
code: 'ERR_INVALID_ARG_TYPE',
|
||||
name: 'TypeError [ERR_INVALID_ARG_TYPE]',
|
||||
message: 'The "prevValue" argument must be of type object. ' +
|
||||
'Received type number'
|
||||
}
|
||||
);
|
||||
|
||||
assert.throws(() => {
|
||||
process.cpuUsage({});
|
||||
}, invalidUserArgument);
|
||||
// Check invalid types.
|
||||
[
|
||||
{},
|
||||
{ user: 'a' },
|
||||
{ user: null, system: 'c' },
|
||||
].forEach((value) => {
|
||||
assert.throws(
|
||||
() => process.cpuUsage(value),
|
||||
{
|
||||
code: 'ERR_INVALID_ARG_TYPE',
|
||||
name: 'TypeError [ERR_INVALID_ARG_TYPE]',
|
||||
message: 'The "prevValue.user" property must be of type number. ' +
|
||||
`Received type ${typeof value.user}`
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
assert.throws(() => {
|
||||
process.cpuUsage({ user: 'a' });
|
||||
}, invalidUserArgument);
|
||||
[
|
||||
{ user: 3, system: 'b' },
|
||||
{ user: 3, system: null }
|
||||
].forEach((value) => {
|
||||
assert.throws(
|
||||
() => process.cpuUsage(value),
|
||||
{
|
||||
code: 'ERR_INVALID_ARG_TYPE',
|
||||
name: 'TypeError [ERR_INVALID_ARG_TYPE]',
|
||||
message: 'The "prevValue.system" property must be of type number. ' +
|
||||
`Received type ${typeof value.system}`
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
assert.throws(() => {
|
||||
process.cpuUsage({ system: 'b' });
|
||||
}, invalidUserArgument);
|
||||
// Check invalid values.
|
||||
[
|
||||
{ user: -1, system: 2 },
|
||||
{ user: Number.POSITIVE_INFINITY, system: 4 }
|
||||
].forEach((value) => {
|
||||
assert.throws(
|
||||
() => process.cpuUsage(value),
|
||||
{
|
||||
code: 'ERR_INVALID_OPT_VALUE',
|
||||
name: 'RangeError [ERR_INVALID_OPT_VALUE]',
|
||||
message: `The value "${value.user}" is invalid ` +
|
||||
'for option "prevValue.user"'
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
assert.throws(() => {
|
||||
process.cpuUsage({ user: null, system: 'c' });
|
||||
}, invalidUserArgument);
|
||||
|
||||
assert.throws(() => {
|
||||
process.cpuUsage({ user: 'd', system: null });
|
||||
}, invalidUserArgument);
|
||||
|
||||
assert.throws(() => {
|
||||
process.cpuUsage({ user: -1, system: 2 });
|
||||
}, invalidUserArgument);
|
||||
|
||||
assert.throws(() => {
|
||||
process.cpuUsage({ user: 3, system: -2 });
|
||||
}, invalidSystemArgument);
|
||||
|
||||
assert.throws(() => {
|
||||
process.cpuUsage({
|
||||
user: Number.POSITIVE_INFINITY,
|
||||
system: 4
|
||||
});
|
||||
}, invalidUserArgument);
|
||||
|
||||
assert.throws(() => {
|
||||
process.cpuUsage({
|
||||
user: 5,
|
||||
system: Number.NEGATIVE_INFINITY
|
||||
});
|
||||
}, invalidSystemArgument);
|
||||
[
|
||||
{ user: 3, system: -2 },
|
||||
{ user: 5, system: Number.NEGATIVE_INFINITY }
|
||||
].forEach((value) => {
|
||||
assert.throws(
|
||||
() => process.cpuUsage(value),
|
||||
{
|
||||
code: 'ERR_INVALID_OPT_VALUE',
|
||||
name: 'RangeError [ERR_INVALID_OPT_VALUE]',
|
||||
message: `The value "${value.system}" is invalid ` +
|
||||
'for option "prevValue.system"'
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
// Ensure that the return value is the expected shape.
|
||||
function validateResult(result) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user