Skip to content

Commit 9e0f771

Browse files
starkwangjasnell
authored andcommitted
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: #14975 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 52fe762 commit 9e0f771

9 files changed

+117
-100
lines changed

lib/buffer.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ Buffer.from = function from(value, encodingOrOffset, length) {
224224
throw new errors.TypeError(
225225
'ERR_INVALID_ARG_TYPE',
226226
'first argument',
227-
['string', 'buffer', 'arrayBuffer', 'array', 'array-like object']
227+
['string', 'buffer', 'arrayBuffer', 'array', 'array-like object'],
228+
value
228229
);
229230
};
230231

@@ -507,7 +508,8 @@ function byteLength(string, encoding) {
507508
}
508509

509510
throw new errors.TypeError(
510-
'ERR_INVALID_ARG_TYPE', 'string', ['string', 'buffer', 'arrayBuffer']
511+
'ERR_INVALID_ARG_TYPE', 'string',
512+
['string', 'buffer', 'arrayBuffer'], string
511513
);
512514
}
513515

@@ -668,7 +670,8 @@ Buffer.prototype.toString = function toString(encoding, start, end) {
668670
Buffer.prototype.equals = function equals(b) {
669671
if (!isUint8Array(b)) {
670672
throw new errors.TypeError(
671-
'ERR_INVALID_ARG_TYPE', 'otherBuffer', ['buffer', 'uint8Array']
673+
'ERR_INVALID_ARG_TYPE', 'otherBuffer',
674+
['buffer', 'uint8Array'], b
672675
);
673676
}
674677
if (this === b)
@@ -696,7 +699,8 @@ Buffer.prototype.compare = function compare(target,
696699
thisEnd) {
697700
if (!isUint8Array(target)) {
698701
throw new errors.TypeError(
699-
'ERR_INVALID_ARG_TYPE', 'target', ['buffer', 'uint8Array']
702+
'ERR_INVALID_ARG_TYPE', 'target',
703+
['buffer', 'uint8Array'], target
700704
);
701705
}
702706
if (arguments.length === 1)
@@ -778,7 +782,8 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
778782
}
779783

780784
throw new errors.TypeError(
781-
'ERR_INVALID_ARG_TYPE', 'val', ['string', 'buffer', 'uint8Array']
785+
'ERR_INVALID_ARG_TYPE', 'value',
786+
['string', 'buffer', 'uint8Array'], val
782787
);
783788
}
784789

@@ -847,7 +852,8 @@ Buffer.prototype.fill = function fill(val, start, end, encoding) {
847852
}
848853

849854
if (encoding !== undefined && typeof encoding !== 'string') {
850-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'encoding', 'string');
855+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'encoding',
856+
'string', encoding);
851857
}
852858
var normalizedEncoding = normalizeEncoding(encoding);
853859
if (normalizedEncoding === undefined) {

test/parallel/test-buffer-bytelength.js

+16-11
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,22 @@ const assert = require('assert');
55
const SlowBuffer = require('buffer').SlowBuffer;
66
const vm = require('vm');
77

8-
// coerce values to string
9-
const errMsg = common.expectsError({
10-
code: 'ERR_INVALID_ARG_TYPE',
11-
type: TypeError,
12-
message: 'The "string" argument must be one of type string, ' +
13-
'buffer, or arrayBuffer'
14-
}, 4);
15-
assert.throws(() => { Buffer.byteLength(32, 'latin1'); }, errMsg);
16-
assert.throws(() => { Buffer.byteLength(NaN, 'utf8'); }, errMsg);
17-
assert.throws(() => { Buffer.byteLength({}, 'latin1'); }, errMsg);
18-
assert.throws(() => { Buffer.byteLength(); }, errMsg);
8+
[
9+
[32, 'latin1'],
10+
[NaN, 'utf8'],
11+
[{}, 'latin1'],
12+
[]
13+
].forEach((args) => {
14+
common.expectsError(
15+
() => Buffer.byteLength(...args),
16+
{
17+
code: 'ERR_INVALID_ARG_TYPE',
18+
type: TypeError,
19+
message: 'The "string" argument must be one of type string, ' +
20+
`buffer, or arrayBuffer. Received type ${typeof args[0]}`
21+
}
22+
);
23+
});
1924

2025
assert.strictEqual(Buffer.byteLength('', undefined, true), -1);
2126

test/parallel/test-buffer-compare-offset.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,6 @@ assert.throws(() => a.compare(b, -Infinity, Infinity), oor);
6969
assert.throws(() => a.compare(), common.expectsError({
7070
code: 'ERR_INVALID_ARG_TYPE',
7171
type: TypeError,
72-
message: 'The "target" argument must be one of type buffer or uint8Array'
72+
message: 'The "target" argument must be one of ' +
73+
'type buffer or uint8Array. Received type undefined'
7374
}));

test/parallel/test-buffer-compare.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@ assert.throws(() => Buffer.compare('abc', Buffer.alloc(1)), errMsg);
4141
assert.throws(() => Buffer.alloc(1).compare('abc'), common.expectsError({
4242
code: 'ERR_INVALID_ARG_TYPE',
4343
type: TypeError,
44-
message: 'The "target" argument must be one of type buffer or uint8Array'
44+
message: 'The "target" argument must be one of ' +
45+
'type buffer or uint8Array. Received type string'
4546
}));

test/parallel/test-buffer-equals.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ assert.ok(!d.equals(e));
1414
assert.ok(d.equals(d));
1515
assert.ok(d.equals(new Uint8Array([0x61, 0x62, 0x63, 0x64, 0x65])));
1616

17-
assert.throws(() => Buffer.alloc(1).equals('abc'),
18-
common.expectsError({
19-
code: 'ERR_INVALID_ARG_TYPE',
20-
type: TypeError,
21-
message: 'The "otherBuffer" argument must be one of type ' +
22-
'buffer or uint8Array'
23-
}));
17+
common.expectsError(
18+
() => Buffer.alloc(1).equals('abc'),
19+
{
20+
code: 'ERR_INVALID_ARG_TYPE',
21+
type: TypeError,
22+
message: 'The "otherBuffer" argument must be one of type ' +
23+
'buffer or uint8Array. Received type string'
24+
}
25+
);

test/parallel/test-buffer-fill.js

+35-31
Original file line numberDiff line numberDiff line change
@@ -192,45 +192,49 @@ deepStrictEqualValues(genBuffer(4, [hexBufFill, 1, -1]), [0, 0, 0, 0]);
192192

193193

194194
// Check exceptions
195-
assert.throws(
196-
() => buf1.fill(0, -1),
197-
common.expectsError({ code: 'ERR_INDEX_OUT_OF_RANGE' }));
198-
assert.throws(
199-
() => buf1.fill(0, 0, buf1.length + 1),
200-
common.expectsError({ code: 'ERR_INDEX_OUT_OF_RANGE' }));
201-
assert.throws(
202-
() => buf1.fill('', -1),
203-
common.expectsError({ code: 'ERR_INDEX_OUT_OF_RANGE' }));
204-
assert.throws(
205-
() => buf1.fill('', 0, buf1.length + 1),
206-
common.expectsError({ code: 'ERR_INDEX_OUT_OF_RANGE' }));
207-
assert.throws(
195+
[
196+
[0, -1],
197+
[0, 0, buf1.length + 1],
198+
['', -1],
199+
['', 0, buf1.length + 1]
200+
].forEach((args) => {
201+
common.expectsError(
202+
() => buf1.fill(...args),
203+
{ code: 'ERR_INDEX_OUT_OF_RANGE' }
204+
);
205+
});
206+
207+
common.expectsError(
208208
() => buf1.fill('a', 0, buf1.length, 'node rocks!'),
209-
common.expectsError({
209+
{
210210
code: 'ERR_UNKNOWN_ENCODING',
211211
type: TypeError,
212212
message: 'Unknown encoding: node rocks!'
213-
}));
214-
assert.throws(
215-
() => buf1.fill('a', 0, 0, NaN),
216-
common.expectsError({
217-
code: 'ERR_INVALID_ARG_TYPE',
218-
message: 'The "encoding" argument must be of type string'
219-
}));
220-
assert.throws(
221-
() => buf1.fill('a', 0, 0, null),
222-
common.expectsError({
223-
code: 'ERR_INVALID_ARG_TYPE',
224-
message: 'The "encoding" argument must be of type string'
225-
}));
226-
assert.throws(
213+
}
214+
);
215+
216+
[
217+
['a', 0, 0, NaN],
218+
['a', 0, 0, null]
219+
].forEach((args) => {
220+
common.expectsError(
221+
() => buf1.fill(...args),
222+
{
223+
code: 'ERR_INVALID_ARG_TYPE',
224+
message: 'The "encoding" argument must be of type ' +
225+
`string. Received type ${args[3] === null ? 'null' : typeof args[3]}`
226+
}
227+
);
228+
});
229+
230+
common.expectsError(
227231
() => buf1.fill('a', 0, 0, 'foo'),
228-
common.expectsError({
232+
{
229233
code: 'ERR_UNKNOWN_ENCODING',
230234
type: TypeError,
231235
message: 'Unknown encoding: foo'
232-
}));
233-
236+
}
237+
);
234238

235239
function genBuffer(size, args) {
236240
const b = Buffer.allocUnsafe(size);

test/parallel/test-buffer-from.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,19 @@ deepStrictEqual(
3636
);
3737

3838
[
39-
{},
40-
new Boolean(true),
41-
{ valueOf() { return null; } },
42-
{ valueOf() { return undefined; } },
43-
{ valueOf: null },
44-
Object.create(null)
45-
].forEach((input) => {
39+
[{}, 'object'],
40+
[new Boolean(true), 'boolean'],
41+
[{ valueOf() { return null; } }, 'object'],
42+
[{ valueOf() { return undefined; } }, 'object'],
43+
[{ valueOf: null }, 'object'],
44+
[Object.create(null), 'object']
45+
].forEach(([input, actualType]) => {
4646
const err = common.expectsError({
4747
code: 'ERR_INVALID_ARG_TYPE',
4848
type: TypeError,
4949
message: 'The first argument must be one of type string, buffer, ' +
50-
'arrayBuffer, array, or array-like object'
50+
'arrayBuffer, array, or array-like object. Received ' +
51+
`type ${actualType}`
5152
});
5253
throws(() => Buffer.from(input), err);
5354
});

test/parallel/test-buffer-includes.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ assert(twoByteString.includes(
148148
assert(!twoByteString.includes('\u03a3', -2, 'ucs2'));
149149

150150
const mixedByteStringUcs2 =
151-
Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395', 'ucs2');
151+
Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395', 'ucs2');
152152
assert(mixedByteStringUcs2.includes('bc', 0, 'ucs2'));
153153
assert(mixedByteStringUcs2.includes('\u03a3', 0, 'ucs2'));
154154
assert(!mixedByteStringUcs2.includes('\u0396', 0, 'ucs2'));
@@ -261,7 +261,7 @@ for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
261261
const length = lengths[lengthIndex];
262262

263263
const patternBufferUcs2 =
264-
allCharsBufferUcs2.slice(index, index + length);
264+
allCharsBufferUcs2.slice(index, index + length);
265265
assert.ok(
266266
allCharsBufferUcs2.includes(patternBufferUcs2, 0, 'ucs2'));
267267

@@ -271,21 +271,21 @@ for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
271271
}
272272
}
273273

274-
const expectedError = common.expectsError({
275-
code: 'ERR_INVALID_ARG_TYPE',
276-
type: TypeError,
277-
message: 'The "val" argument must be one of type ' +
278-
'string, buffer, or uint8Array'
279-
}, 3);
280-
assert.throws(() => {
281-
b.includes(() => {});
282-
}, expectedError);
283-
assert.throws(() => {
284-
b.includes({});
285-
}, expectedError);
286-
assert.throws(() => {
287-
b.includes([]);
288-
}, expectedError);
274+
[
275+
() => { },
276+
{},
277+
[]
278+
].forEach((val) => {
279+
common.expectsError(
280+
() => b.includes(val),
281+
{
282+
code: 'ERR_INVALID_ARG_TYPE',
283+
type: TypeError,
284+
message: 'The "value" argument must be one of type string, ' +
285+
`buffer, or uint8Array. Received type ${typeof val}`
286+
}
287+
);
288+
});
289289

290290
// test truncation of Number arguments to uint8
291291
{

test/parallel/test-buffer-indexof.js

+15-18
Original file line numberDiff line numberDiff line change
@@ -344,24 +344,21 @@ assert.strictEqual(Buffer.from('aaaaa').indexOf('b', 'ucs2'), -1);
344344
}
345345
}
346346

347-
const argumentExpected = common.expectsError({
348-
code: 'ERR_INVALID_ARG_TYPE',
349-
type: TypeError,
350-
message: 'The "val" argument must be one of type ' +
351-
'string, buffer, or uint8Array'
352-
}, 3);
353-
354-
assert.throws(() => {
355-
b.indexOf(() => { });
356-
}, argumentExpected);
357-
358-
assert.throws(() => {
359-
b.indexOf({});
360-
}, argumentExpected);
361-
362-
assert.throws(() => {
363-
b.indexOf([]);
364-
}, argumentExpected);
347+
[
348+
() => {},
349+
{},
350+
[]
351+
].forEach((val) => {
352+
common.expectsError(
353+
() => b.indexOf(val),
354+
{
355+
code: 'ERR_INVALID_ARG_TYPE',
356+
type: TypeError,
357+
message: 'The "value" argument must be one of type string, ' +
358+
`buffer, or uint8Array. Received type ${typeof val}`
359+
}
360+
);
361+
});
365362

366363
// Test weird offset arguments.
367364
// The following offsets coerce to NaN or 0, searching the whole Buffer

0 commit comments

Comments
 (0)