From cb845534b2cc4f35fb602846c8608b5f2c64f3da Mon Sep 17 00:00:00 2001 From: Saxon Fletcher Date: Tue, 17 Jun 2025 13:39:32 +1000 Subject: [PATCH] error handling --- .../studio/data/branches/branch-diff-query.ts | 2 +- .../edge-function-body-query.ts | 6 ++-- .../studio/hooks/misc/useEdgeFunctionsDiff.ts | 13 ++++++++ apps/studio/pages/project/[ref]/merge.tsx | 31 +++++++++++++------ 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/apps/studio/data/branches/branch-diff-query.ts b/apps/studio/data/branches/branch-diff-query.ts index e61b21e0c54..a57bd265319 100644 --- a/apps/studio/data/branches/branch-diff-query.ts +++ b/apps/studio/data/branches/branch-diff-query.ts @@ -40,7 +40,7 @@ export async function getBranchDiff({ return diffData || '' } catch (error) { handleError(error) - throw error + return '' } } diff --git a/apps/studio/data/edge-functions/edge-function-body-query.ts b/apps/studio/data/edge-functions/edge-function-body-query.ts index af35720db6e..d604b96395f 100644 --- a/apps/studio/data/edge-functions/edge-function-body-query.ts +++ b/apps/studio/data/edge-functions/edge-function-body-query.ts @@ -54,10 +54,8 @@ export async function getEdgeFunctionBody( const { files } = await parseResponse.json() return files as EdgeFunctionFile[] } catch (error) { - console.error('Failed to parse edge function code:', error) - throw new Error( - 'Failed to parse function code. The file may be corrupted or in an invalid format.' - ) + handleError(error) + return [] } } diff --git a/apps/studio/hooks/misc/useEdgeFunctionsDiff.ts b/apps/studio/hooks/misc/useEdgeFunctionsDiff.ts index 2de0ce731a7..c601f463d33 100644 --- a/apps/studio/hooks/misc/useEdgeFunctionsDiff.ts +++ b/apps/studio/hooks/misc/useEdgeFunctionsDiff.ts @@ -1,5 +1,6 @@ import { useMemo } from 'react' import { useQueries } from '@tanstack/react-query' +import { handleError } from 'data/fetchers' import { getEdgeFunctionBody, type EdgeFunctionBodyData, @@ -104,6 +105,18 @@ export const useEdgeFunctionsDiff = ({ ...removedBodiesQueries, ].some((q) => q.isLoading) + // Aggregate errors across all queries and handle the first encountered error. + const firstError = [ + ...currentBodiesQueries, + ...mainBodiesQueries, + ...addedBodiesQueries, + ...removedBodiesQueries, + ].find((q) => q.error)?.error + + if (firstError) { + handleError(firstError) + } + // Build lookup maps -------------------------------------------------------- const currentBodiesMap: Record = {} currentBodiesQueries.forEach((q, idx) => { diff --git a/apps/studio/pages/project/[ref]/merge.tsx b/apps/studio/pages/project/[ref]/merge.tsx index 2d9f3d8665d..19d53094761 100644 --- a/apps/studio/pages/project/[ref]/merge.tsx +++ b/apps/studio/pages/project/[ref]/merge.tsx @@ -30,9 +30,13 @@ const MergePage: NextPageWithLayout = () => { const { ref } = useParams() const gitlessBranching = useFlag('gitlessBranching') - if (!gitlessBranching) { - router.push(`/project/${ref}/branches`) - } + + // Redirect to branches page if feature flag disabled + useEffect(() => { + if (!gitlessBranching && ref) { + router.push(`/project/${ref}/branches`) + } + }, [gitlessBranching, ref, router]) const project = useSelectedProject() const [isSubmitting, setIsSubmitting] = useState(false) @@ -41,7 +45,10 @@ const MergePage: NextPageWithLayout = () => { const parentProjectRef = project?.parent_project_ref // Get branch information - const { data: branches } = useBranchesQuery({ projectRef: parentProjectRef }) + const { data: branches } = useBranchesQuery( + { projectRef: parentProjectRef }, + { enabled: gitlessBranching && !!parentProjectRef } + ) const currentBranch = branches?.find((branch) => branch.project_ref === ref) const mainBranch = branches?.find((branch) => branch.is_default) @@ -53,7 +60,7 @@ const MergePage: NextPageWithLayout = () => { const { data: workflowRuns } = useWorkflowRunsQuery( { projectRef: parentProjectRef }, { - enabled: !!parentProjectRef && !!workflowRunId, + enabled: gitlessBranching && !!parentProjectRef && !!workflowRunId, refetchInterval: 3000, // Poll every 3 seconds to check for status changes } ) @@ -73,7 +80,7 @@ const MergePage: NextPageWithLayout = () => { const { data: workflowRunLogs } = useWorkflowRunQuery( { workflowRunId }, { - enabled: !!workflowRunId, + enabled: gitlessBranching && !!workflowRunId, refetchInterval: isPolling ? 2000 : false, // Poll logs every 2 seconds until complete } ) @@ -89,7 +96,7 @@ const MergePage: NextPageWithLayout = () => { branchId: currentBranch?.id || '', projectRef: parentProjectRef || '', }, - { enabled: !!currentBranch?.id } + { enabled: gitlessBranching && !!currentBranch?.id } ) // Show toast notifications when workflow status changes @@ -119,11 +126,11 @@ const MergePage: NextPageWithLayout = () => { // Get edge functions for both branches const { data: currentBranchFunctions, isLoading: isCurrentFunctionsLoading } = - useEdgeFunctionsQuery({ projectRef: ref }, { enabled: !!ref }) + useEdgeFunctionsQuery({ projectRef: ref }, { enabled: gitlessBranching && !!ref }) const { data: mainBranchFunctions, isLoading: isMainFunctionsLoading } = useEdgeFunctionsQuery( { projectRef: parentProjectRef }, - { enabled: !!parentProjectRef } + { enabled: gitlessBranching && !!parentProjectRef } ) // Check if there are any changes (database or edge functions) @@ -238,6 +245,7 @@ const MergePage: NextPageWithLayout = () => { ] }, [workflowRunId, currentTab]) + // If not on a preview branch or branch info unavailable, show notice if (!isBranch || !currentBranch) { return ( @@ -248,6 +256,11 @@ const MergePage: NextPageWithLayout = () => { ) } + if (!gitlessBranching) { + // Render nothing while redirecting + return null + } + const isDataLoaded = !isDiffLoading && !isCurrentFunctionsLoading && !isMainFunctionsLoading const isMergeDisabled = hasAnyChanges && isDataLoaded