From 3904d04f7d24e97a70e2ab6bbc773376da13c899 Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Sat, 30 Jul 2022 17:17:37 +0530 Subject: [PATCH] fix(ux): handle db delete exception --- fyo/demux/db.ts | 4 ++-- main/helpers.ts | 11 ++++----- main/registerIpcMainActionListeners.ts | 2 +- src/utils/ipcCalls.ts | 31 ++++++++++++++++++++++++-- utils/ipc/types.ts | 4 ++-- 5 files changed, 40 insertions(+), 12 deletions(-) diff --git a/fyo/demux/db.ts b/fyo/demux/db.ts index c9d050fe..43ad37ea 100644 --- a/fyo/demux/db.ts +++ b/fyo/demux/db.ts @@ -2,7 +2,7 @@ import { ipcRenderer } from 'electron'; import { DatabaseError, NotImplemented } from 'fyo/utils/errors'; import { SchemaMap } from 'schemas/types'; import { DatabaseDemuxBase, DatabaseMethod } from 'utils/db/types'; -import { DatabaseResponse } from 'utils/ipc/types'; +import { BackendResponse } from 'utils/ipc/types'; import { IPC_ACTIONS } from 'utils/messages'; export class DatabaseDemux extends DatabaseDemuxBase { @@ -12,7 +12,7 @@ export class DatabaseDemux extends DatabaseDemuxBase { this.#isElectron = isElectron; } - async #handleDBCall(func: () => Promise): Promise { + async #handleDBCall(func: () => Promise): Promise { const response = await func(); if (response.error?.name) { diff --git a/main/helpers.ts b/main/helpers.ts index d55066f3..25516db6 100644 --- a/main/helpers.ts +++ b/main/helpers.ts @@ -3,7 +3,7 @@ import fs from 'fs/promises'; import { ConfigFile, ConfigKeys } from 'fyo/core/types'; import { Main } from 'main'; import config from 'utils/config'; -import { DatabaseResponse } from 'utils/ipc/types'; +import { BackendResponse } from 'utils/ipc/types'; import { IPC_CHANNELS } from 'utils/messages'; interface ConfigFilesWithModified extends ConfigFile { @@ -54,15 +54,16 @@ export async function getConfigFilesWithModified(files: ConfigFile[]) { } export async function getErrorHandledReponse(func: () => Promise) { - const response: DatabaseResponse = {}; + const response: BackendResponse = {}; try { response.data = await func(); } catch (err) { response.error = { - name: (err as Error).name, - message: (err as Error).message, - stack: (err as Error).stack, + name: (err as NodeJS.ErrnoException).name, + message: (err as NodeJS.ErrnoException).message, + stack: (err as NodeJS.ErrnoException).stack, + code: (err as NodeJS.ErrnoException).code, }; } diff --git a/main/registerIpcMainActionListeners.ts b/main/registerIpcMainActionListeners.ts index 472ec60c..72163234 100644 --- a/main/registerIpcMainActionListeners.ts +++ b/main/registerIpcMainActionListeners.ts @@ -139,7 +139,7 @@ export default function registerIpcMainActionListeners(main: Main) { }); ipcMain.handle(IPC_ACTIONS.DELETE_FILE, async (_, filePath) => { - await fs.unlink(filePath); + return getErrorHandledReponse(async () => await fs.unlink(filePath)); }); ipcMain.handle(IPC_ACTIONS.GET_DB_LIST, async (_) => { diff --git a/src/utils/ipcCalls.ts b/src/utils/ipcCalls.ts index 49f29469..a972a1db 100644 --- a/src/utils/ipcCalls.ts +++ b/src/utils/ipcCalls.ts @@ -3,9 +3,11 @@ */ import { ipcRenderer } from 'electron'; import { t } from 'fyo'; +import { BaseError } from 'fyo/utils/errors'; +import { BackendResponse } from 'utils/ipc/types'; import { IPC_ACTIONS, IPC_MESSAGES } from 'utils/messages'; import { setLanguageMap } from './language'; -import { showToast } from './ui'; +import { showMessageDialog, showToast } from './ui'; export async function checkForUpdates() { await ipcRenderer.invoke(IPC_ACTIONS.CHECK_FOR_UPDATES); @@ -17,7 +19,32 @@ export async function openLink(link: string) { } export async function deleteDb(filePath: string) { - await ipcRenderer.invoke(IPC_ACTIONS.DELETE_FILE, filePath); + const { error } = (await ipcRenderer.invoke( + IPC_ACTIONS.DELETE_FILE, + filePath + )) as BackendResponse; + + if (error?.code === 'EBUSY') { + showMessageDialog({ + message: t`Delete Failed`, + detail: t`Please restart and try again`, + }); + } else if (error?.code === 'ENOENT') { + showMessageDialog({ + message: t`Delete Failed`, + detail: t`File ${filePath} does not exist`, + }); + } else if (error?.code === 'EPERM') { + showMessageDialog({ + message: t`Cannot Delete`, + detail: t`Close Frappe Books and try manually`, + }); + } else if (error) { + const err = new BaseError(500, error.message); + err.name = error.name; + err.stack = error.stack; + throw err; + } } export async function saveData(data: string, savePath: string) { diff --git a/utils/ipc/types.ts b/utils/ipc/types.ts index 964ff796..4ce76fc4 100644 --- a/utils/ipc/types.ts +++ b/utils/ipc/types.ts @@ -1,4 +1,4 @@ -export interface DatabaseResponse { +export interface BackendResponse { data?: unknown; - error?: { message: string; name: string; stack?: string }; + error?: { message: string; name: string; stack?: string; code?: string }; }