buffer: improve error messages
Some errors in buffer module losed some arguments or received wrong arguments when they were created. This PR added these losing arguments and fixed the wrong arguments. PR-URL: https://github.com/nodejs/node/pull/14975 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
parent
52fe762606
commit
9e0f771224
@ -224,7 +224,8 @@ Buffer.from = function from(value, encodingOrOffset, length) {
|
|||||||
throw new errors.TypeError(
|
throw new errors.TypeError(
|
||||||
'ERR_INVALID_ARG_TYPE',
|
'ERR_INVALID_ARG_TYPE',
|
||||||
'first argument',
|
'first argument',
|
||||||
['string', 'buffer', 'arrayBuffer', 'array', 'array-like object']
|
['string', 'buffer', 'arrayBuffer', 'array', 'array-like object'],
|
||||||
|
value
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -507,7 +508,8 @@ function byteLength(string, encoding) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
throw new errors.TypeError(
|
throw new errors.TypeError(
|
||||||
'ERR_INVALID_ARG_TYPE', 'string', ['string', 'buffer', 'arrayBuffer']
|
'ERR_INVALID_ARG_TYPE', 'string',
|
||||||
|
['string', 'buffer', 'arrayBuffer'], string
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -668,7 +670,8 @@ Buffer.prototype.toString = function toString(encoding, start, end) {
|
|||||||
Buffer.prototype.equals = function equals(b) {
|
Buffer.prototype.equals = function equals(b) {
|
||||||
if (!isUint8Array(b)) {
|
if (!isUint8Array(b)) {
|
||||||
throw new errors.TypeError(
|
throw new errors.TypeError(
|
||||||
'ERR_INVALID_ARG_TYPE', 'otherBuffer', ['buffer', 'uint8Array']
|
'ERR_INVALID_ARG_TYPE', 'otherBuffer',
|
||||||
|
['buffer', 'uint8Array'], b
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (this === b)
|
if (this === b)
|
||||||
@ -696,7 +699,8 @@ Buffer.prototype.compare = function compare(target,
|
|||||||
thisEnd) {
|
thisEnd) {
|
||||||
if (!isUint8Array(target)) {
|
if (!isUint8Array(target)) {
|
||||||
throw new errors.TypeError(
|
throw new errors.TypeError(
|
||||||
'ERR_INVALID_ARG_TYPE', 'target', ['buffer', 'uint8Array']
|
'ERR_INVALID_ARG_TYPE', 'target',
|
||||||
|
['buffer', 'uint8Array'], target
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (arguments.length === 1)
|
if (arguments.length === 1)
|
||||||
@ -778,7 +782,8 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
throw new errors.TypeError(
|
throw new errors.TypeError(
|
||||||
'ERR_INVALID_ARG_TYPE', 'val', ['string', 'buffer', 'uint8Array']
|
'ERR_INVALID_ARG_TYPE', 'value',
|
||||||
|
['string', 'buffer', 'uint8Array'], val
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -847,7 +852,8 @@ Buffer.prototype.fill = function fill(val, start, end, encoding) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (encoding !== undefined && typeof encoding !== 'string') {
|
if (encoding !== undefined && typeof encoding !== 'string') {
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'encoding', 'string');
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'encoding',
|
||||||
|
'string', encoding);
|
||||||
}
|
}
|
||||||
var normalizedEncoding = normalizeEncoding(encoding);
|
var normalizedEncoding = normalizeEncoding(encoding);
|
||||||
if (normalizedEncoding === undefined) {
|
if (normalizedEncoding === undefined) {
|
||||||
|
@ -5,17 +5,22 @@ const assert = require('assert');
|
|||||||
const SlowBuffer = require('buffer').SlowBuffer;
|
const SlowBuffer = require('buffer').SlowBuffer;
|
||||||
const vm = require('vm');
|
const vm = require('vm');
|
||||||
|
|
||||||
// coerce values to string
|
[
|
||||||
const errMsg = common.expectsError({
|
[32, 'latin1'],
|
||||||
code: 'ERR_INVALID_ARG_TYPE',
|
[NaN, 'utf8'],
|
||||||
type: TypeError,
|
[{}, 'latin1'],
|
||||||
message: 'The "string" argument must be one of type string, ' +
|
[]
|
||||||
'buffer, or arrayBuffer'
|
].forEach((args) => {
|
||||||
}, 4);
|
common.expectsError(
|
||||||
assert.throws(() => { Buffer.byteLength(32, 'latin1'); }, errMsg);
|
() => Buffer.byteLength(...args),
|
||||||
assert.throws(() => { Buffer.byteLength(NaN, 'utf8'); }, errMsg);
|
{
|
||||||
assert.throws(() => { Buffer.byteLength({}, 'latin1'); }, errMsg);
|
code: 'ERR_INVALID_ARG_TYPE',
|
||||||
assert.throws(() => { Buffer.byteLength(); }, errMsg);
|
type: TypeError,
|
||||||
|
message: 'The "string" argument must be one of type string, ' +
|
||||||
|
`buffer, or arrayBuffer. Received type ${typeof args[0]}`
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
assert.strictEqual(Buffer.byteLength('', undefined, true), -1);
|
assert.strictEqual(Buffer.byteLength('', undefined, true), -1);
|
||||||
|
|
||||||
|
@ -69,5 +69,6 @@ assert.throws(() => a.compare(b, -Infinity, Infinity), oor);
|
|||||||
assert.throws(() => a.compare(), common.expectsError({
|
assert.throws(() => a.compare(), common.expectsError({
|
||||||
code: 'ERR_INVALID_ARG_TYPE',
|
code: 'ERR_INVALID_ARG_TYPE',
|
||||||
type: TypeError,
|
type: TypeError,
|
||||||
message: 'The "target" argument must be one of type buffer or uint8Array'
|
message: 'The "target" argument must be one of ' +
|
||||||
|
'type buffer or uint8Array. Received type undefined'
|
||||||
}));
|
}));
|
||||||
|
@ -41,5 +41,6 @@ assert.throws(() => Buffer.compare('abc', Buffer.alloc(1)), errMsg);
|
|||||||
assert.throws(() => Buffer.alloc(1).compare('abc'), common.expectsError({
|
assert.throws(() => Buffer.alloc(1).compare('abc'), common.expectsError({
|
||||||
code: 'ERR_INVALID_ARG_TYPE',
|
code: 'ERR_INVALID_ARG_TYPE',
|
||||||
type: TypeError,
|
type: TypeError,
|
||||||
message: 'The "target" argument must be one of type buffer or uint8Array'
|
message: 'The "target" argument must be one of ' +
|
||||||
|
'type buffer or uint8Array. Received type string'
|
||||||
}));
|
}));
|
||||||
|
@ -14,10 +14,12 @@ assert.ok(!d.equals(e));
|
|||||||
assert.ok(d.equals(d));
|
assert.ok(d.equals(d));
|
||||||
assert.ok(d.equals(new Uint8Array([0x61, 0x62, 0x63, 0x64, 0x65])));
|
assert.ok(d.equals(new Uint8Array([0x61, 0x62, 0x63, 0x64, 0x65])));
|
||||||
|
|
||||||
assert.throws(() => Buffer.alloc(1).equals('abc'),
|
common.expectsError(
|
||||||
common.expectsError({
|
() => Buffer.alloc(1).equals('abc'),
|
||||||
code: 'ERR_INVALID_ARG_TYPE',
|
{
|
||||||
type: TypeError,
|
code: 'ERR_INVALID_ARG_TYPE',
|
||||||
message: 'The "otherBuffer" argument must be one of type ' +
|
type: TypeError,
|
||||||
'buffer or uint8Array'
|
message: 'The "otherBuffer" argument must be one of type ' +
|
||||||
}));
|
'buffer or uint8Array. Received type string'
|
||||||
|
}
|
||||||
|
);
|
||||||
|
@ -192,45 +192,49 @@ deepStrictEqualValues(genBuffer(4, [hexBufFill, 1, -1]), [0, 0, 0, 0]);
|
|||||||
|
|
||||||
|
|
||||||
// Check exceptions
|
// Check exceptions
|
||||||
assert.throws(
|
[
|
||||||
() => buf1.fill(0, -1),
|
[0, -1],
|
||||||
common.expectsError({ code: 'ERR_INDEX_OUT_OF_RANGE' }));
|
[0, 0, buf1.length + 1],
|
||||||
assert.throws(
|
['', -1],
|
||||||
() => buf1.fill(0, 0, buf1.length + 1),
|
['', 0, buf1.length + 1]
|
||||||
common.expectsError({ code: 'ERR_INDEX_OUT_OF_RANGE' }));
|
].forEach((args) => {
|
||||||
assert.throws(
|
common.expectsError(
|
||||||
() => buf1.fill('', -1),
|
() => buf1.fill(...args),
|
||||||
common.expectsError({ code: 'ERR_INDEX_OUT_OF_RANGE' }));
|
{ code: 'ERR_INDEX_OUT_OF_RANGE' }
|
||||||
assert.throws(
|
);
|
||||||
() => buf1.fill('', 0, buf1.length + 1),
|
});
|
||||||
common.expectsError({ code: 'ERR_INDEX_OUT_OF_RANGE' }));
|
|
||||||
assert.throws(
|
common.expectsError(
|
||||||
() => buf1.fill('a', 0, buf1.length, 'node rocks!'),
|
() => buf1.fill('a', 0, buf1.length, 'node rocks!'),
|
||||||
common.expectsError({
|
{
|
||||||
code: 'ERR_UNKNOWN_ENCODING',
|
code: 'ERR_UNKNOWN_ENCODING',
|
||||||
type: TypeError,
|
type: TypeError,
|
||||||
message: 'Unknown encoding: node rocks!'
|
message: 'Unknown encoding: node rocks!'
|
||||||
}));
|
}
|
||||||
assert.throws(
|
);
|
||||||
() => buf1.fill('a', 0, 0, NaN),
|
|
||||||
common.expectsError({
|
[
|
||||||
code: 'ERR_INVALID_ARG_TYPE',
|
['a', 0, 0, NaN],
|
||||||
message: 'The "encoding" argument must be of type string'
|
['a', 0, 0, null]
|
||||||
}));
|
].forEach((args) => {
|
||||||
assert.throws(
|
common.expectsError(
|
||||||
() => buf1.fill('a', 0, 0, null),
|
() => buf1.fill(...args),
|
||||||
common.expectsError({
|
{
|
||||||
code: 'ERR_INVALID_ARG_TYPE',
|
code: 'ERR_INVALID_ARG_TYPE',
|
||||||
message: 'The "encoding" argument must be of type string'
|
message: 'The "encoding" argument must be of type ' +
|
||||||
}));
|
`string. Received type ${args[3] === null ? 'null' : typeof args[3]}`
|
||||||
assert.throws(
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
common.expectsError(
|
||||||
() => buf1.fill('a', 0, 0, 'foo'),
|
() => buf1.fill('a', 0, 0, 'foo'),
|
||||||
common.expectsError({
|
{
|
||||||
code: 'ERR_UNKNOWN_ENCODING',
|
code: 'ERR_UNKNOWN_ENCODING',
|
||||||
type: TypeError,
|
type: TypeError,
|
||||||
message: 'Unknown encoding: foo'
|
message: 'Unknown encoding: foo'
|
||||||
}));
|
}
|
||||||
|
);
|
||||||
|
|
||||||
function genBuffer(size, args) {
|
function genBuffer(size, args) {
|
||||||
const b = Buffer.allocUnsafe(size);
|
const b = Buffer.allocUnsafe(size);
|
||||||
|
@ -36,18 +36,19 @@ deepStrictEqual(
|
|||||||
);
|
);
|
||||||
|
|
||||||
[
|
[
|
||||||
{},
|
[{}, 'object'],
|
||||||
new Boolean(true),
|
[new Boolean(true), 'boolean'],
|
||||||
{ valueOf() { return null; } },
|
[{ valueOf() { return null; } }, 'object'],
|
||||||
{ valueOf() { return undefined; } },
|
[{ valueOf() { return undefined; } }, 'object'],
|
||||||
{ valueOf: null },
|
[{ valueOf: null }, 'object'],
|
||||||
Object.create(null)
|
[Object.create(null), 'object']
|
||||||
].forEach((input) => {
|
].forEach(([input, actualType]) => {
|
||||||
const err = common.expectsError({
|
const err = common.expectsError({
|
||||||
code: 'ERR_INVALID_ARG_TYPE',
|
code: 'ERR_INVALID_ARG_TYPE',
|
||||||
type: TypeError,
|
type: TypeError,
|
||||||
message: 'The first argument must be one of type string, buffer, ' +
|
message: 'The first argument must be one of type string, buffer, ' +
|
||||||
'arrayBuffer, array, or array-like object'
|
'arrayBuffer, array, or array-like object. Received ' +
|
||||||
|
`type ${actualType}`
|
||||||
});
|
});
|
||||||
throws(() => Buffer.from(input), err);
|
throws(() => Buffer.from(input), err);
|
||||||
});
|
});
|
||||||
|
@ -148,7 +148,7 @@ assert(twoByteString.includes(
|
|||||||
assert(!twoByteString.includes('\u03a3', -2, 'ucs2'));
|
assert(!twoByteString.includes('\u03a3', -2, 'ucs2'));
|
||||||
|
|
||||||
const mixedByteStringUcs2 =
|
const mixedByteStringUcs2 =
|
||||||
Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395', 'ucs2');
|
Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395', 'ucs2');
|
||||||
assert(mixedByteStringUcs2.includes('bc', 0, 'ucs2'));
|
assert(mixedByteStringUcs2.includes('bc', 0, 'ucs2'));
|
||||||
assert(mixedByteStringUcs2.includes('\u03a3', 0, 'ucs2'));
|
assert(mixedByteStringUcs2.includes('\u03a3', 0, 'ucs2'));
|
||||||
assert(!mixedByteStringUcs2.includes('\u0396', 0, 'ucs2'));
|
assert(!mixedByteStringUcs2.includes('\u0396', 0, 'ucs2'));
|
||||||
@ -261,7 +261,7 @@ for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
|
|||||||
const length = lengths[lengthIndex];
|
const length = lengths[lengthIndex];
|
||||||
|
|
||||||
const patternBufferUcs2 =
|
const patternBufferUcs2 =
|
||||||
allCharsBufferUcs2.slice(index, index + length);
|
allCharsBufferUcs2.slice(index, index + length);
|
||||||
assert.ok(
|
assert.ok(
|
||||||
allCharsBufferUcs2.includes(patternBufferUcs2, 0, 'ucs2'));
|
allCharsBufferUcs2.includes(patternBufferUcs2, 0, 'ucs2'));
|
||||||
|
|
||||||
@ -271,21 +271,21 @@ for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const expectedError = common.expectsError({
|
[
|
||||||
code: 'ERR_INVALID_ARG_TYPE',
|
() => { },
|
||||||
type: TypeError,
|
{},
|
||||||
message: 'The "val" argument must be one of type ' +
|
[]
|
||||||
'string, buffer, or uint8Array'
|
].forEach((val) => {
|
||||||
}, 3);
|
common.expectsError(
|
||||||
assert.throws(() => {
|
() => b.includes(val),
|
||||||
b.includes(() => {});
|
{
|
||||||
}, expectedError);
|
code: 'ERR_INVALID_ARG_TYPE',
|
||||||
assert.throws(() => {
|
type: TypeError,
|
||||||
b.includes({});
|
message: 'The "value" argument must be one of type string, ' +
|
||||||
}, expectedError);
|
`buffer, or uint8Array. Received type ${typeof val}`
|
||||||
assert.throws(() => {
|
}
|
||||||
b.includes([]);
|
);
|
||||||
}, expectedError);
|
});
|
||||||
|
|
||||||
// test truncation of Number arguments to uint8
|
// test truncation of Number arguments to uint8
|
||||||
{
|
{
|
||||||
|
@ -344,24 +344,21 @@ assert.strictEqual(Buffer.from('aaaaa').indexOf('b', 'ucs2'), -1);
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const argumentExpected = common.expectsError({
|
[
|
||||||
code: 'ERR_INVALID_ARG_TYPE',
|
() => {},
|
||||||
type: TypeError,
|
{},
|
||||||
message: 'The "val" argument must be one of type ' +
|
[]
|
||||||
'string, buffer, or uint8Array'
|
].forEach((val) => {
|
||||||
}, 3);
|
common.expectsError(
|
||||||
|
() => b.indexOf(val),
|
||||||
assert.throws(() => {
|
{
|
||||||
b.indexOf(() => { });
|
code: 'ERR_INVALID_ARG_TYPE',
|
||||||
}, argumentExpected);
|
type: TypeError,
|
||||||
|
message: 'The "value" argument must be one of type string, ' +
|
||||||
assert.throws(() => {
|
`buffer, or uint8Array. Received type ${typeof val}`
|
||||||
b.indexOf({});
|
}
|
||||||
}, argumentExpected);
|
);
|
||||||
|
});
|
||||||
assert.throws(() => {
|
|
||||||
b.indexOf([]);
|
|
||||||
}, argumentExpected);
|
|
||||||
|
|
||||||
// Test weird offset arguments.
|
// Test weird offset arguments.
|
||||||
// The following offsets coerce to NaN or 0, searching the whole Buffer
|
// The following offsets coerce to NaN or 0, searching the whole Buffer
|
||||||
|
Loading…
x
Reference in New Issue
Block a user