diff --git a/src/app/utils/formatErrors.test.ts b/src/app/utils/formatErrors.test.ts index 23d476ad95..e5b81ddc90 100644 --- a/src/app/utils/formatErrors.test.ts +++ b/src/app/utils/formatErrors.test.ts @@ -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"); }); }); diff --git a/src/app/utils/formatErrors.ts b/src/app/utils/formatErrors.ts index b5784fc27b..c9c9b600ca 100644 --- a/src/app/utils/formatErrors.ts +++ b/src/app/utils/formatErrors.ts @@ -20,7 +20,8 @@ const flattenErrors = (errors: E): string | null => { export type ErrorType = | APIError - | EventError; + | EventError + | Error; export const formatErrors = ( errors?: ErrorType, @@ -34,9 +35,14 @@ export const formatErrors = ( 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;