diff --git a/src/frontend/apps/drive/src/features/api/APIError.ts b/src/frontend/apps/drive/src/features/api/APIError.ts index e93dcda6..24fb6f5e 100644 --- a/src/frontend/apps/drive/src/features/api/APIError.ts +++ b/src/frontend/apps/drive/src/features/api/APIError.ts @@ -1,5 +1,6 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import i18n from "@/features/i18n/initI18n"; +import { AppError } from "../errors/AppError"; export class APIError extends Error { data?: any; @@ -19,25 +20,24 @@ export const errorToString = (error: unknown): string => { if (error instanceof APIError) { // If there is a data, it means that the error is a JSON object if (error.data) { - return errorToString(error.data); + /** + * This is made to handle full text errors from the API like: + * + * "title": [ + * "The title field is required." + * ] + */ + return Object.entries(error.data) + .map(([, value]) => `${value}`) + .join("\n"); } // If there is no data, it means that the error is a string, probably a complicated html error. return i18n.t("api.error.unexpected"); } - if (error instanceof Error) { + // We want to show the error message from the AppError only. Not message from the Error class as they + // can be really technical and not helpful for the user. For those we show the generic error message. + if (error instanceof AppError) { return error.message; } - if (typeof error === "object" && error !== null) { - /** - * This is made to handle full text errors from the API like: - * - * "title": [ - * "The title field is required." - * ] - */ - return Object.entries(error) - .map(([, value]) => `${errorToString(value)}`) - .join("\n"); - } return i18n.t("api.error.unexpected"); }; diff --git a/src/frontend/apps/drive/src/features/errors/AppError.ts b/src/frontend/apps/drive/src/features/errors/AppError.ts new file mode 100644 index 00000000..56ee6854 --- /dev/null +++ b/src/frontend/apps/drive/src/features/errors/AppError.ts @@ -0,0 +1 @@ +export class AppError extends Error {}