mirror of
https://github.com/nodejs/node.git
synced 2024-11-29 23:16:30 +01:00
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: https://github.com/nodejs/node/pull/13739 Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
parent
d2913384aa
commit
a0f7284346
@ -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}`);
|
||||
|
@ -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,
|
||||
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 [
|
||||
|
@ -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));
|
||||
|
Loading…
Reference in New Issue
Block a user