diff --git a/lib/internal/process.js b/lib/internal/process.js index 375fb314cbd..b735ba2e4b0 100644 --- a/lib/internal/process.js +++ b/lib/internal/process.js @@ -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); } } diff --git a/test/parallel/test-process-cpuUsage.js b/test/parallel/test-process-cpuUsage.js index 92c7b74c591..0b1d53978ca 100644 --- a/test/parallel/test-process-cpuUsage.js +++ b/test/parallel/test-process-cpuUsage.js @@ -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) {