From 07c80bb02c05aa432c92331183e96756fc6f6903 Mon Sep 17 00:00:00 2001 From: Anmol Singh Bhatia <121005188+anmolsinghbhatia@users.noreply.github.com> Date: Fri, 18 Jul 2025 14:25:28 +0530 Subject: [PATCH] [WEB-4525] fix: project create/update error handling (#7429) * chore: project create/update error handling * chore: code refactor --- .../ce/components/projects/create/root.tsx | 55 +++++++------------ apps/web/core/components/project/form.tsx | 55 +++++++------------ 2 files changed, 42 insertions(+), 68 deletions(-) diff --git a/apps/web/ce/components/projects/create/root.tsx b/apps/web/ce/components/projects/create/root.tsx index aeb8b3aae..71935fa2e 100644 --- a/apps/web/ce/components/projects/create/root.tsx +++ b/apps/web/ce/components/projects/create/root.tsx @@ -99,45 +99,32 @@ export const CreateProjectForm: FC = observer((props) = // Handle the new error format where codes are nested in arrays under field names const errorData = err?.data ?? {}; - // Check for specific error codes in the new format - if (errorData.name?.includes("PROJECT_NAME_ALREADY_EXIST")) { - setToast({ - type: TOAST_TYPE.ERROR, - title: t("toast.error"), - message: t("project_name_already_taken"), - }); - } + const nameError = errorData.name?.includes("PROJECT_NAME_ALREADY_EXIST"); + const identifierError = errorData?.identifier?.includes("PROJECT_IDENTIFIER_ALREADY_EXIST"); - if (errorData?.identifier?.includes("PROJECT_IDENTIFIER_ALREADY_EXIST")) { - setToast({ - type: TOAST_TYPE.ERROR, - title: t("toast.error"), - message: t("project_identifier_already_taken"), - }); - } - - // Handle other field-specific errors (excluding name and identifier which are handled above) - Object.keys(errorData).forEach((field) => { - // Skip name and identifier fields as they're handled separately above - if (field === "name" || field === "identifier") return; - - const fieldErrors = errorData[field]; - if (Array.isArray(fieldErrors)) { - fieldErrors.forEach((errorMessage) => { - setToast({ - type: TOAST_TYPE.ERROR, - title: t("error"), - message: errorMessage, - }); - }); - } else if (typeof fieldErrors === "string") { + if (nameError || identifierError) { + if (nameError) { setToast({ type: TOAST_TYPE.ERROR, - title: t("error"), - message: fieldErrors, + title: t("toast.error"), + message: t("project_name_already_taken"), }); } - }); + + if (identifierError) { + setToast({ + type: TOAST_TYPE.ERROR, + title: t("toast.error"), + message: t("project_identifier_already_taken"), + }); + } + } else { + setToast({ + type: TOAST_TYPE.ERROR, + title: t("toast.error"), + message: t("something_went_wrong"), + }); + } } catch (error) { // Fallback error handling if the error processing fails console.error("Error processing API error:", error); diff --git a/apps/web/core/components/project/form.tsx b/apps/web/core/components/project/form.tsx index c93a316d8..452d9d0b6 100644 --- a/apps/web/core/components/project/form.tsx +++ b/apps/web/core/components/project/form.tsx @@ -116,45 +116,32 @@ export const ProjectDetailsForm: FC = (props) => { // Handle the new error format where codes are nested in arrays under field names const errorData = err ?? {}; - // Check for specific error codes in the new format - if (errorData.name?.includes("PROJECT_NAME_ALREADY_EXIST")) { - setToast({ - type: TOAST_TYPE.ERROR, - title: t("toast.error"), - message: t("project_name_already_taken"), - }); - } + const nameError = errorData.name?.includes("PROJECT_NAME_ALREADY_EXIST"); + const identifierError = errorData?.identifier?.includes("PROJECT_IDENTIFIER_ALREADY_EXIST"); - if (errorData?.identifier?.includes("PROJECT_IDENTIFIER_ALREADY_EXIST")) { - setToast({ - type: TOAST_TYPE.ERROR, - title: t("toast.error"), - message: t("project_identifier_already_taken"), - }); - } - - // Handle other field-specific errors (excluding name and identifier which are handled above) - Object.keys(errorData).forEach((field) => { - // Skip name and identifier fields as they're handled separately above - if (field === "name" || field === "identifier") return; - - const fieldErrors = errorData[field]; - if (Array.isArray(fieldErrors)) { - fieldErrors.forEach((errorMessage) => { - setToast({ - type: TOAST_TYPE.ERROR, - title: t("error"), - message: errorMessage, - }); - }); - } else if (typeof fieldErrors === "string") { + if (nameError || identifierError) { + if (nameError) { setToast({ type: TOAST_TYPE.ERROR, - title: t("error"), - message: fieldErrors, + title: t("toast.error"), + message: t("project_name_already_taken"), }); } - }); + + if (identifierError) { + setToast({ + type: TOAST_TYPE.ERROR, + title: t("toast.error"), + message: t("project_identifier_already_taken"), + }); + } + } else { + setToast({ + type: TOAST_TYPE.ERROR, + title: t("toast.error"), + message: t("something_went_wrong"), + }); + } } catch (error) { // Fallback error handling if the error processing fails console.error("Error processing API error:", error);