Skip to content

Commit

Permalink
lib: add case of no argument to ERR_INVALID_ARG_VALUE in errors.js
Browse files Browse the repository at this point in the history
ERR_INVALID_ARG_VALUE is an error for wrong arguments given to the
function. But calling a function without argument should also generate
a sensible error message. For no argument case, parameter 'value'
should be passed with zero value and the error message is generated.

In readSync(fd, buffer, offset, length, position), triggers
validateOffsetLengthRead() in lib/internal/fs/utils.js for validation.
When buffer is empty, a weird message is generated "The value of offset
is out of range.It must be >= 0 && <= 0. Received 0". There should be a
special case when buffer is empty or bufferLength is zero, which should
trigger ERR_INVALID_ARG_VALUE error.

Fixes: nodejs#21193
  • Loading branch information
AdityaSrivast committed Jun 11, 2018
1 parent f2f5c0c commit e05f49c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
2 changes: 1 addition & 1 deletion lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ E('ERR_INVALID_ARG_VALUE', (name, value, reason = 'is invalid') => {
if (inspected.length > 128) {
inspected = `${inspected.slice(0, 128)}...`;
} else if (inspected === '0') {
inspected = 'No value.';
inspected = 'no value.';
}
return `The argument '${name}' ${reason}. Received ${inspected}`;
}, TypeError, RangeError);
Expand Down
14 changes: 9 additions & 5 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,15 @@ function validateBuffer(buffer) {

function validateOffsetLengthRead(offset, length, bufferLength) {
let err;
if (bufferLength === 0) {
err = new ERR_INVALID_ARG_VALUE('buffer', 0,
"is empty and can't be written.");
} else if (offset < 0 || offset >= bufferLength) {
err = new ERR_OUT_OF_RANGE('offset', `>= 0 && <= ${bufferLength}`, offset);

if (offset < 0 || offset >= bufferLength) {
if (bufferLength === 0) {
throw ERR_INVALID_ARG_VALUE('buffer', 0,
'is empty and can\'t be written');
} else {
err = new ERR_OUT_OF_RANGE('offset',
`>= 0 && <= ${bufferLength}`, offset);
}
} else if (length < 0 || offset + length > bufferLength) {
err = new ERR_OUT_OF_RANGE('length',
`>= 0 && <= ${bufferLength - offset}`, length);
Expand Down
28 changes: 9 additions & 19 deletions test/parallel/test-internal-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,15 @@ errors.E('TEST_ERROR_2', (a, b) => `${a} ${b}`, Error);
assert.strictEqual(err.code, 'TEST_ERROR_2');
}

// {
// assert.throws(() => new errors.codes.TEST_ERROR_1(){
// message: 'Code: TEST_ERROR_1; The provided arguments ' +
// 'length (0) does not match the required ones (1).'
// }
// );
// }

assert.expectsError(() => {
const fs = require('fs');
const file = '/home/aditya/node/test/nodetest/testcodes';
const keyfile = fs.openSync(file, 'r');
const string = new Uint8Array();
const num = fs.readSync(keyfile, string, 0, 10, 0);
console.log(num);
}, { code: 'ERR_OUT_OF_RANGE',
type: RangeError,
message: 'Just Testing'
});
{
assert.throws(
() => new errors.codes.TEST_ERROR_1(),
{
message: 'Code: TEST_ERROR_1; The provided arguments ' +
'length (0) does not match the required ones (1).'
}
);
}

// Tests for common.expectsError
common.expectsError(() => {
Expand Down

0 comments on commit e05f49c

Please sign in to comment.