refactor(csv): align additional error messages (#5796)

Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
This commit is contained in:
Ian Bull 2024-08-26 01:11:55 -04:00 committed by GitHub
parent 7314eec8d4
commit 9a8b3a5d4e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 49 additions and 41 deletions

View File

@ -74,7 +74,7 @@ export async function parseRecord(
}
if (options.separator === undefined) {
throw new TypeError("Separator is required");
throw new TypeError("Cannot parse record: separator is required");
}
let line = fullLine;
@ -211,18 +211,22 @@ export function createBareQuoteErrorMessage(
zeroBasedLine: number,
zeroBasedColumn: number,
) {
return `record on line ${zeroBasedRecordStartLine + 1}; parse error on line ${
zeroBasedLine + 1
}, column ${zeroBasedColumn + 1}: bare " in non-quoted-field`;
return `Syntax error on line ${
zeroBasedRecordStartLine + 1
}; parse error on line ${zeroBasedLine + 1}, column ${
zeroBasedColumn + 1
}: bare " in non-quoted-field`;
}
export function createQuoteErrorMessage(
zeroBasedRecordStartLine: number,
zeroBasedLine: number,
zeroBasedColumn: number,
) {
return `record on line ${zeroBasedRecordStartLine + 1}; parse error on line ${
zeroBasedLine + 1
}, column ${zeroBasedColumn + 1}: extraneous or missing " in quoted-field`;
return `Syntax error on line ${
zeroBasedRecordStartLine + 1
}; parse error on line ${zeroBasedLine + 1}, column ${
zeroBasedColumn + 1
}: extraneous or missing " in quoted-field`;
}
export function convertRowToObject(
@ -232,9 +236,9 @@ export function convertRowToObject(
) {
if (row.length !== headers.length) {
throw new Error(
`record on line ${
`Syntax error on line ${
zeroBasedLine + 1
} has ${row.length} fields, but the header has ${headers.length} fields`,
}: The record has ${row.length} fields, but the header has ${headers.length} fields`,
);
}
const out: Record<string, unknown> = {};

View File

@ -222,7 +222,7 @@ class Parser {
INVALID_RUNE.includes(options.comment)) ||
options.separator === options.comment
) {
throw new Error("Invalid Delimiter");
throw new Error("Cannot parse input: invalid delimiter");
}
// The number of fields per record that is either inferred from the first
@ -261,7 +261,7 @@ class Parser {
if (lineResult.length > 0) {
if (typeof _nbFields === "number" && _nbFields !== lineResult.length) {
throw new SyntaxError(
`record on line ${lineIndex}: expected ${_nbFields} fields but got ${lineResult.length}`,
`Syntax error on line ${lineIndex}: expected ${_nbFields} fields but got ${lineResult.length}`,
);
}
result.push(lineResult);
@ -472,7 +472,7 @@ export function parse(input: string): string[][];
* assertThrows(
* () => parse(string, { fieldsPerRecord: 0 }),
* SyntaxError,
* "record on line 2: expected 2 fields but got 3",
* "Syntax error on line 2: expected 2 fields but got 3",
* );
* ```
*
@ -485,7 +485,7 @@ export function parse(input: string): string[][];
* assertThrows(
* () => parse(string, { fieldsPerRecord: 2 }),
* SyntaxError,
* "record on line 2: expected 2 fields but got 3",
* "Syntax error on line 2: expected 2 fields but got 3",
* );
* ```
*
@ -512,7 +512,9 @@ export function parse<const T extends ParseOptions>(
if (options.skipFirstRow) {
const head = r.shift();
if (head === undefined) throw new TypeError("Headers must be defined");
if (head === undefined) {
throw new TypeError("Cannot parse input: headers must be defined");
}
headers = head;
}

View File

@ -311,7 +311,7 @@ export type RowType<T> = T extends undefined ? string[]
* await assertRejects(
* () => reader.read(),
* SyntaxError,
* "record on line 2: expected 2 fields but got 3",
* "Syntax error on line 2: expected 2 fields but got 3",
* );
* ```
*
@ -333,7 +333,7 @@ export type RowType<T> = T extends undefined ? string[]
* await assertRejects(
* () => reader.read(),
* SyntaxError,
* "record on line 2: expected 2 fields but got 3",
* "Syntax error on line 2: expected 2 fields but got 3",
* );
* ```
*
@ -448,7 +448,7 @@ export class CsvParseStream<
record.length !== this.#fieldsPerRecord
) {
throw new SyntaxError(
`record on line ${
`Syntax error on line ${
this.#zeroBasedLineIndex + 1
}: expected ${this.#fieldsPerRecord} fields but got ${record.length}`,
);

View File

@ -45,7 +45,7 @@ Deno.test({
await assertRejects(
() => reader.read(),
SyntaxError,
`record on line 4; parse error on line 5, column 1: extraneous or missing " in quoted-field`,
`Syntax error on line 4; parse error on line 5, column 1: extraneous or missing " in quoted-field`,
);
},
});
@ -86,7 +86,7 @@ Deno.test({
separator: undefined,
error: {
klass: TypeError,
msg: "Separator is required",
msg: "Cannot parse record: separator is required",
},
},
{
@ -164,7 +164,7 @@ field"`,
fieldsPerRecord: 0,
error: {
klass: SyntaxError,
msg: "record on line 2: expected 3 fields but got 2",
msg: "Syntax error on line 2: expected 3 fields but got 2",
},
},
{
@ -174,7 +174,7 @@ field"`,
fieldsPerRecord: 3,
error: {
klass: SyntaxError,
msg: "record on line 2: expected 3 fields but got 2",
msg: "Syntax error on line 2: expected 3 fields but got 2",
},
},
{
@ -356,7 +356,8 @@ x,,,
columns: ["foo", "bar", "baz"],
error: {
klass: Error,
msg: "record on line 2 has 2 fields, but the header has 3 fields",
msg:
"Syntax error on line 2: The record has 2 fields, but the header has 3 fields",
},
},
{
@ -366,7 +367,8 @@ x,,,
columns: ["foo", "bar", "baz"],
error: {
klass: Error,
msg: "record on line 2 has 4 fields, but the header has 3 fields",
msg:
"Syntax error on line 2: The record has 4 fields, but the header has 3 fields",
},
},
{
@ -375,7 +377,7 @@ x,,,
error: {
klass: SyntaxError,
msg:
'record on line 1; parse error on line 1, column 3: bare " in non-quoted-field',
'Syntax error on line 1; parse error on line 1, column 3: bare " in non-quoted-field',
},
},
{
@ -384,7 +386,7 @@ x,,,
error: {
klass: SyntaxError,
msg:
'record on line 1; parse error on line 1, column 4: extraneous or missing " in quoted-field',
'Syntax error on line 1; parse error on line 1, column 4: extraneous or missing " in quoted-field',
},
},
{
@ -393,7 +395,7 @@ x,,,
error: {
klass: SyntaxError,
msg:
'record on line 1; parse error on line 3, column 2: extraneous or missing " in quoted-field',
'Syntax error on line 1; parse error on line 3, column 2: extraneous or missing " in quoted-field',
},
},
{
@ -402,7 +404,7 @@ x,,,
error: {
klass: SyntaxError,
msg:
'record on line 2; parse error on line 4, column 2: extraneous or missing " in quoted-field',
'Syntax error on line 2; parse error on line 4, column 2: extraneous or missing " in quoted-field',
},
},
{

View File

@ -294,7 +294,7 @@ Deno.test({
assertThrows(
() => parse(input, { fieldsPerRecord: 0 }),
SyntaxError,
"record on line 2: expected 3 fields but got 2",
"Syntax error on line 2: expected 3 fields but got 2",
);
},
});
@ -305,7 +305,7 @@ Deno.test({
assertThrows(
() => parse(input, { fieldsPerRecord: 2 }),
SyntaxError,
"record on line 1: expected 2 fields but got 3",
"Syntax error on line 1: expected 2 fields but got 3",
);
},
});
@ -434,7 +434,7 @@ c"d,e`;
assertThrows(
() => parse(input, { fieldsPerRecord: 2 }),
SyntaxError,
'record on line 1; parse error on line 2, column 2: extraneous or missing " in quoted-field',
'Syntax error on line 1; parse error on line 2, column 2: extraneous or missing " in quoted-field',
);
},
});
@ -448,7 +448,7 @@ c"d,e`;
assertThrows(
() => parse(input, { fieldsPerRecord: 2 }),
SyntaxError,
'record on line 2; parse error on line 4, column 1: extraneous or missing " in quoted-field',
'Syntax error on line 2; parse error on line 4, column 1: extraneous or missing " in quoted-field',
);
},
});
@ -463,7 +463,7 @@ c"d,e`;
assertThrows(
() => parse(input),
SyntaxError,
'record on line 4; parse error on line 4, column 1: extraneous or missing " in quoted-field',
'Syntax error on line 4; parse error on line 4, column 1: extraneous or missing " in quoted-field',
);
},
});
@ -703,7 +703,7 @@ c"d,e`;
assertThrows(
() => parse(input, { separator: "\n" }),
Error,
"Invalid Delimiter",
"Cannot parse input: invalid delimiter",
);
},
});
@ -714,7 +714,7 @@ c"d,e`;
assertThrows(
() => parse(input, { separator: "\r" }),
Error,
"Invalid Delimiter",
"Cannot parse input: invalid delimiter",
);
},
});
@ -725,7 +725,7 @@ c"d,e`;
assertThrows(
() => parse(input, { separator: '"' }),
Error,
"Invalid Delimiter",
"Cannot parse input: invalid delimiter",
);
},
});
@ -736,7 +736,7 @@ c"d,e`;
assertThrows(
() => parse(input, { comment: "\n" }),
Error,
"Invalid Delimiter",
"Cannot parse input: invalid delimiter",
);
},
});
@ -747,7 +747,7 @@ c"d,e`;
assertThrows(
() => parse(input, { comment: "\r" }),
Error,
"Invalid Delimiter",
"Cannot parse input: invalid delimiter",
);
},
});
@ -758,7 +758,7 @@ c"d,e`;
assertThrows(
() => parse(input, { separator: "X", comment: "X" }),
Error,
"Invalid Delimiter",
"Cannot parse input: invalid delimiter",
);
},
});
@ -841,7 +841,7 @@ c"d,e`;
columns: ["foo", "bar", "baz"],
}),
Error,
"record on line 2 has 2 fields, but the header has 3 fields",
"Syntax error on line 2: The record has 2 fields, but the header has 3 fields",
);
},
});
@ -856,7 +856,7 @@ c"d,e`;
columns: ["foo", "bar", "baz"],
}),
Error,
"record on line 2 has 4 fields, but the header has 3 fields",
"Syntax error on line 2: The record has 4 fields, but the header has 3 fields",
);
},
});
@ -920,7 +920,7 @@ c"d,e`;
assertThrows(
() => parse("", { skipFirstRow: true }),
Error,
"Headers must be defined",
"Cannot parse input: headers must be defined",
);
},
});