From e30b1985d1dd15d845fc854476126ebbe8777cc8 Mon Sep 17 00:00:00 2001 From: Cristhian Zanforlin Lousa Date: Tue, 22 Apr 2025 19:27:01 -0300 Subject: [PATCH] fix: Enhance error handling with new constants in flow management components (devx) (#7562) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 📝 (constants.ts): add UUID_PARSING_ERROR constant for better error handling in use-add-flow.ts 🐛 (use-add-flow.ts): handle UUID_PARSING_ERROR in onError callback to display a specific error message and redirect to flows page if UUID parsing error occurs * ✨ (use-add-flow.ts): introduce constant REDIRECT_DELAY to improve code readability and maintainability --- src/frontend/src/constants/constants.ts | 2 ++ src/frontend/src/hooks/flows/use-add-flow.ts | 29 ++++++++++++++------ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/frontend/src/constants/constants.ts b/src/frontend/src/constants/constants.ts index cfe5f46a3..7ec483bec 100644 --- a/src/frontend/src/constants/constants.ts +++ b/src/frontend/src/constants/constants.ts @@ -1068,3 +1068,5 @@ export const OPENAI_VOICES = [ export const DEFAULT_POLLING_INTERVAL = 5000; export const DEFAULT_TIMEOUT = 30000; export const DEFAULT_FILE_PICKER_TIMEOUT = 60000; + +export const UUID_PARSING_ERROR = "uuid_parsing"; diff --git a/src/frontend/src/hooks/flows/use-add-flow.ts b/src/frontend/src/hooks/flows/use-add-flow.ts index 3fbbcb32a..23c229b25 100644 --- a/src/frontend/src/hooks/flows/use-add-flow.ts +++ b/src/frontend/src/hooks/flows/use-add-flow.ts @@ -1,3 +1,4 @@ +import { UUID_PARSING_ERROR } from "@/constants/constants"; import { usePostAddFlow } from "@/controllers/API/queries/flows/use-post-add-flow"; import useAlertStore from "@/stores/alertStore"; import useFlowsManagerStore from "@/stores/flowsManagerStore"; @@ -17,15 +18,19 @@ import { cloneDeep } from "lodash"; import { useParams } from "react-router-dom"; import useDeleteFlow from "./use-delete-flow"; +const FLOW_CREATION_ERROR = "Flow creation error"; +const FOLDER_NOT_FOUND_ERROR = "Folder not found. Redirecting to flows..."; +const FLOW_CREATION_ERROR_MESSAGE = + "An unexpected error occurred, please try again"; +const REDIRECT_DELAY = 3000; const useAddFlow = () => { const flows = useFlowsManagerStore((state) => state.flows); const setFlows = useFlowsManagerStore((state) => state.setFlows); const { deleteFlow } = useDeleteFlow(); const { setFlowToCanvas } = useFlowsManagerStore(); - + const setNoticeData = useAlertStore.getState().setNoticeData; const { folderId } = useParams(); - const myCollectionId = useFolderStore((state) => state.myCollectionId); const unavailableFields = useGlobalVariablesStore( @@ -92,18 +97,26 @@ const useAddFlow = () => { resolve(createdFlow.id); }, onError: (error) => { + if (error?.response?.data?.detail[0]?.type === UUID_PARSING_ERROR) { + setNoticeData({ + title: FOLDER_NOT_FOUND_ERROR, + }); + setTimeout(() => { + window.location.href = `/flows`; + }, REDIRECT_DELAY); + + return; + } + if (error.response?.data?.detail) { useAlertStore.getState().setErrorData({ - title: "Could not create flow", + title: FLOW_CREATION_ERROR, list: [error.response?.data?.detail], }); } else { useAlertStore.getState().setErrorData({ - title: "Could not create flow", - list: [ - error.message ?? - "An unexpected error occurred, please try again", - ], + title: FLOW_CREATION_ERROR, + list: [error.message ?? FLOW_CREATION_ERROR_MESSAGE], }); } reject(error); // Re-throw the error so the caller can handle it if needed},