Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore(sql-editor): gracefully fallback if error occurs while opening a sheet (#3379)

Liu Jiaa69a39c2110
diff --git a/frontend/src/components/ProvideSQLEditorContext.vue b/frontend/src/components/ProvideSQLEditorContext.vue
index c4caff3c14..7caa64d130 100644
--- a/frontend/src/components/ProvideSQLEditorContext.vue
+++ b/frontend/src/components/ProvideSQLEditorContext.vue
@@ -100,10 +100,22 @@ const prepareSheet = async () => {
if (Number.isNaN(sheetId)) {
return false;
}
+ const openingSheetTab = tabStore.tabList.find(
+ (tab) => tab.sheetId === sheetId
+ );
+
sqlEditorStore.isFetchingSheet = true;
const sheet = await sheetStore.getOrFetchSheetById(sheetId);
sqlEditorStore.isFetchingSheet = false;
+
if (sheet.id === UNKNOWN_ID) {
+ if (openingSheetTab) {
+ // If a sheet is open in a tab but it returns 404 NOT_FOUND
+ // that means the sheet has been deleted somewhere else.
+ // We need to turn the sheet to an unsaved tab.
+ openingSheetTab.sheetId = undefined;
+ openingSheetTab.isSaved = false;
+ }
return false;
}
if (!isSheetReadable(sheet, currentUser.value)) {
@@ -114,10 +126,6 @@ const prepareSheet = async () => {
});
return false;
}
-
- const openingSheetTab = tabStore.tabList.find(
- (tab) => tab.sheetId === sheet.id
- );
if (openingSheetTab) {
// Switch to a sheet tab if it's open already.
tabStore.setCurrentTabId(openingSheetTab.id);
@@ -231,12 +239,28 @@ const syncURLWithConnection = () => {
sheetSlug: makeSheetSlug(sheet),
},
});
- return;
+ } else {
+ // A sheet is not found, fallback to an unsaved tab.
+ tabStore.updateCurrentTab({
+ sheetId: undefined,
+ isSaved: false,
+ });
}
+ return;
}
if (instanceId !== UNKNOWN_ID) {
const instance = instanceStore.getInstanceById(instanceId);
const database = databaseStore.getDatabaseById(databaseId); // might be <<Unknown database>> here
+ // Sometimes the instance and/or the database might be <<Unknown>> since
+ // they might be deleted somewhere else during the life of the page.
+ // So we need to sync the connection values for cleaning up to prevent
+ // exceptions.
+ tabStore.updateCurrentTab({
+ connection: {
+ instanceId: instance.id,
+ databaseId: database.id,
+ },
+ });
router.replace({
name: "sql-editor.detail",
params: {
diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts
index bf1fc388b9..6a64dce1fa 100644
--- a/frontend/src/router/index.ts
+++ b/frontend/src/router/index.ts
@@ -1358,16 +1358,8 @@ router.beforeEach((to, from, next) => {
const sheetId = idFromSlug(sheetSlug);
useSheetStore()
.fetchSheetById(sheetId)
- .then(() => {
- next();
- })
- .catch((error) => {
- next({
- name: "error.404",
- replace: false,
- });
- throw error;
- });
+ .then(() => next())
+ .catch(() => next());
return;
}
@@ -1380,25 +1372,13 @@ router.beforeEach((to, from, next) => {
useSQLEditorStore()
.fetchConnectionByInstanceId(instanceId)
.then(() => next())
- .catch((error) => {
- next({
- name: "error.404",
- replace: false,
- });
- throw error;
- });
+ .catch(() => next());
} else {
// Connected to db
useSQLEditorStore()
.fetchConnectionByInstanceIdAndDatabaseId(instanceId, databaseId)
.then(() => next())
- .catch((error) => {
- next({
- name: "error.404",
- replace: false,
- });
- throw error;
- });
+ .catch(() => next());
}
return;
}
diff --git a/frontend/src/store/modules/sheet.ts b/frontend/src/store/modules/sheet.ts
index 5e4cc87e5d..04c1d7382a 100644
--- a/frontend/src/store/modules/sheet.ts
+++ b/frontend/src/store/modules/sheet.ts
@@ -244,14 +244,18 @@ export const useSheetStore = defineStore("sheet", {
return sheetList;
},
async fetchSheetById(sheetId: SheetId) {
- const data = (await axios.get(`/api/sheet/${sheetId}`)).data;
- const sheet = convertSheet(data.data, data.included);
- this.setSheetById({
- sheetId: sheet.id,
- sheet: sheet,
- });
+ try {
+ const data = (await axios.get(`/api/sheet/${sheetId}`)).data;
+ const sheet = convertSheet(data.data, data.included);
+ this.setSheetById({
+ sheetId: sheet.id,
+ sheet: sheet,
+ });
- return sheet;
+ return sheet;
+ } catch {
+ return unknown("SHEET");
+ }
},
async getOrFetchSheetById(sheetId: SheetId) {
const storedSheet = this.sheetById.get(sheetId);
diff --git a/frontend/src/store/modules/sqlEditor.ts b/frontend/src/store/modules/sqlEditor.ts
index a713f2ad99..f49b74708a 100644
--- a/frontend/src/store/modules/sqlEditor.ts
+++ b/frontend/src/store/modules/sqlEditor.ts
@@ -80,35 +80,45 @@ export const useSQLEditorStore = defineStore("sqlEditor", {
instanceId: InstanceId,
databaseId: DatabaseId
): Promise<Connection> {
- await Promise.all([
- useDatabaseStore().getOrFetchDatabaseById(databaseId),
- useInstanceStore().getOrFetchInstanceById(instanceId),
- useTableStore().getOrFetchTableListByDatabaseId(databaseId),
- ]);
+ try {
+ await Promise.all([
+ useDatabaseStore().getOrFetchDatabaseById(databaseId),
+ useInstanceStore().getOrFetchInstanceById(instanceId),
+ useTableStore().getOrFetchTableListByDatabaseId(databaseId),
+ ]);
- return {
- instanceId,
- databaseId,
- };
+ return {
+ instanceId,
+ databaseId,
+ };
+ } catch {
+ // Fallback to disconnected if error occurs such as 404.
+ return { instanceId: UNKNOWN_ID, databaseId: UNKNOWN_ID };
+ }
},
async fetchConnectionByInstanceId(
instanceId: InstanceId
): Promise<Connection> {
- const [databaseList] = await Promise.all([
- useDatabaseStore().getDatabaseListByInstanceId(instanceId),
- useInstanceStore().getOrFetchInstanceById(instanceId),
- ]);
- const tableStore = useTableStore();
- await Promise.all(
- databaseList.map((db) =>
- tableStore.getOrFetchTableListByDatabaseId(db.id)
- )
- );
+ try {
+ const [databaseList] = await Promise.all([
+ useDatabaseStore().getDatabaseListByInstanceId(instanceId),
+ useInstanceStore().getOrFetchInstanceById(instanceId),
+ ]);
+ const tableStore = useTableStore();
+ await Promise.all(
+ databaseList.map((db) =>
+ tableStore.getOrFetchTableListByDatabaseId(db.id)
+ )
+ );
- return {
- instanceId,
- databaseId: UNKNOWN_ID,
- };
+ return {
+ instanceId,
+ databaseId: UNKNOWN_ID,
+ };
+ } catch {
+ // Fallback to disconnected if error occurs such as 404.
+ return { instanceId: UNKNOWN_ID, databaseId: UNKNOWN_ID };
+ }
},
async fetchQueryHistoryList() {
this.setIsFetchingQueryHistory(true);
diff --git a/frontend/src/store/modules/tab.ts b/frontend/src/store/modules/tab.ts
index 498b53d502..aea3b6a171 100644
--- a/frontend/src/store/modules/tab.ts
+++ b/frontend/src/store/modules/tab.ts
@@ -167,7 +167,7 @@ export const useTabStore = defineStore("tab", () => {
// Fetch opening sheets if needed
const sheetStore = useSheetStore();
tabList.value.forEach((tab) => {
- if (tab.sheetId) {
+ if (tab.sheetId && tab.sheetId !== UNKNOWN_ID) {
sheetStore.getOrFetchSheetById(tab.sheetId);
}
});