Skip to content

Commit a0f7284

Browse files
committed
errors,process: fix error message of hrtime()
process.hrtime() incorrectly passed the function name to errors.TypeError instead of the name of the argument. Additionally, the type of the actual argument was added to the error message and a new error code ERR_INVALID_ARRAY_LENGTH was added. PR-URL: #13739 Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent d291338 commit a0f7284

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

lib/internal/errors.js

+9
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,15 @@ E('ERR_HTTP_INVALID_CHAR', 'Invalid character in statusMessage.');
122122
E('ERR_HTTP_INVALID_STATUS_CODE',
123123
(originalStatusCode) => `Invalid status code: ${originalStatusCode}`);
124124
E('ERR_INDEX_OUT_OF_RANGE', 'Index out of range');
125+
E('ERR_INVALID_ARRAY_LENGTH',
126+
(name, length, actual) => {
127+
let msg = `The "${name}" array must have a length of ${length}`;
128+
if (arguments.length > 2) {
129+
const len = Array.isArray(actual) ? actual.length : actual;
130+
msg += `. Received length ${len}`;
131+
}
132+
return msg;
133+
});
125134
E('ERR_INVALID_ARG_TYPE', invalidArgType);
126135
E('ERR_INVALID_CALLBACK', 'callback must be a function');
127136
E('ERR_INVALID_FD', (fd) => `"fd" must be a positive integer: ${fd}`);

lib/internal/process.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,19 @@ function setup_hrtime() {
7878
_hrtime(hrValues);
7979

8080
if (time !== undefined) {
81-
if (Array.isArray(time) && time.length === 2) {
82-
const sec = (hrValues[0] * 0x100000000 + hrValues[1]) - time[0];
83-
const nsec = hrValues[2] - time[1];
84-
const needsBorrow = nsec < 0;
85-
return [needsBorrow ? sec - 1 : sec, needsBorrow ? nsec + 1e9 : nsec];
81+
if (!Array.isArray(time)) {
82+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'time', 'Array',
83+
time);
8684
}
87-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
88-
'process.hrtime()', 'Array');
85+
if (time.length !== 2) {
86+
throw new errors.TypeError('ERR_INVALID_ARRAY_LENGTH', 'time', 2,
87+
time);
88+
}
89+
90+
const sec = (hrValues[0] * 0x100000000 + hrValues[1]) - time[0];
91+
const nsec = hrValues[2] - time[1];
92+
const needsBorrow = nsec < 0;
93+
return [needsBorrow ? sec - 1 : sec, needsBorrow ? nsec + 1e9 : nsec];
8994
}
9095

9196
return [

test/parallel/test-process-hrtime.js

+20-10
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,35 @@ validateTuple(tuple);
3232
// validate that passing an existing tuple returns another valid tuple
3333
validateTuple(process.hrtime(tuple));
3434

35-
const invalidHrtimeArgument = common.expectsError({
36-
code: 'ERR_INVALID_ARG_TYPE',
37-
type: TypeError,
38-
message: 'The "process.hrtime()" argument must be of type Array'
39-
});
40-
4135
// test that only an Array may be passed to process.hrtime()
4236
assert.throws(() => {
4337
process.hrtime(1);
44-
}, invalidHrtimeArgument);
38+
}, common.expectsError({
39+
code: 'ERR_INVALID_ARG_TYPE',
40+
type: TypeError,
41+
message: 'The "time" argument must be of type Array. Received type number'
42+
}));
4543
assert.throws(() => {
4644
process.hrtime([]);
47-
}, invalidHrtimeArgument);
45+
}, common.expectsError({
46+
code: 'ERR_INVALID_ARRAY_LENGTH',
47+
type: TypeError,
48+
message: 'The "time" array must have a length of 2. Received length 0'
49+
}));
4850
assert.throws(() => {
4951
process.hrtime([1]);
50-
}, invalidHrtimeArgument);
52+
}, common.expectsError({
53+
code: 'ERR_INVALID_ARRAY_LENGTH',
54+
type: TypeError,
55+
message: 'The "time" array must have a length of 2. Received length 1'
56+
}));
5157
assert.throws(() => {
5258
process.hrtime([1, 2, 3]);
53-
}, invalidHrtimeArgument);
59+
}, common.expectsError({
60+
code: 'ERR_INVALID_ARRAY_LENGTH',
61+
type: TypeError,
62+
message: 'The "time" array must have a length of 2. Received length 3'
63+
}));
5464

5565
function validateTuple(tuple) {
5666
assert(Array.isArray(tuple));

0 commit comments

Comments
 (0)