Skip to content

Commit

Permalink
fs: complete error message for validate function
Browse files Browse the repository at this point in the history
PR-URL: #19909
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Weijia Wang <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
  • Loading branch information
ah-yu authored and jasnell committed Apr 16, 2018
1 parent ba438fe commit d7b162c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/internal/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,13 @@ function validateLen(len) {
let err;

if (!isInt32(len)) {
if (typeof value !== 'number') {
if (typeof len !== 'number') {
err = new ERR_INVALID_ARG_TYPE('len', 'number', len);
} else {
// TODO(BridgeAR): Improve this error message.
} else if (!Number.isInteger(len)) {
err = new ERR_OUT_OF_RANGE('len', 'an integer', len);
} else {
// 2 ** 31 === 2147483648
err = new ERR_OUT_OF_RANGE('len', '> -2147483649 && < 2147483648', len);
}
}

Expand Down
25 changes: 25 additions & 0 deletions test/parallel/test-fs-truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,31 @@ function testFtruncate(cb) {
);
});

[-1.5, 1.5].forEach((input) => {
assert.throws(
() => fs.ftruncate(fd, input),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "len" is out of range. It must be ' +
`an integer. Received ${input}`
}
);
});

// 2 ** 31 = 2147483648
[2147483648, -2147483649].forEach((input) => {
assert.throws(
() => fs.ftruncate(fd, input),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "len" is out of range. It must be ' +
`> -2147483649 && < 2147483648. Received ${input}`
}
);
});

fs.ftruncate(fd, undefined, common.mustCall(function(err) {
assert.ifError(err);
assert(fs.readFileSync(file5).equals(Buffer.from('')));
Expand Down

0 comments on commit d7b162c

Please sign in to comment.