Skip to content

Commit 3904d04

Browse files
committed
fix(ux): handle db delete exception
1 parent 5c5dc09 commit 3904d04

File tree

5 files changed

+40
-12
lines changed

5 files changed

+40
-12
lines changed

fyo/demux/db.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ipcRenderer } from 'electron';
22
import { DatabaseError, NotImplemented } from 'fyo/utils/errors';
33
import { SchemaMap } from 'schemas/types';
44
import { DatabaseDemuxBase, DatabaseMethod } from 'utils/db/types';
5-
import { DatabaseResponse } from 'utils/ipc/types';
5+
import { BackendResponse } from 'utils/ipc/types';
66
import { IPC_ACTIONS } from 'utils/messages';
77

88
export class DatabaseDemux extends DatabaseDemuxBase {
@@ -12,7 +12,7 @@ export class DatabaseDemux extends DatabaseDemuxBase {
1212
this.#isElectron = isElectron;
1313
}
1414

15-
async #handleDBCall(func: () => Promise<DatabaseResponse>): Promise<unknown> {
15+
async #handleDBCall(func: () => Promise<BackendResponse>): Promise<unknown> {
1616
const response = await func();
1717

1818
if (response.error?.name) {

main/helpers.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import fs from 'fs/promises';
33
import { ConfigFile, ConfigKeys } from 'fyo/core/types';
44
import { Main } from 'main';
55
import config from 'utils/config';
6-
import { DatabaseResponse } from 'utils/ipc/types';
6+
import { BackendResponse } from 'utils/ipc/types';
77
import { IPC_CHANNELS } from 'utils/messages';
88

99
interface ConfigFilesWithModified extends ConfigFile {
@@ -54,15 +54,16 @@ export async function getConfigFilesWithModified(files: ConfigFile[]) {
5454
}
5555

5656
export async function getErrorHandledReponse(func: () => Promise<unknown>) {
57-
const response: DatabaseResponse = {};
57+
const response: BackendResponse = {};
5858

5959
try {
6060
response.data = await func();
6161
} catch (err) {
6262
response.error = {
63-
name: (err as Error).name,
64-
message: (err as Error).message,
65-
stack: (err as Error).stack,
63+
name: (err as NodeJS.ErrnoException).name,
64+
message: (err as NodeJS.ErrnoException).message,
65+
stack: (err as NodeJS.ErrnoException).stack,
66+
code: (err as NodeJS.ErrnoException).code,
6667
};
6768
}
6869

main/registerIpcMainActionListeners.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export default function registerIpcMainActionListeners(main: Main) {
139139
});
140140

141141
ipcMain.handle(IPC_ACTIONS.DELETE_FILE, async (_, filePath) => {
142-
await fs.unlink(filePath);
142+
return getErrorHandledReponse(async () => await fs.unlink(filePath));
143143
});
144144

145145
ipcMain.handle(IPC_ACTIONS.GET_DB_LIST, async (_) => {

src/utils/ipcCalls.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
*/
44
import { ipcRenderer } from 'electron';
55
import { t } from 'fyo';
6+
import { BaseError } from 'fyo/utils/errors';
7+
import { BackendResponse } from 'utils/ipc/types';
68
import { IPC_ACTIONS, IPC_MESSAGES } from 'utils/messages';
79
import { setLanguageMap } from './language';
8-
import { showToast } from './ui';
10+
import { showMessageDialog, showToast } from './ui';
911

1012
export async function checkForUpdates() {
1113
await ipcRenderer.invoke(IPC_ACTIONS.CHECK_FOR_UPDATES);
@@ -17,7 +19,32 @@ export async function openLink(link: string) {
1719
}
1820

1921
export async function deleteDb(filePath: string) {
20-
await ipcRenderer.invoke(IPC_ACTIONS.DELETE_FILE, filePath);
22+
const { error } = (await ipcRenderer.invoke(
23+
IPC_ACTIONS.DELETE_FILE,
24+
filePath
25+
)) as BackendResponse;
26+
27+
if (error?.code === 'EBUSY') {
28+
showMessageDialog({
29+
message: t`Delete Failed`,
30+
detail: t`Please restart and try again`,
31+
});
32+
} else if (error?.code === 'ENOENT') {
33+
showMessageDialog({
34+
message: t`Delete Failed`,
35+
detail: t`File ${filePath} does not exist`,
36+
});
37+
} else if (error?.code === 'EPERM') {
38+
showMessageDialog({
39+
message: t`Cannot Delete`,
40+
detail: t`Close Frappe Books and try manually`,
41+
});
42+
} else if (error) {
43+
const err = new BaseError(500, error.message);
44+
err.name = error.name;
45+
err.stack = error.stack;
46+
throw err;
47+
}
2148
}
2249

2350
export async function saveData(data: string, savePath: string) {

utils/ipc/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export interface DatabaseResponse {
1+
export interface BackendResponse {
22
data?: unknown;
3-
error?: { message: string; name: string; stack?: string };
3+
error?: { message: string; name: string; stack?: string; code?: string };
44
}

0 commit comments

Comments
 (0)