Skip to content

Commit 8c8ed64

Browse files
committed
make all error messages the same
1 parent d5925eb commit 8c8ed64

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

src/parse.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@ export function parse(serialized) {
1616

1717
if (typeof parsed === 'number') return hydrate(parsed, true);
1818

19-
if (!Array.isArray(parsed)) {
20-
throw new SyntaxError(
21-
`Expected array, got ${parsed === null ? 'null' : typeof parsed}`
22-
);
19+
if (!Array.isArray(parsed) || parsed.length === 0) {
20+
throw new SyntaxError('Invalid input');
2321
}
2422

2523
const values = /** @type {any[]} */ (parsed);
26-
if (values.length === 0) throw new SyntaxError(`Unexpected empty array`);
2724

2825
const hydrated = Array(values.length);
2926

@@ -35,7 +32,7 @@ export function parse(serialized) {
3532
if (index === NEGATIVE_INFINITY) return -Infinity;
3633
if (index === NEGATIVE_ZERO) return -0;
3734

38-
if (standalone) throw new SyntaxError(`Unexpected number ${index}`);
35+
if (standalone) throw new SyntaxError(`Invalid input`);
3936

4037
if (index in hydrated) return hydrated[index];
4138

test/test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -404,37 +404,37 @@ const invalid = [
404404
{
405405
name: 'hole',
406406
json: '-2',
407-
message: 'Unexpected number -2'
407+
message: 'Invalid input'
408408
},
409409
{
410410
name: 'string',
411411
json: '"hello"',
412-
message: 'Expected array, got string'
412+
message: 'Invalid input'
413413
},
414414
{
415415
name: 'number',
416416
json: '42',
417-
message: 'Unexpected number 42'
417+
message: 'Invalid input'
418418
},
419419
{
420420
name: 'boolean',
421421
json: 'true',
422-
message: 'Expected array, got boolean'
422+
message: 'Invalid input'
423423
},
424424
{
425425
name: 'null',
426426
json: 'null',
427-
message: 'Expected array, got null'
427+
message: 'Invalid input'
428428
},
429429
{
430430
name: 'object',
431431
json: '{}',
432-
message: 'Expected array, got object'
432+
message: 'Invalid input'
433433
},
434434
{
435435
name: 'empty array',
436436
json: '[]',
437-
message: 'Unexpected empty array'
437+
message: 'Invalid input'
438438
}
439439
];
440440

0 commit comments

Comments
 (0)