BREAKING(csv): throw TypeError in stringify() instead of StringifyError (#5347)

This commit is contained in:
Asher Gomez 2024-07-10 20:32:59 +10:00 committed by GitHub
parent adcfb5f1bb
commit 41be714d64
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 49 deletions

View File

@ -170,41 +170,6 @@ function normalizeColumn(column: Column): NormalizedColumn {
return { header, prop }; return { header, prop };
} }
/**
* Error thrown in {@linkcode stringify}.
*
* @example Usage
* ```ts no-assert
* import { stringify, StringifyError } from "@std/csv/stringify";
*
* try {
* stringify([{ a: 1 }, { a: 2 }], { separator: "\r\n" });
* } catch (error) {
* if (error instanceof StringifyError) {
* console.error(error.message);
* }
* }
* ```
*/
export class StringifyError extends Error {
/**
* Construct a new instance.
*
* @example Usage
* ```ts no-eval
* import { StringifyError } from "@std/csv/stringify";
*
* throw new StringifyError("An error occurred");
* ```
*
* @param message The error message.
*/
constructor(message?: string) {
super(message);
this.name = "StringifyError";
}
}
/** /**
* Returns an array of values from an object using the property accessors * Returns an array of values from an object using the property accessors
* (and optional transform function) in each column * (and optional transform function) in each column
@ -226,7 +191,7 @@ function getValuesFromItem(
if (Array.isArray(value)) { if (Array.isArray(value)) {
if (typeof prop === "number") value = value[prop]; if (typeof prop === "number") value = value[prop];
else { else {
throw new StringifyError( throw new TypeError(
'Property accessor is not of type "number"', 'Property accessor is not of type "number"',
); );
} }
@ -240,7 +205,7 @@ function getValuesFromItem(
if (Array.isArray(item)) { if (Array.isArray(item)) {
values.push(...item); values.push(...item);
} else if (typeof item === "object") { } else if (typeof item === "object") {
throw new StringifyError( throw new TypeError(
"No property accessor function was provided for object", "No property accessor function was provided for object",
); );
} else { } else {
@ -313,7 +278,7 @@ export function stringify(
' - U+0022: Quotation mark (")', ' - U+0022: Quotation mark (")',
" - U+000D U+000A: Carriage Return + Line Feed (\\r\\n)", " - U+000D U+000A: Carriage Return + Line Feed (\\r\\n)",
].join("\n"); ].join("\n");
throw new StringifyError(message); throw new TypeError(message);
} }
const normalizedColumns = columns.map(normalizeColumn); const normalizedColumns = columns.map(normalizeColumn);

View File

@ -1,6 +1,5 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { CsvStringifyStream } from "./stringify_stream.ts"; import { CsvStringifyStream } from "./stringify_stream.ts";
import { StringifyError } from "./stringify.ts";
import { assertEquals, assertRejects } from "@std/assert"; import { assertEquals, assertRejects } from "@std/assert";
Deno.test({ Deno.test({
@ -29,7 +28,7 @@ Deno.test({
]).pipeThrough(new CsvStringifyStream({ columns: ["id", "name"] })); ]).pipeThrough(new CsvStringifyStream({ columns: ["id", "name"] }));
await assertRejects( await assertRejects(
async () => await Array.fromAsync(readable), async () => await Array.fromAsync(readable),
StringifyError, TypeError,
); );
}); });
@ -53,7 +52,7 @@ Deno.test({
]).pipeThrough(new CsvStringifyStream({ separator: "\r\n" })); ]).pipeThrough(new CsvStringifyStream({ separator: "\r\n" }));
await assertRejects( await assertRejects(
async () => await Array.fromAsync(readable), async () => await Array.fromAsync(readable),
StringifyError, TypeError,
); );
}); });
@ -64,7 +63,7 @@ Deno.test({
]).pipeThrough(new CsvStringifyStream({ columns: { length: 1 } as any })); ]).pipeThrough(new CsvStringifyStream({ columns: { length: 1 } as any }));
await assertRejects( await assertRejects(
async () => await Array.fromAsync(readable), async () => await Array.fromAsync(readable),
StringifyError, TypeError,
"No property accessor function was provided for object", "No property accessor function was provided for object",
); );
}); });
@ -93,7 +92,7 @@ Deno.test({
]).pipeThrough(new CsvStringifyStream()); ]).pipeThrough(new CsvStringifyStream());
await assertRejects( await assertRejects(
async () => await Array.fromAsync(readable), async () => await Array.fromAsync(readable),
StringifyError, TypeError,
); );
}); });
}, },

View File

@ -10,7 +10,7 @@ import {
assertStringIncludes, assertStringIncludes,
assertThrows, assertThrows,
} from "@std/assert"; } from "@std/assert";
import { stringify, StringifyError } from "./stringify.ts"; import { stringify } from "./stringify.ts";
const CRLF = "\r\n"; const CRLF = "\r\n";
const BYTE_ORDER_MARK = "\ufeff"; const BYTE_ORDER_MARK = "\ufeff";
@ -26,7 +26,7 @@ Deno.test({
const errorMessage = 'Property accessor is not of type "number"'; const errorMessage = 'Property accessor is not of type "number"';
assertThrows( assertThrows(
() => stringify(data, { columns }), () => stringify(data, { columns }),
StringifyError, TypeError,
errorMessage, errorMessage,
); );
}, },
@ -46,7 +46,7 @@ Deno.test({
const options = { separator: '"', columns }; const options = { separator: '"', columns };
assertThrows( assertThrows(
() => stringify(data, options), () => stringify(data, options),
StringifyError, TypeError,
errorMessage, errorMessage,
); );
}, },
@ -66,7 +66,7 @@ Deno.test({
const options = { separator: "\r\n", columns }; const options = { separator: "\r\n", columns };
assertThrows( assertThrows(
() => stringify(data, options), () => stringify(data, options),
StringifyError, TypeError,
errorMessage, errorMessage,
); );
}, },
@ -80,7 +80,7 @@ Deno.test({
const data = [{ a: 1 }, { a: 2 }]; const data = [{ a: 1 }, { a: 2 }];
assertThrows( assertThrows(
() => stringify(data), () => stringify(data),
StringifyError, TypeError,
"No property accessor function was provided for object", "No property accessor function was provided for object",
); );
}, },
@ -93,7 +93,7 @@ Deno.test({
const data = [{ a: 1 }, { a: 2 }]; const data = [{ a: 1 }, { a: 2 }];
assertThrows( assertThrows(
() => stringify(data), () => stringify(data),
StringifyError, TypeError,
"No property accessor function was provided for object", "No property accessor function was provided for object",
); );
}, },