Skip to content

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

Update error handling for table editor RQs (#21443)

Joshen Lim7556a3181c74
diff --git a/apps/studio/data/__templates/resource-query.ts b/apps/studio/data/__templates/resource-query.ts
index 08a72400c23..6fa63c86e78 100644
--- a/apps/studio/data/__templates/resource-query.ts
+++ b/apps/studio/data/__templates/resource-query.ts
@@ -1,7 +1,7 @@
import { useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query'
import { useCallback } from 'react'
-import { get } from 'data/fetchers'
+import { get, handleError } from 'data/fetchers'
import { ResponseError } from 'types'
import { resourceKeys } from './keys'
@@ -22,8 +22,7 @@ export async function getResource({ projectRef, id }: ResourceVariables, signal?
params: { path: { ref: projectRef, id } },
signal,
})
- if (error) throw new Error((error as ResponseError).message)
-
+ if (error) handleError(error)
return data
}
diff --git a/apps/studio/data/__templates/resource-update-mutation.ts b/apps/studio/data/__templates/resource-update-mutation.ts
index 16310dcb978..2fd34512f8f 100644
--- a/apps/studio/data/__templates/resource-update-mutation.ts
+++ b/apps/studio/data/__templates/resource-update-mutation.ts
@@ -1,7 +1,7 @@
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import toast from 'react-hot-toast'
-import { patch } from 'data/fetchers'
+import { handleError, patch } from 'data/fetchers'
import { ResponseError } from 'types'
import { resourceKeys } from './keys'
@@ -18,7 +18,7 @@ export async function updateResource({ projectRef, id, updatedParam }: ResourceU
body: { updatedParam },
})
- if (error) throw new Error((error as ResponseError).message)
+ if (error) handleError(error)
return data
}
diff --git a/apps/studio/data/__templates/resources-query.ts b/apps/studio/data/__templates/resources-query.ts
index 8545612ba7e..5ed424df8b4 100644
--- a/apps/studio/data/__templates/resources-query.ts
+++ b/apps/studio/data/__templates/resources-query.ts
@@ -1,7 +1,7 @@
import { useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query'
import { useCallback } from 'react'
-import { get } from 'data/fetchers'
+import { get, handleError } from 'data/fetchers'
import { ResponseError } from 'types'
import { resourceKeys } from './keys'
@@ -17,7 +17,7 @@ export async function getResources({ projectRef }: ResourcesVariables, signal?:
params: { path: { ref: projectRef } },
signal,
})
- if (error) throw new Error((error as ResponseError).message)
+ if (error) handleError(error)
return data
}
diff --git a/apps/studio/data/database/pooling-configuration-query.ts b/apps/studio/data/database/pooling-configuration-query.ts
index 8f0ccce52ba..7e03ccb3cd5 100644
--- a/apps/studio/data/database/pooling-configuration-query.ts
+++ b/apps/studio/data/database/pooling-configuration-query.ts
@@ -1,8 +1,9 @@
import { useQuery, UseQueryOptions } from '@tanstack/react-query'
-import { get } from 'data/fetchers'
+
+import { components } from 'data/api'
+import { get, handleError } from 'data/fetchers'
import { ResponseError } from 'types'
import { databaseKeys } from './keys'
-import { components } from 'data/api'
export type PoolingConfigurationVariables = {
projectRef?: string
@@ -20,7 +21,7 @@ export async function getPoolingConfiguration(
params: { path: { ref: projectRef } },
signal,
})
- if (error) throw new Error((error as ResponseError).message)
+ if (error) handleError(error)
return data
}
diff --git a/apps/studio/data/fetchers.ts b/apps/studio/data/fetchers.ts
index 22f12bbbfe5..0799e1a1dcb 100644
--- a/apps/studio/data/fetchers.ts
+++ b/apps/studio/data/fetchers.ts
@@ -3,6 +3,7 @@ import { getAccessToken } from 'lib/gotrue'
import { uuidv4 } from 'lib/helpers'
import createClient from 'openapi-fetch'
import { paths } from './api' // generated from openapi-typescript
+import { ResponseError } from 'types'
const DEFAULT_HEADERS = {
'Content-Type': 'application/json',
@@ -148,3 +149,7 @@ export const options: typeof _options = async (url, init) => {
headers,
})
}
+
+export const handleError = (error: ResponseError) => {
+ throw new Error(error.message)
+}
diff --git a/apps/studio/data/read-replicas/load-balancers-query.ts b/apps/studio/data/read-replicas/load-balancers-query.ts
index 5d610e7d58e..e0fb4fff4d5 100644
--- a/apps/studio/data/read-replicas/load-balancers-query.ts
+++ b/apps/studio/data/read-replicas/load-balancers-query.ts
@@ -1,10 +1,11 @@
import { useQuery, UseQueryOptions } from '@tanstack/react-query'
-import { get } from 'data/fetchers'
-import { ResponseError } from 'types'
-import { replicaKeys } from './keys'
+
import { components } from 'data/api'
-import { useFlag } from 'hooks'
+import { get, handleError } from 'data/fetchers'
import { useProjectDetailQuery } from 'data/projects/project-detail-query'
+import { useFlag } from 'hooks'
+import { ResponseError } from 'types'
+import { replicaKeys } from './keys'
export type LoadBalancersVariables = {
projectRef?: string
@@ -23,7 +24,7 @@ export async function getLoadBalancers(
signal,
})
- if (error) throw new Error((error as ResponseError).message)
+ if (error) handleError(error)
return data
}
diff --git a/apps/studio/data/sql/execute-sql-query.ts b/apps/studio/data/sql/execute-sql-query.ts
index 2b46e6a0e97..5e8b4fbf8a1 100644
--- a/apps/studio/data/sql/execute-sql-query.ts
+++ b/apps/studio/data/sql/execute-sql-query.ts
@@ -1,6 +1,6 @@
import { QueryClient, QueryKey, useQuery, UseQueryOptions } from '@tanstack/react-query'
-import { post } from 'data/fetchers'
+import { post, handleError as handleErrorFetchers } from 'data/fetchers'
import {
ROLE_IMPERSONATION_NO_RESULTS,
ROLE_IMPERSONATION_SQL_LINE_COUNT,
@@ -85,7 +85,7 @@ export async function executeSql(
}
if (handleError !== undefined) return handleError(error as any)
- else throw error
+ else handleErrorFetchers(error as any)
}
if (
diff --git a/apps/studio/data/sql/format-sql-query.ts b/apps/studio/data/sql/format-sql-query.ts
index 1ca6b48be97..06254c55d40 100644
--- a/apps/studio/data/sql/format-sql-query.ts
+++ b/apps/studio/data/sql/format-sql-query.ts
@@ -14,9 +14,7 @@ export async function formatQuery(
) {
let headers = new Headers()
- if (connectionString) {
- headers.set('x-connection-encrypted', connectionString)
- }
+ if (connectionString) headers.set('x-connection-encrypted', connectionString)
const response = await post(
`${API_URL}/pg-meta/${projectRef}/query/format`,
diff --git a/apps/studio/data/tables/table-create-mutation.ts b/apps/studio/data/tables/table-create-mutation.ts
index 62f27e27f58..fc1975ed6d1 100644
--- a/apps/studio/data/tables/table-create-mutation.ts
+++ b/apps/studio/data/tables/table-create-mutation.ts
@@ -2,7 +2,7 @@ import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react
import { toast } from 'react-hot-toast'
import { components } from 'data/api'
-import { post } from 'data/fetchers'
+import { handleError, post } from 'data/fetchers'
import { ResponseError } from 'types'
import { tableKeys } from './keys'
@@ -28,7 +28,7 @@ export async function createTable({ projectRef, connectionString, payload }: Tab
headers,
})
- if (error) throw error
+ if (error) handleError(error)
return data
}
diff --git a/apps/studio/data/tables/table-delete-mutation.ts b/apps/studio/data/tables/table-delete-mutation.ts
index d6937c3efb9..cd8d7be5dce 100644
--- a/apps/studio/data/tables/table-delete-mutation.ts
+++ b/apps/studio/data/tables/table-delete-mutation.ts
@@ -2,7 +2,7 @@ import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react
import { toast } from 'react-hot-toast'
import { entityTypeKeys } from 'data/entity-types/keys'
-import { del } from 'data/fetchers'
+import { del, handleError } from 'data/fetchers'
import { viewKeys } from 'data/views/keys'
import { ResponseError } from 'types'
import { tableKeys } from './keys'
@@ -33,7 +33,7 @@ export async function deleteTable({
headers,
})
- if (error) throw error
+ if (error) handleError(error)
return data
}
diff --git a/apps/studio/data/tables/table-query.ts b/apps/studio/data/tables/table-query.ts
index 4ca967c695e..2bec6eb5a18 100644
--- a/apps/studio/data/tables/table-query.ts
+++ b/apps/studio/data/tables/table-query.ts
@@ -1,9 +1,10 @@
import { PostgresTable } from '@supabase/postgres-meta'
import { useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query'
-import { get } from 'lib/common/fetch'
+// import { get } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
import { tableKeys } from './keys'
import { useCallback } from 'react'
+import { get, handleError } from 'data/fetchers'
export type TableVariables = {
id?: number
@@ -19,26 +20,25 @@ export async function getTable(
{ id, projectRef, connectionString }: TableVariables,
signal?: AbortSignal
) {
- if (!projectRef) {
- throw new Error('projectRef is required')
- }
- if (!id) {
- throw new Error('id is required')
- }
+ if (!projectRef) throw new Error('projectRef is required')
+ if (!id) throw new Error('id is required')
let headers = new Headers()
if (connectionString) headers.set('x-connection-encrypted', connectionString)
- const response = (await get(`${API_URL}/pg-meta/${projectRef}/tables?id=${id}`, {
- headers: Object.fromEntries(headers),
+ const { data, error } = await get('/platform/pg-meta/{ref}/tables', {
+ params: {
+ header: { 'x-connection-encrypted': connectionString! },
+ path: { ref: projectRef },
+ // @ts-ignore
+ query: { id: id.toString() },
+ },
+ headers,
signal,
- })) as TableResponse
-
- if ('error' in response) {
- throw response.error
- }
+ })
- return response as PostgresTable
+ if (error) handleError(error)
+ return data as unknown as PostgresTable
}
export type TableData = Awaited<ReturnType<typeof getTable>>
diff --git a/apps/studio/data/tables/table-update-mutation.ts b/apps/studio/data/tables/table-update-mutation.ts
index 3ab10a5521d..274b91c035e 100644
--- a/apps/studio/data/tables/table-update-mutation.ts
+++ b/apps/studio/data/tables/table-update-mutation.ts
@@ -2,7 +2,7 @@ import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react
import { toast } from 'react-hot-toast'
import { components } from 'data/api'
-import { patch } from 'data/fetchers'
+import { handleError, patch } from 'data/fetchers'
import { ResponseError } from 'types'
import { tableKeys } from './keys'
@@ -35,7 +35,7 @@ export async function updateTable({
headers,
})
- if (error) throw error
+ if (error) handleError(error)
return data
}
diff --git a/apps/studio/data/tables/tables-query.ts b/apps/studio/data/tables/tables-query.ts
index 5ce1babcd05..14472190c21 100644
--- a/apps/studio/data/tables/tables-query.ts
+++ b/apps/studio/data/tables/tables-query.ts
@@ -1,8 +1,9 @@
import { PostgresTable } from '@supabase/postgres-meta'
import { useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query'
-import { get } from 'data/fetchers'
import { sortBy } from 'lodash'
import { useCallback } from 'react'
+
+import { get, handleError } from 'data/fetchers'
import { ResponseError } from 'types'
import { tableKeys } from './keys'
@@ -44,21 +45,15 @@ export async function getTables(
const { data, error } = await get('/platform/pg-meta/{ref}/tables', {
params: {
- header: {
- 'x-connection-encrypted': connectionString!,
- },
- path: {
- ref: projectRef,
- },
+ header: { 'x-connection-encrypted': connectionString! },
+ path: { ref: projectRef },
query: queryParams as any,
},
headers,
signal,
})
- if (!Array.isArray(data) && error) {
- throw error
- }
+ if (!Array.isArray(data) && error) handleError(error)
// Sort the data if the sortByName option is true
if (Array.isArray(data) && sortByProperty) {
diff --git a/apps/studio/data/views/view-query.ts b/apps/studio/data/views/view-query.ts
index b87ae24165b..5a7b365f317 100644
--- a/apps/studio/data/views/view-query.ts
+++ b/apps/studio/data/views/view-query.ts
@@ -1,7 +1,7 @@
import { PostgresView } from '@supabase/postgres-meta'
import { useQuery, UseQueryOptions } from '@tanstack/react-query'
-import { get } from 'data/fetchers'
+import { get, handleError } from 'data/fetchers'
import { ResponseError } from 'types'
import { viewKeys } from './keys'
@@ -35,7 +35,7 @@ export async function getView(
signal,
})
- if (error) throw new Error((error as ResponseError).message)
+ if (error) handleError(error)
return data as unknown as PostgresView
}
diff --git a/apps/studio/data/views/views-query.ts b/apps/studio/data/views/views-query.ts
index a367a3466a9..12767524c84 100644
--- a/apps/studio/data/views/views-query.ts
+++ b/apps/studio/data/views/views-query.ts
@@ -1,7 +1,7 @@
import { UseQueryOptions, useQuery, useQueryClient } from '@tanstack/react-query'
import { useCallback } from 'react'
-import { get } from 'data/fetchers'
+import { get, handleError } from 'data/fetchers'
import { ResponseError } from 'types'
import { viewKeys } from './keys'
@@ -32,7 +32,7 @@ export async function getViews(
signal,
})
- if (error) throw new Error((error as ResponseError).message)
+ if (error) handleError(error)
return data
}