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 custom domains mutations

Joshen Lim33ed870ecbf2
diff --git a/studio/components/interfaces/Settings/General/CustomDomainConfig/CustomDomainActivate.tsx b/studio/components/interfaces/Settings/General/CustomDomainConfig/CustomDomainActivate.tsx
index 70a5f868976..4167fd9562c 100644
--- a/studio/components/interfaces/Settings/General/CustomDomainConfig/CustomDomainActivate.tsx
+++ b/studio/components/interfaces/Settings/General/CustomDomainConfig/CustomDomainActivate.tsx
@@ -17,43 +17,23 @@ export type CustomDomainActivateProps = {
const CustomDomainActivate = ({ projectRef, customDomain }: CustomDomainActivateProps) => {
const { ui } = useStore()
-
const [isActivateConfirmModalVisible, setIsActivateConfirmModalVisible] = useState(false)
const { mutateAsync: activateCustomDomain } = useCustomDomainActivateMutation()
const { mutateAsync: deleteCustomDomain, isLoading: isDeleting } = useCustomDomainDeleteMutation()
const onActivateCustomDomain = async () => {
- if (!projectRef) {
- throw new Error('Project ref is required')
- }
-
- try {
- await activateCustomDomain({ projectRef })
+ if (!projectRef) return console.error('Project ref is required')
- ui.setNotification({
- category: 'success',
- message: `Successfully activated custom domain`,
- })
-
- setIsActivateConfirmModalVisible(false)
- } catch (error: any) {
- ui.setNotification({
- category: 'error',
- message: error.message,
- })
- }
+ await activateCustomDomain({ projectRef })
+ ui.setNotification({ category: 'success', message: `Successfully activated custom domain` })
+ setIsActivateConfirmModalVisible(false)
}
const onCancelCustomDomain = async () => {
- if (!projectRef) {
- throw new Error('Project ref is required')
- }
- try {
- await deleteCustomDomain({ projectRef })
- } catch (error: any) {
- ui.setNotification({ category: 'error', message: error.message })
- }
+ if (!projectRef) return console.error('Project ref is required')
+
+ await deleteCustomDomain({ projectRef })
}
return (
diff --git a/studio/components/interfaces/Settings/General/CustomDomainConfig/CustomDomainDelete.tsx b/studio/components/interfaces/Settings/General/CustomDomainConfig/CustomDomainDelete.tsx
index e09a3a0b3fc..6c5adeafdd6 100644
--- a/studio/components/interfaces/Settings/General/CustomDomainConfig/CustomDomainDelete.tsx
+++ b/studio/components/interfaces/Settings/General/CustomDomainConfig/CustomDomainDelete.tsx
@@ -1,13 +1,13 @@
+import { observer } from 'mobx-react-lite'
import Link from 'next/link'
import { useState } from 'react'
-import { observer } from 'mobx-react-lite'
import { Button, IconExternalLink, IconTrash } from 'ui'
-import { useStore } from 'hooks'
+import ConfirmModal from 'components/ui/Dialogs/ConfirmDialog'
+import Panel from 'components/ui/Panel'
import { useCustomDomainDeleteMutation } from 'data/custom-domains/custom-domains-delete-mutation'
import { CustomDomainResponse } from 'data/custom-domains/custom-domains-query'
-import Panel from 'components/ui/Panel'
-import ConfirmModal from 'components/ui/Dialogs/ConfirmDialog'
+import { useStore } from 'hooks'
export type CustomDomainDeleteProps = {
projectRef?: string
@@ -16,31 +16,15 @@ export type CustomDomainDeleteProps = {
const CustomDomainDelete = ({ projectRef, customDomain }: CustomDomainDeleteProps) => {
const { ui } = useStore()
-
const [isDeleteConfirmModalVisible, setIsDeleteConfirmModalVisible] = useState(false)
-
const { mutateAsync: deleteCustomDomain } = useCustomDomainDeleteMutation()
const onDeleteCustomDomain = async () => {
- if (!projectRef) {
- throw new Error('Project ref is required')
- }
-
- try {
- await deleteCustomDomain({ projectRef })
-
- ui.setNotification({
- category: 'success',
- message: `Successfully deleted custom domain`,
- })
+ if (!projectRef) return console.error('Project ref is required')
- setIsDeleteConfirmModalVisible(false)
- } catch (error: any) {
- ui.setNotification({
- category: 'error',
- message: error.message,
- })
- }
+ await deleteCustomDomain({ projectRef })
+ ui.setNotification({ category: 'success', message: `Successfully deleted custom domain` })
+ setIsDeleteConfirmModalVisible(false)
}
return (
diff --git a/studio/components/interfaces/Settings/General/CustomDomainConfig/CustomDomainVerify.tsx b/studio/components/interfaces/Settings/General/CustomDomainConfig/CustomDomainVerify.tsx
index ac4dc4a27ef..46c021e0a1c 100644
--- a/studio/components/interfaces/Settings/General/CustomDomainConfig/CustomDomainVerify.tsx
+++ b/studio/components/interfaces/Settings/General/CustomDomainConfig/CustomDomainVerify.tsx
@@ -1,16 +1,15 @@
+import { observer } from 'mobx-react-lite'
import Link from 'next/link'
import { useState } from 'react'
-import { observer } from 'mobx-react-lite'
import { Alert, Button, IconAlertCircle, IconExternalLink, IconHelpCircle, IconRefreshCw } from 'ui'
-import { useStore } from 'hooks'
+import InformationBox from 'components/ui/InformationBox'
+import Panel from 'components/ui/Panel'
import { ProjectApiResponse } from 'data/config/project-api-query'
-import { CustomDomainResponse } from 'data/custom-domains/custom-domains-query'
import { useCustomDomainDeleteMutation } from 'data/custom-domains/custom-domains-delete-mutation'
+import { CustomDomainResponse } from 'data/custom-domains/custom-domains-query'
import { useCustomDomainReverifyMutation } from 'data/custom-domains/custom-domains-reverify-mutation'
import DNSRecord from './DNSRecord'
-import Panel from 'components/ui/Panel'
-import InformationBox from 'components/ui/InformationBox'
export type CustomDomainVerifyProps = {
projectRef?: string
@@ -19,7 +18,6 @@ export type CustomDomainVerifyProps = {
}
const CustomDomainVerify = ({ projectRef, customDomain, settings }: CustomDomainVerifyProps) => {
- const { ui } = useStore()
const [isNotVerifiedYet, setIsNotVerifiedYet] = useState(false)
const { mutate: reverifyCustomDomain, isLoading: isReverifyLoading } =
@@ -36,22 +34,13 @@ const CustomDomainVerify = ({ projectRef, customDomain, settings }: CustomDomain
)
const onReverifyCustomDomain = () => {
- if (!projectRef) {
- throw new Error('Project ref is required')
- }
-
+ if (!projectRef) return console.error('Project ref is required')
reverifyCustomDomain({ projectRef })
}
const onCancelCustomDomain = async () => {
- if (!projectRef) {
- throw new Error('Project ref is required')
- }
- try {
- await deleteCustomDomain({ projectRef })
- } catch (error: any) {
- ui.setNotification({ category: 'error', message: error.message })
- }
+ if (!projectRef) return console.error('Project ref is required')
+ await deleteCustomDomain({ projectRef })
}
return (
diff --git a/studio/components/interfaces/Settings/General/CustomDomainConfig/CustomDomainsConfigureHostname.tsx b/studio/components/interfaces/Settings/General/CustomDomainConfig/CustomDomainsConfigureHostname.tsx
index 75c54228551..5a70f7baafd 100644
--- a/studio/components/interfaces/Settings/General/CustomDomainConfig/CustomDomainsConfigureHostname.tsx
+++ b/studio/components/interfaces/Settings/General/CustomDomainConfig/CustomDomainsConfigureHostname.tsx
@@ -40,7 +40,7 @@ const CustomDomainsConfigureHostname = () => {
}
const onCreateCustomDomain = async (values: yup.InferType<typeof schema>) => {
- if (!ref) throw new Error('Project ref is required')
+ if (!ref) return console.error('Project ref is required')
const cnameVerified = await verifyCNAME(values.domain)
if (!cnameVerified) {
@@ -50,18 +50,7 @@ const CustomDomainsConfigureHostname = () => {
})
}
- try {
- await createCustomDomain({
- projectRef: ref,
- customDomain: values.domain,
- })
- } catch (error: any) {
- ui.setNotification({
- error,
- category: 'error',
- message: error.message,
- })
- }
+ await createCustomDomain({ projectRef: ref, customDomain: values.domain })
}
return (
diff --git a/studio/data/__templates/README.md b/studio/data/__templates/README.md
index 04c002df983..aa6bcccf92c 100644
--- a/studio/data/__templates/README.md
+++ b/studio/data/__templates/README.md
@@ -82,8 +82,13 @@ const { mutateAsync: someAction } = useMutation({
})
const onConfirm = async () => {
+ // Assuming that your mutation needs a URL param like project ref
+ // This check is just to satistfy the linting - there's an implicit assumption that
+ // projectRef here will definitely be available since its obtained from the URL
+ if (!projectRef) return console.error('Project ref is required')
+
// Any logic before calling the mutation
- await someAction(someParameters)
+ await someAction({ projectRef, otherParameters })
// Any logic after calling the mutation
}
```
diff --git a/studio/data/custom-domains/custom-domains-activate-mutation.ts b/studio/data/custom-domains/custom-domains-activate-mutation.ts
index 25e2b1f4a35..b4231d40fa6 100644
--- a/studio/data/custom-domains/custom-domains-activate-mutation.ts
+++ b/studio/data/custom-domains/custom-domains-activate-mutation.ts
@@ -1,6 +1,9 @@
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
+import toast from 'react-hot-toast'
+
import { post } from 'lib/common/fetch'
import { API_ADMIN_URL } from 'lib/constants'
+import { ResponseError } from 'types'
import { customDomainKeys } from './keys'
export type CustomDomainActivateVariables = {
@@ -8,18 +11,12 @@ export type CustomDomainActivateVariables = {
}
export async function activateCustomDomain({ projectRef }: CustomDomainActivateVariables) {
- if (!projectRef) {
- throw new Error('projectRef is required')
- }
-
const response = await post(
`${API_ADMIN_URL}/projects/${projectRef}/custom-hostname/activate`,
{}
)
- if (response.error) {
- throw response.error
- }
+ if (response.error) throw response.error
return response
}
@@ -27,23 +24,29 @@ type CustomDomainActivateData = Awaited<ReturnType<typeof activateCustomDomain>>
export const useCustomDomainActivateMutation = ({
onSuccess,
+ onError,
...options
}: Omit<
- UseMutationOptions<CustomDomainActivateData, unknown, CustomDomainActivateVariables>,
+ UseMutationOptions<CustomDomainActivateData, ResponseError, CustomDomainActivateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
- return useMutation<CustomDomainActivateData, unknown, CustomDomainActivateVariables>(
+ return useMutation<CustomDomainActivateData, ResponseError, CustomDomainActivateVariables>(
(vars) => activateCustomDomain(vars),
{
async onSuccess(data, variables, context) {
const { projectRef } = variables
-
await queryClient.invalidateQueries(customDomainKeys.list(projectRef))
-
await onSuccess?.(data, variables, context)
},
+ async onError(data, variables, context) {
+ if (onError === undefined) {
+ toast.error(`Failed to activate custom domain: ${data.message}`)
+ } else {
+ onError(data, variables, context)
+ }
+ },
...options,
}
)
diff --git a/studio/data/custom-domains/custom-domains-create-mutation.ts b/studio/data/custom-domains/custom-domains-create-mutation.ts
index d3819d521fd..8a45ba44cf2 100644
--- a/studio/data/custom-domains/custom-domains-create-mutation.ts
+++ b/studio/data/custom-domains/custom-domains-create-mutation.ts
@@ -1,6 +1,9 @@
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
+import { toast } from 'react-hot-toast'
+
import { post } from 'lib/common/fetch'
import { API_ADMIN_URL } from 'lib/constants'
+import { ResponseError } from 'types'
import { customDomainKeys } from './keys'
export type CustomDomainCreateVariables = {
@@ -12,20 +15,12 @@ export async function createCustomDomain({
projectRef,
customDomain,
}: CustomDomainCreateVariables) {
- if (!projectRef) {
- throw new Error('projectRef is required')
- }
-
const response = await post(
`${API_ADMIN_URL}/projects/${projectRef}/custom-hostname/initialize`,
- {
- custom_hostname: customDomain,
- }
+ { custom_hostname: customDomain }
)
- if (response.error) {
- throw response.error
- }
+ if (response.error) throw response.error
return response
}
@@ -33,23 +28,29 @@ type CustomDomainCreateData = Awaited<ReturnType<typeof createCustomDomain>>
export const useCustomDomainCreateMutation = ({
onSuccess,
+ onError,
...options
}: Omit<
- UseMutationOptions<CustomDomainCreateData, unknown, CustomDomainCreateVariables>,
+ UseMutationOptions<CustomDomainCreateData, ResponseError, CustomDomainCreateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
- return useMutation<CustomDomainCreateData, unknown, CustomDomainCreateVariables>(
+ return useMutation<CustomDomainCreateData, ResponseError, CustomDomainCreateVariables>(
(vars) => createCustomDomain(vars),
{
async onSuccess(data, variables, context) {
const { projectRef } = variables
-
await queryClient.invalidateQueries(customDomainKeys.list(projectRef))
-
await onSuccess?.(data, variables, context)
},
+ async onError(data, variables, context) {
+ if (onError === undefined) {
+ toast.error(`Failed to create custom domain: ${data.message}`)
+ } else {
+ onError(data, variables, context)
+ }
+ },
...options,
}
)
diff --git a/studio/data/custom-domains/custom-domains-delete-mutation.ts b/studio/data/custom-domains/custom-domains-delete-mutation.ts
index 7d6ca20f112..0cc760a9a86 100644
--- a/studio/data/custom-domains/custom-domains-delete-mutation.ts
+++ b/studio/data/custom-domains/custom-domains-delete-mutation.ts
@@ -2,21 +2,17 @@ import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react
import { delete_ } from 'lib/common/fetch'
import { API_ADMIN_URL } from 'lib/constants'
import { customDomainKeys } from './keys'
+import { toast } from 'react-hot-toast'
+import { ResponseError } from 'types'
export type CustomDomainDeleteVariables = {
projectRef: string
}
export async function deleteCustomDomain({ projectRef }: CustomDomainDeleteVariables) {
- if (!projectRef) {
- throw new Error('projectRef is required')
- }
-
const response = await delete_(`${API_ADMIN_URL}/projects/${projectRef}/custom-hostname`, {})
- if (response.error) {
- throw response.error
- }
+ if (response.error) throw response.error
return response
}
@@ -24,14 +20,15 @@ type CustomDomainDeleteData = Awaited<ReturnType<typeof deleteCustomDomain>>
export const useCustomDomainDeleteMutation = ({
onSuccess,
+ onError,
...options
}: Omit<
- UseMutationOptions<CustomDomainDeleteData, unknown, CustomDomainDeleteVariables>,
+ UseMutationOptions<CustomDomainDeleteData, ResponseError, CustomDomainDeleteVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
- return useMutation<CustomDomainDeleteData, unknown, CustomDomainDeleteVariables>(
+ return useMutation<CustomDomainDeleteData, ResponseError, CustomDomainDeleteVariables>(
(vars) => deleteCustomDomain(vars),
{
async onSuccess(data, variables, context) {
@@ -49,6 +46,13 @@ export const useCustomDomainDeleteMutation = ({
await onSuccess?.(data, variables, context)
},
+ async onError(data, variables, context) {
+ if (onError === undefined) {
+ toast.error(`Failed to delete custom domain: ${data.message}`)
+ } else {
+ onError(data, variables, context)
+ }
+ },
...options,
}
)
diff --git a/studio/data/custom-domains/custom-domains-reverify-mutation.ts b/studio/data/custom-domains/custom-domains-reverify-mutation.ts
index 7e4e48113bf..4bf4836f136 100644
--- a/studio/data/custom-domains/custom-domains-reverify-mutation.ts
+++ b/studio/data/custom-domains/custom-domains-reverify-mutation.ts
@@ -2,24 +2,20 @@ import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react
import { post } from 'lib/common/fetch'
import { API_ADMIN_URL } from 'lib/constants'
import { customDomainKeys } from './keys'
+import { toast } from 'react-hot-toast'
+import { ResponseError } from 'types'
export type CustomDomainReverifyVariables = {
projectRef: string
}
export async function reverifyCustomDomain({ projectRef }: CustomDomainReverifyVariables) {
- if (!projectRef) {
- throw new Error('projectRef is required')
- }
-
const response = await post(
`${API_ADMIN_URL}/projects/${projectRef}/custom-hostname/reverify`,
{}
)
- if (response.error) {
- throw response.error
- }
+ if (response.error) throw response.error
return response
}
@@ -27,23 +23,29 @@ type CustomDomainReverifyData = Awaited<ReturnType<typeof reverifyCustomDomain>>
export const useCustomDomainReverifyMutation = ({
onSuccess,
+ onError,
...options
}: Omit<
- UseMutationOptions<CustomDomainReverifyData, unknown, CustomDomainReverifyVariables>,
+ UseMutationOptions<CustomDomainReverifyData, ResponseError, CustomDomainReverifyVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
- return useMutation<CustomDomainReverifyData, unknown, CustomDomainReverifyVariables>(
+ return useMutation<CustomDomainReverifyData, ResponseError, CustomDomainReverifyVariables>(
(vars) => reverifyCustomDomain(vars),
{
async onSuccess(data, variables, context) {
const { projectRef } = variables
-
await queryClient.invalidateQueries(customDomainKeys.list(projectRef))
-
await onSuccess?.(data, variables, context)
},
+ async onError(data, variables, context) {
+ if (onError === undefined) {
+ toast.error(`Failed to reverify custom domain: ${data.message}`)
+ } else {
+ onError(data, variables, context)
+ }
+ },
...options,
}
)