The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Refactor error handling for content mutations
Joshen Limeb3aec2909b4
diff --git a/studio/components/layouts/SQLEditorLayout/QueryItem.tsx b/studio/components/layouts/SQLEditorLayout/QueryItem.tsxindex d7cb1ad653e..2548f273590 100644--- a/studio/components/layouts/SQLEditorLayout/QueryItem.tsx+++ b/studio/components/layouts/SQLEditorLayout/QueryItem.tsx@@ -75,6 +75,9 @@ const QueryItemActions = observer(({ tabInfo, activeId }: QueryItemActionsProps)snap.removeSnippet(data.id)}},+ onError(error) {+ ui.setNotification({ category: 'error', message: `Failed to delete query: ${error.message}` })+ },})const { id, name } = tabInfo || {}@@ -100,20 +103,13 @@ const QueryItemActions = observer(({ tabInfo, activeId }: QueryItemActionsProps)if (!ref) return console.error('Project ref is required')if (!id) return console.error('Snippet ID is required')- try {- await deleteContent({ projectRef: ref, id })+ await deleteContent({ projectRef: ref, id })- const existingSnippetIds = (snap.orders[ref] ?? []).filter((x) => x !== id)- if (existingSnippetIds.length === 0) {- router.push(`/project/${ref}/sql`)- } else {- router.push(`/project/${ref}/sql/${existingSnippetIds[0]}`)- }- } catch (error: any) {- ui.setNotification({- category: 'error',- message: `Failed to create new query: ${error.message}`,- })+ const existingSnippetIds = (snap.orders[ref] ?? []).filter((x) => x !== id)+ if (existingSnippetIds.length === 0) {+ router.push(`/project/${ref}/sql`)+ } else {+ router.push(`/project/${ref}/sql/${existingSnippetIds[0]}`)}}diff --git a/studio/data/content/content-create-mutation.ts b/studio/data/content/content-create-mutation.tsindex a7235fd70ec..a08a1b6c9ea 100644--- a/studio/data/content/content-create-mutation.ts+++ b/studio/data/content/content-create-mutation.ts@@ -1,7 +1,9 @@import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'+import toast from 'react-hot-toast'+import { post } from 'lib/common/fetch'import { API_URL } from 'lib/constants'-import { UserContent } from 'types'+import { ResponseError, UserContent } from 'types'import { contentKeys } from './keys'type CreateContentVariables = {@@ -25,23 +27,29 @@ type CreateContentData = Awaited<ReturnType<typeof createContent>>export const useContentCreateMutation = ({onSuccess,+ onError,...options}: Omit<- UseMutationOptions<CreateContentData, unknown, CreateContentVariables>,+ UseMutationOptions<CreateContentData, ResponseError, CreateContentVariables>,'mutationFn'> = {}) => {const queryClient = useQueryClient()- return useMutation<CreateContentData, unknown, CreateContentVariables>(+ return useMutation<CreateContentData, ResponseError, CreateContentVariables>((args) => createContent(args),{async onSuccess(data, variables, context) {const { projectRef } = variables-await Promise.all([queryClient.invalidateQueries(contentKeys.list(projectRef))])-await onSuccess?.(data, variables, context)},+ async onError(data, variables, context) {+ if (onError === undefined) {+ toast.error(`Failed to create content: ${data.message}`)+ } else {+ onError(data, variables, context)+ }+ },...options,})diff --git a/studio/data/content/content-delete-mutation.ts b/studio/data/content/content-delete-mutation.tsindex 14653436e14..3fce510a473 100644--- a/studio/data/content/content-delete-mutation.ts+++ b/studio/data/content/content-delete-mutation.ts@@ -1,7 +1,9 @@import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'+import toast from 'react-hot-toast'+import { delete_ } from 'lib/common/fetch'import { API_URL } from 'lib/constants'-import { UserContent } from 'types'+import { ResponseError, UserContent } from 'types'import { contentKeys } from './keys'type DeleteContentVariables = { projectRef: string; id: string }@@ -24,23 +26,29 @@ type DeleteContentData = Awaited<ReturnType<typeof deleteContent>>export const useContentDeleteMutation = ({onSuccess,+ onError,...options}: Omit<- UseMutationOptions<DeleteContentData, unknown, DeleteContentVariables>,+ UseMutationOptions<DeleteContentData, ResponseError, DeleteContentVariables>,'mutationFn'> = {}) => {const queryClient = useQueryClient()- return useMutation<DeleteContentData, unknown, DeleteContentVariables>(+ return useMutation<DeleteContentData, ResponseError, DeleteContentVariables>((args) => deleteContent(args),{async onSuccess(data, variables, context) {const { projectRef } = variables-await Promise.all([queryClient.invalidateQueries(contentKeys.list(projectRef))])-await onSuccess?.(data, variables, context)},+ async onError(data, variables, context) {+ if (onError === undefined) {+ toast.error(`Failed to delete content: ${data.message}`)+ } else {+ onError(data, variables, context)+ }+ },...options,})diff --git a/studio/data/content/content-update-mutation.ts b/studio/data/content/content-update-mutation.tsindex a6c4deb843f..5ce1afced6e 100644--- a/studio/data/content/content-update-mutation.ts+++ b/studio/data/content/content-update-mutation.ts@@ -1,7 +1,9 @@+import toast from 'react-hot-toast'import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'+import { patch } from 'lib/common/fetch'import { API_URL } from 'lib/constants'-import { UserContent } from 'types'+import { ResponseError, UserContent } from 'types'import { contentKeys } from './keys'type UpdateContentVariables = {@@ -30,23 +32,29 @@ type UpdateContentData = Awaited<ReturnType<typeof updateContent>>export const useContentUpdateMutation = ({onSuccess,+ onError,...options}: Omit<- UseMutationOptions<UpdateContentData, unknown, UpdateContentVariables>,+ UseMutationOptions<UpdateContentData, ResponseError, UpdateContentVariables>,'mutationFn'> = {}) => {const queryClient = useQueryClient()- return useMutation<UpdateContentData, unknown, UpdateContentVariables>(+ return useMutation<UpdateContentData, ResponseError, UpdateContentVariables>((args) => updateContent(args),{async onSuccess(data, variables, context) {const { projectRef } = variables-await Promise.all([queryClient.invalidateQueries(contentKeys.list(projectRef))])-await onSuccess?.(data, variables, context)},+ async onError(data, variables, context) {+ if (onError === undefined) {+ toast.error(`Failed to update content: ${data.message}`)+ } else {+ onError(data, variables, context)+ }+ },...options,})