Skip to content

Commit

Permalink
process: fix error handling
Browse files Browse the repository at this point in the history
This makes sure the proper error is returned. Right now the error
is not specific enough.

PR-URL: #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>
  • Loading branch information
BridgeAR committed Mar 25, 2018
1 parent 333adf6 commit 058e7fb
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 60 deletions.
18 changes: 16 additions & 2 deletions lib/internal/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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);
}
}

Expand Down
131 changes: 73 additions & 58 deletions test/parallel/test-process-cpuUsage.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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({});
}, invalidUserArgument);

assert.throws(() => {
process.cpuUsage({ user: 'a' });
}, invalidUserArgument);

assert.throws(() => {
process.cpuUsage({ system: 'b' });
}, invalidUserArgument);

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);
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'
}
);

// 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}`
}
);
});

[
{ 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}`
}
);
});

// 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"'
}
);
});

[
{ 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) {
Expand Down

0 comments on commit 058e7fb

Please sign in to comment.