2
0
mirror of https://github.com/frappe/books.git synced 2024-12-22 10:58:59 +00:00

fix(ux): handle db delete exception

This commit is contained in:
18alantom 2022-07-30 17:17:37 +05:30
parent 5c5dc09d58
commit 3904d04f7d
5 changed files with 40 additions and 12 deletions

View File

@ -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<DatabaseResponse>): Promise<unknown> {
async #handleDBCall(func: () => Promise<BackendResponse>): Promise<unknown> {
const response = await func();
if (response.error?.name) {

View File

@ -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<unknown>) {
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,
};
}

View File

@ -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 (_) => {

View File

@ -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) {

View File

@ -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 };
}