Skip to content

Commit 058e7fb

Browse files
committed
process: fix error handling
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>
1 parent 333adf6 commit 058e7fb

File tree

2 files changed

+89
-60
lines changed

2 files changed

+89
-60
lines changed

lib/internal/process.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {
77
ERR_CPU_USAGE,
88
ERR_INVALID_ARG_TYPE,
99
ERR_INVALID_ARRAY_LENGTH,
10+
ERR_INVALID_OPT_VALUE,
1011
ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET,
1112
ERR_UNKNOWN_SIGNAL
1213
}
@@ -41,11 +42,24 @@ function setup_cpuUsage() {
4142
// If a previous value was passed in, ensure it has the correct shape.
4243
if (prevValue) {
4344
if (!previousValueIsValid(prevValue.user)) {
44-
throw new ERR_INVALID_ARG_TYPE('preValue.user', 'number');
45+
if (typeof prevValue !== 'object')
46+
throw new ERR_INVALID_ARG_TYPE('prevValue', 'object', prevValue);
47+
48+
if (typeof prevValue.user !== 'number') {
49+
throw new ERR_INVALID_ARG_TYPE('prevValue.user',
50+
'number', prevValue.user);
51+
}
52+
throw new ERR_INVALID_OPT_VALUE.RangeError('prevValue.user',
53+
prevValue.user);
4554
}
4655

4756
if (!previousValueIsValid(prevValue.system)) {
48-
throw new ERR_INVALID_ARG_TYPE('preValue.system', 'number');
57+
if (typeof prevValue.system !== 'number') {
58+
throw new ERR_INVALID_ARG_TYPE('prevValue.system',
59+
'number', prevValue.system);
60+
}
61+
throw new ERR_INVALID_OPT_VALUE.RangeError('prevValue.system',
62+
prevValue.system);
4963
}
5064
}
5165

test/parallel/test-process-cpuUsage.js

Lines changed: 73 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
2+
require('../common');
23
const assert = require('assert');
3-
const common = require('../common');
44
const result = process.cpuUsage();
55

66
// Validate the result of calling with no previous value argument.
@@ -31,65 +31,80 @@ for (let i = 0; i < 10; i++) {
3131
assert(diffUsage.user >= 0);
3232
assert(diffUsage.system >= 0);
3333
}
34-
const invalidUserArgument = common.expectsError({
35-
code: 'ERR_INVALID_ARG_TYPE',
36-
type: TypeError,
37-
message: 'The "preValue.user" property must be of type number'
38-
}, 8);
39-
40-
const invalidSystemArgument = common.expectsError({
41-
code: 'ERR_INVALID_ARG_TYPE',
42-
type: TypeError,
43-
message: 'The "preValue.system" property must be of type number'
44-
}, 2);
45-
4634

4735
// Ensure that an invalid shape for the previous value argument throws an error.
48-
assert.throws(() => {
49-
process.cpuUsage(1);
50-
}, invalidUserArgument);
51-
52-
assert.throws(() => {
53-
process.cpuUsage({});
54-
}, invalidUserArgument);
55-
56-
assert.throws(() => {
57-
process.cpuUsage({ user: 'a' });
58-
}, invalidUserArgument);
59-
60-
assert.throws(() => {
61-
process.cpuUsage({ system: 'b' });
62-
}, invalidUserArgument);
63-
64-
assert.throws(() => {
65-
process.cpuUsage({ user: null, system: 'c' });
66-
}, invalidUserArgument);
67-
68-
assert.throws(() => {
69-
process.cpuUsage({ user: 'd', system: null });
70-
}, invalidUserArgument);
71-
72-
assert.throws(() => {
73-
process.cpuUsage({ user: -1, system: 2 });
74-
}, invalidUserArgument);
75-
76-
assert.throws(() => {
77-
process.cpuUsage({ user: 3, system: -2 });
78-
}, invalidSystemArgument);
79-
80-
assert.throws(() => {
81-
process.cpuUsage({
82-
user: Number.POSITIVE_INFINITY,
83-
system: 4
84-
});
85-
}, invalidUserArgument);
86-
87-
assert.throws(() => {
88-
process.cpuUsage({
89-
user: 5,
90-
system: Number.NEGATIVE_INFINITY
91-
});
92-
}, invalidSystemArgument);
36+
assert.throws(
37+
() => process.cpuUsage(1),
38+
{
39+
code: 'ERR_INVALID_ARG_TYPE',
40+
name: 'TypeError [ERR_INVALID_ARG_TYPE]',
41+
message: 'The "prevValue" argument must be of type object. ' +
42+
'Received type number'
43+
}
44+
);
45+
46+
// Check invalid types.
47+
[
48+
{},
49+
{ user: 'a' },
50+
{ user: null, system: 'c' },
51+
].forEach((value) => {
52+
assert.throws(
53+
() => process.cpuUsage(value),
54+
{
55+
code: 'ERR_INVALID_ARG_TYPE',
56+
name: 'TypeError [ERR_INVALID_ARG_TYPE]',
57+
message: 'The "prevValue.user" property must be of type number. ' +
58+
`Received type ${typeof value.user}`
59+
}
60+
);
61+
});
62+
63+
[
64+
{ user: 3, system: 'b' },
65+
{ user: 3, system: null }
66+
].forEach((value) => {
67+
assert.throws(
68+
() => process.cpuUsage(value),
69+
{
70+
code: 'ERR_INVALID_ARG_TYPE',
71+
name: 'TypeError [ERR_INVALID_ARG_TYPE]',
72+
message: 'The "prevValue.system" property must be of type number. ' +
73+
`Received type ${typeof value.system}`
74+
}
75+
);
76+
});
77+
78+
// Check invalid values.
79+
[
80+
{ user: -1, system: 2 },
81+
{ user: Number.POSITIVE_INFINITY, system: 4 }
82+
].forEach((value) => {
83+
assert.throws(
84+
() => process.cpuUsage(value),
85+
{
86+
code: 'ERR_INVALID_OPT_VALUE',
87+
name: 'RangeError [ERR_INVALID_OPT_VALUE]',
88+
message: `The value "${value.user}" is invalid ` +
89+
'for option "prevValue.user"'
90+
}
91+
);
92+
});
93+
94+
[
95+
{ user: 3, system: -2 },
96+
{ user: 5, system: Number.NEGATIVE_INFINITY }
97+
].forEach((value) => {
98+
assert.throws(
99+
() => process.cpuUsage(value),
100+
{
101+
code: 'ERR_INVALID_OPT_VALUE',
102+
name: 'RangeError [ERR_INVALID_OPT_VALUE]',
103+
message: `The value "${value.system}" is invalid ` +
104+
'for option "prevValue.system"'
105+
}
106+
);
107+
});
93108

94109
// Ensure that the return value is the expected shape.
95110
function validateResult(result) {

0 commit comments

Comments
 (0)