Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: TypeError formatting #5076

Merged
merged 1 commit into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/app/utils/formatErrors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ describe("formatErrors", () => {
expect(formatErrors(error, "name")).toEqual("Too long.");
});

it("can return the errors for a single key from an array", () => {
const error = {
name: ["Too long.", "Too late."],
};
expect(formatErrors(error, "name")).toEqual("Too long. Too late.");
it("correctly formats fetch TypeError", () => {
const typeError = new TypeError("Failed to fetch");
expect(formatErrors(typeError)).toEqual("Failed to fetch");
});
});
14 changes: 10 additions & 4 deletions src/app/utils/formatErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const flattenErrors = <E>(errors: E): string | null => {

export type ErrorType<E = null, I = any, K extends keyof I = any> =
| APIError<E>
| EventError<I, E, K>;
| EventError<I, E, K>
| Error;

export const formatErrors = <E, I, K extends keyof I>(
errors?: ErrorType<E, I, K>,
Expand All @@ -34,9 +35,14 @@ export const formatErrors = <E, I, K extends keyof I>(
if (errorKey && errorKey in errors) {
errorMessage = flattenErrors(errors[errorKey as keyof typeof errors]);
} else {
errorMessage = Object.entries(errors)
.map(([key, value]) => `${key}: ${flattenErrors(value)}`)
.join(" ");
const errorEntries = Object.entries(errors);
if (errorEntries.length > 0) {
errorMessage = Object.entries(errors)
.map(([key, value]) => `${key}: ${flattenErrors(value)}`)
.join(" ");
} else if (errors instanceof Error) {
errorMessage = errors?.message;
}
}
} else if (typeof errors === "string") {
errorMessage = errors;
Expand Down
Loading