Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process: fix error message of hrtime() #13739

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ E('ERR_HTTP_INVALID_CHAR', 'Invalid character in statusMessage.');
E('ERR_HTTP_INVALID_STATUS_CODE',
(originalStatusCode) => `Invalid status code: ${originalStatusCode}`);
E('ERR_INDEX_OUT_OF_RANGE', 'Index out of range');
E('ERR_INVALID_ARRAY_LENGTH',
(name, length, actual) => {
let msg = `The "${name}" array must have a length of ${length}`;
if (arguments.length > 2) {
const len = Array.isArray(actual) ? actual.length : actual;
msg += `. Received length ${len}`;
}
return msg;
});
E('ERR_INVALID_ARG_TYPE', invalidArgType);
E('ERR_INVALID_CALLBACK', 'callback must be a function');
E('ERR_INVALID_FD', (fd) => `"fd" must be a positive integer: ${fd}`);
Expand Down
19 changes: 12 additions & 7 deletions lib/internal/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,19 @@ function setup_hrtime() {
_hrtime(hrValues);

if (time !== undefined) {
if (Array.isArray(time) && time.length === 2) {
const sec = (hrValues[0] * 0x100000000 + hrValues[1]) - time[0];
const nsec = hrValues[2] - time[1];
const needsBorrow = nsec < 0;
return [needsBorrow ? sec - 1 : sec, needsBorrow ? nsec + 1e9 : nsec];
if (!Array.isArray(time)) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'time', 'Array',
time);
}
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
'process.hrtime()', 'Array');
if (time.length !== 2) {
throw new errors.TypeError('ERR_INVALID_ARRAY_LENGTH', 'time', 2,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be better as a RangeError.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure about this either. RangeError sounds logical at first considering that the length is out of the valid range (even though that range only includes a single value), but on the other hand, the array is used like a mathematical tuple and I would say that for tuples, the number of elements is part of the type. IMO RangeError is better when it is either about a numeric value passed to a function or if there is actually a specific range of allowed implicit values such as the array length (which permits more than a single value). Both TypeError and RangeError are fine by me, but I would like some other opinions on this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RangeError is defined in ES as

Indicates a value that is not in the set or range of allowable values.

Here, it's not that time is not in the set or range of allowable values, but rather a property of it, so TypeError should be more appropriate.

The definition also implies that RangeError can also be used for strings, such as in String.prototype.normalize.

time);
}

const sec = (hrValues[0] * 0x100000000 + hrValues[1]) - time[0];
const nsec = hrValues[2] - time[1];
const needsBorrow = nsec < 0;
return [needsBorrow ? sec - 1 : sec, needsBorrow ? nsec + 1e9 : nsec];
}

return [
Expand Down
30 changes: 20 additions & 10 deletions test/parallel/test-process-hrtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,35 @@ validateTuple(tuple);
// validate that passing an existing tuple returns another valid tuple
validateTuple(process.hrtime(tuple));

const invalidHrtimeArgument = common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "process.hrtime()" argument must be of type Array'
});

// test that only an Array may be passed to process.hrtime()
assert.throws(() => {
process.hrtime(1);
}, invalidHrtimeArgument);
}, common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "time" argument must be of type Array. Received type number'
}));
assert.throws(() => {
process.hrtime([]);
}, invalidHrtimeArgument);
}, common.expectsError({
code: 'ERR_INVALID_ARRAY_LENGTH',
type: TypeError,
message: 'The "time" array must have a length of 2. Received length 0'
}));
assert.throws(() => {
process.hrtime([1]);
}, invalidHrtimeArgument);
}, common.expectsError({
code: 'ERR_INVALID_ARRAY_LENGTH',
type: TypeError,
message: 'The "time" array must have a length of 2. Received length 1'
}));
assert.throws(() => {
process.hrtime([1, 2, 3]);
}, invalidHrtimeArgument);
}, common.expectsError({
code: 'ERR_INVALID_ARRAY_LENGTH',
type: TypeError,
message: 'The "time" array must have a length of 2. Received length 3'
}));

function validateTuple(tuple) {
assert(Array.isArray(tuple));
Expand Down