Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Refactor error handling for edge function mutations

Joshen Limff6509c16d93
diff --git a/studio/components/interfaces/Functions/EdgeFunctionDetails/EdgeFunctionDetails.tsx b/studio/components/interfaces/Functions/EdgeFunctionDetails/EdgeFunctionDetails.tsx
index 60a433db257..6b7055ace13 100644
--- a/studio/components/interfaces/Functions/EdgeFunctionDetails/EdgeFunctionDetails.tsx
+++ b/studio/components/interfaces/Functions/EdgeFunctionDetails/EdgeFunctionDetails.tsx
@@ -1,44 +1,42 @@
+import * as Tooltip from '@radix-ui/react-tooltip'
+import { PermissionAction } from '@supabase/shared-types/out/constants'
+import clsx from 'clsx'
import dayjs from 'dayjs'
import Link from 'next/link'
import { useRouter } from 'next/router'
-import { FC, useEffect, useMemo, useState } from 'react'
-import * as Tooltip from '@radix-ui/react-tooltip'
-import { PermissionAction } from '@supabase/shared-types/out/constants'
+import { useEffect, useMemo, useState } from 'react'
import {
Alert,
- IconTerminal,
- IconMinimize2,
- IconMaximize2,
Button,
- Modal,
Form,
- Toggle,
- Input,
IconExternalLink,
+ IconMaximize2,
+ IconMinimize2,
+ IconTerminal,
+ Input,
+ Modal,
+ Toggle,
} from 'ui'
-import { useStore, useCheckPermissions } from 'hooks'
import { useParams } from 'common/hooks'
-import Panel from 'components/ui/Panel'
-import CommandRender from '../CommandRender'
-import { generateCLICommands } from './EdgeFunctionDetails.utils'
-import { useProjectApiQuery } from 'data/config/project-api-query'
-import { useEdgeFunctionUpdateMutation } from 'data/edge-functions/edge-functions-update-mutation'
-import { useEdgeFunctionDeleteMutation } from 'data/edge-functions/edge-functions-delete-mutation'
import {
+ FormActions,
FormHeader,
FormPanel,
- FormActions,
FormSection,
- FormSectionLabel,
FormSectionContent,
+ FormSectionLabel,
} from 'components/ui/Forms'
+import Panel from 'components/ui/Panel'
+import { useProjectApiQuery } from 'data/config/project-api-query'
import { useEdgeFunctionQuery } from 'data/edge-functions/edge-function-query'
-import clsx from 'clsx'
-
-interface Props {}
+import { useEdgeFunctionDeleteMutation } from 'data/edge-functions/edge-functions-delete-mutation'
+import { useEdgeFunctionUpdateMutation } from 'data/edge-functions/edge-functions-update-mutation'
+import { useCheckPermissions, useStore } from 'hooks'
+import CommandRender from '../CommandRender'
+import { generateCLICommands } from './EdgeFunctionDetails.utils'
-const EdgeFunctionDetails: FC<Props> = () => {
+const EdgeFunctionDetails = () => {
const router = useRouter()
const { ui } = useStore()
const { ref: projectRef, functionSlug } = useParams()
@@ -47,7 +45,7 @@ const EdgeFunctionDetails: FC<Props> = () => {
const { data: settings } = useProjectApiQuery({ projectRef })
const { data: selectedFunction } = useEdgeFunctionQuery({ projectRef, slug: functionSlug })
- const { mutateAsync: updateEdgeFunction } = useEdgeFunctionUpdateMutation()
+ const { mutateAsync: updateEdgeFunction, isLoading: isUpdating } = useEdgeFunctionUpdateMutation()
const { mutateAsync: deleteEdgeFunction, isLoading: isDeleting } = useEdgeFunctionDeleteMutation()
const formId = 'edge-function-update-form'
@@ -68,46 +66,29 @@ const EdgeFunctionDetails: FC<Props> = () => {
anonKey
)
- const onUpdateFunction = async (values: any, { setSubmitting, resetForm }: any) => {
+ const onUpdateFunction = async (values: any, { resetForm }: any) => {
if (!projectRef) return console.error('Project ref is required')
if (selectedFunction === undefined) return console.error('No edge function selected')
- setSubmitting(true)
- try {
- await updateEdgeFunction({
- projectRef,
- slug: selectedFunction.slug,
- payload: values,
- })
- resetForm({ values, initialValues: values })
- ui.setNotification({ category: 'success', message: `Successfully updated edge function` })
- } catch (error: any) {
- ui.setNotification({
- category: 'error',
- message: `Failed to update edge function: ${error.message}`,
- })
- } finally {
- setSubmitting(false)
- }
+ await updateEdgeFunction({
+ projectRef,
+ slug: selectedFunction.slug,
+ payload: values,
+ })
+ resetForm({ values, initialValues: values })
+ ui.setNotification({ category: 'success', message: `Successfully updated edge function` })
}
const onConfirmDelete = async () => {
if (!projectRef) return console.error('Project ref is required')
if (selectedFunction === undefined) return console.error('No edge function selected')
- try {
- await deleteEdgeFunction({ projectRef, slug: selectedFunction.slug })
- ui.setNotification({
- category: 'success',
- message: `Successfully deleted "${selectedFunction.name}"`,
- })
- router.push(`/project/${projectRef}/functions`)
- } catch (error: any) {
- ui.setNotification({
- category: 'error',
- message: `Failed to delete function: ${error.message}`,
- })
- }
+ await deleteEdgeFunction({ projectRef, slug: selectedFunction.slug })
+ ui.setNotification({
+ category: 'success',
+ message: `Successfully deleted "${selectedFunction.name}"`,
+ })
+ router.push(`/project/${projectRef}/functions`)
}
const hasImportMap = useMemo(
@@ -119,7 +100,7 @@ const EdgeFunctionDetails: FC<Props> = () => {
<>
<div className="space-y-4 pb-16">
<Form id={formId} initialValues={{}} onSubmit={onUpdateFunction}>
- {({ isSubmitting, handleReset, values, initialValues, resetForm }: any) => {
+ {({ handleReset, values, initialValues, resetForm }: any) => {
const hasChanges = JSON.stringify(values) !== JSON.stringify(initialValues)
// [Alaister] although this "technically" is breaking the rules of React hooks
@@ -143,7 +124,7 @@ const EdgeFunctionDetails: FC<Props> = () => {
<div className="flex py-4 px-8">
<FormActions
form={formId}
- isSubmitting={isSubmitting}
+ isSubmitting={isUpdating}
hasChanges={hasChanges}
handleReset={handleReset}
helper={
diff --git a/studio/data/edge-functions/edge-functions-delete-mutation.ts b/studio/data/edge-functions/edge-functions-delete-mutation.ts
index d642758cd9c..c1ec1a32526 100644
--- a/studio/data/edge-functions/edge-functions-delete-mutation.ts
+++ b/studio/data/edge-functions/edge-functions-delete-mutation.ts
@@ -1,6 +1,9 @@
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
+import { toast } from 'react-hot-toast'
+
import { delete_ } from 'lib/common/fetch'
import { API_ADMIN_URL } from 'lib/constants'
+import { ResponseError } from 'types'
import { edgeFunctionsKeys } from './keys'
export type EdgeFunctionsDeleteVariables = {
@@ -23,14 +26,15 @@ type EdgeFunctionsDeleteData = Awaited<ReturnType<typeof deleteEdgeFunction>>
export const useEdgeFunctionDeleteMutation = ({
onSuccess,
+ onError,
...options
}: Omit<
- UseMutationOptions<EdgeFunctionsDeleteData, unknown, EdgeFunctionsDeleteVariables>,
+ UseMutationOptions<EdgeFunctionsDeleteData, ResponseError, EdgeFunctionsDeleteVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
- return useMutation<EdgeFunctionsDeleteData, unknown, EdgeFunctionsDeleteVariables>(
+ return useMutation<EdgeFunctionsDeleteData, ResponseError, EdgeFunctionsDeleteVariables>(
(vars) => deleteEdgeFunction(vars),
{
async onSuccess(data, variables, context) {
@@ -40,6 +44,13 @@ export const useEdgeFunctionDeleteMutation = ({
})
await onSuccess?.(data, variables, context)
},
+ async onError(data, variables, context) {
+ if (onError === undefined) {
+ toast.error(`Failed to delete edge function: ${data.message}`)
+ } else {
+ onError(data, variables, context)
+ }
+ },
...options,
}
)
diff --git a/studio/data/edge-functions/edge-functions-update-mutation.ts b/studio/data/edge-functions/edge-functions-update-mutation.ts
index 56c6be6c835..a77f9d0e3d1 100644
--- a/studio/data/edge-functions/edge-functions-update-mutation.ts
+++ b/studio/data/edge-functions/edge-functions-update-mutation.ts
@@ -1,6 +1,9 @@
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
+import { toast } from 'react-hot-toast'
+
import { patch } from 'lib/common/fetch'
import { API_ADMIN_URL } from 'lib/constants'
+import { ResponseError } from 'types'
import { edgeFunctionsKeys } from './keys'
export type EdgeFunctionsUpdateVariables = {
@@ -30,14 +33,15 @@ type EdgeFunctionsUpdateData = Awaited<ReturnType<typeof updateEdgeFunction>>
export const useEdgeFunctionUpdateMutation = ({
onSuccess,
+ onError,
...options
}: Omit<
- UseMutationOptions<EdgeFunctionsUpdateData, unknown, EdgeFunctionsUpdateVariables>,
+ UseMutationOptions<EdgeFunctionsUpdateData, ResponseError, EdgeFunctionsUpdateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
- return useMutation<EdgeFunctionsUpdateData, unknown, EdgeFunctionsUpdateVariables>(
+ return useMutation<EdgeFunctionsUpdateData, ResponseError, EdgeFunctionsUpdateVariables>(
(vars) => updateEdgeFunction(vars),
{
async onSuccess(data, variables, context) {
@@ -45,6 +49,13 @@ export const useEdgeFunctionUpdateMutation = ({
await queryClient.invalidateQueries(edgeFunctionsKeys.detail(projectRef, slug))
await onSuccess?.(data, variables, context)
},
+ async onError(data, variables, context) {
+ if (onError === undefined) {
+ toast.error(`Failed to update edge function: ${data.message}`)
+ } else {
+ onError(data, variables, context)
+ }
+ },
...options,
}
)