From cd234f8826adfa50b176707e3e5fd99d976a34bd Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Wed, 31 Aug 2022 14:25:25 +0530 Subject: [PATCH] fix: better error handling on updation fails - ask user whether to restart post update download --- backend/database/manager.ts | 2 +- backend/helpers.ts | 7 ++++-- main/registerAutoUpdaterListeners.ts | 25 ++++++++++++++------ main/registerIpcMainActionListeners.ts | 13 +++++++--- main/registerProcessListeners.ts | 5 ++-- src/renderer/registerIpcRendererListeners.ts | 15 +++++++----- 6 files changed, 46 insertions(+), 21 deletions(-) diff --git a/backend/database/manager.ts b/backend/database/manager.ts index f97f69c2..3d86a612 100644 --- a/backend/database/manager.ts +++ b/backend/database/manager.ts @@ -5,7 +5,7 @@ import { getSchemas } from '../../schemas'; import { databaseMethodSet, emitMainProcessError, - unlinkIfExists + unlinkIfExists, } from '../helpers'; import patches from '../patches'; import { BespokeQueries } from './bespoke'; diff --git a/backend/helpers.ts b/backend/helpers.ts index ae2f0df1..88f6ec3e 100644 --- a/backend/helpers.ts +++ b/backend/helpers.ts @@ -51,8 +51,11 @@ export const databaseMethodSet: Set = new Set([ 'exists', ]); -export function emitMainProcessError(error: unknown) { - (process.emit as Function)(CUSTOM_EVENTS.MAIN_PROCESS_ERROR, error); +export function emitMainProcessError( + error: unknown, + more?: Record +) { + (process.emit as Function)(CUSTOM_EVENTS.MAIN_PROCESS_ERROR, error, more); } export async function checkFileAccess(filePath: string, mode?: number) { diff --git a/main/registerAutoUpdaterListeners.ts b/main/registerAutoUpdaterListeners.ts index 811c117c..ef858ecf 100644 --- a/main/registerAutoUpdaterListeners.ts +++ b/main/registerAutoUpdaterListeners.ts @@ -1,7 +1,7 @@ +import { emitMainProcessError } from 'backend/helpers'; import { app, dialog } from 'electron'; import { autoUpdater, UpdateInfo } from 'electron-updater'; import { Main } from '../main'; -import { IPC_CHANNELS } from '../utils/messages'; export default function registerAutoUpdaterListeners(main: Main) { autoUpdater.autoDownload = false; @@ -13,11 +13,7 @@ export default function registerAutoUpdaterListeners(main: Main) { return; } - main.mainWindow!.webContents.send(IPC_CHANNELS.LOG_MAIN_PROCESS_ERROR, error); - dialog.showErrorBox( - 'Update Error: ', - error == null ? 'unknown' : (error.stack || error).toString() - ); + emitMainProcessError(error); }); autoUpdater.on('update-available', async (info: UpdateInfo) => { @@ -30,7 +26,7 @@ export default function registerAutoUpdaterListeners(main: Main) { if (!isCurrentBeta && isNextBeta) { const option = await dialog.showMessageBox({ type: 'info', - title: `Update Frappe Books?`, + title: 'Update Available', message: `Download version ${nextVersion}?`, buttons: ['Yes', 'No'], }); @@ -44,4 +40,19 @@ export default function registerAutoUpdaterListeners(main: Main) { await autoUpdater.downloadUpdate(); }); + + autoUpdater.on('update-downloaded', async () => { + const option = await dialog.showMessageBox({ + type: 'info', + title: 'Update Downloaded', + message: 'Restart Frappe Books to install update?', + buttons: ['Yes', 'No'], + }); + + if (option.response === 1) { + return; + } + + autoUpdater.quitAndInstall(); + }); } diff --git a/main/registerIpcMainActionListeners.ts b/main/registerIpcMainActionListeners.ts index 4b386b38..8205e191 100644 --- a/main/registerIpcMainActionListeners.ts +++ b/main/registerIpcMainActionListeners.ts @@ -1,3 +1,4 @@ +import { emitMainProcessError } from 'backend/helpers'; import { app, dialog, ipcMain } from 'electron'; import { autoUpdater } from 'electron-updater'; import fs from 'fs/promises'; @@ -52,10 +53,16 @@ export default function registerIpcMainActionListeners(main: Main) { }); ipcMain.handle(IPC_ACTIONS.CHECK_FOR_UPDATES, async () => { - if (!main.isDevelopment && !main.checkedForUpdate) { - await autoUpdater.checkForUpdates(); - main.checkedForUpdate = true; + if (main.isDevelopment || main.checkedForUpdate) { + return; } + + try { + await autoUpdater.checkForUpdates(); + } catch (error) { + emitMainProcessError(error); + } + main.checkedForUpdate = true; }); ipcMain.handle(IPC_ACTIONS.GET_LANGUAGE_MAP, async (event, code) => { diff --git a/main/registerProcessListeners.ts b/main/registerProcessListeners.ts index 0622717a..6b42c929 100644 --- a/main/registerProcessListeners.ts +++ b/main/registerProcessListeners.ts @@ -17,10 +17,11 @@ export default function registerProcessListeners(main: Main) { } } - process.on(CUSTOM_EVENTS.MAIN_PROCESS_ERROR, (error) => { + process.on(CUSTOM_EVENTS.MAIN_PROCESS_ERROR, (error, more) => { main.mainWindow!.webContents.send( IPC_CHANNELS.LOG_MAIN_PROCESS_ERROR, - error + error, + more ); }); diff --git a/src/renderer/registerIpcRendererListeners.ts b/src/renderer/registerIpcRendererListeners.ts index 45b865fb..010d63b8 100644 --- a/src/renderer/registerIpcRendererListeners.ts +++ b/src/renderer/registerIpcRendererListeners.ts @@ -4,13 +4,16 @@ import { fyo } from 'src/initFyo'; import { IPC_CHANNELS } from 'utils/messages'; export default function registerIpcRendererListeners() { - ipcRenderer.on(IPC_CHANNELS.LOG_MAIN_PROCESS_ERROR, async (_, error) => { - if (fyo.store.isDevelopment) { - console.error(error); - } + ipcRenderer.on( + IPC_CHANNELS.LOG_MAIN_PROCESS_ERROR, + async (_, error, more) => { + if (fyo.store.isDevelopment) { + console.error(error); + } - await handleError(true, error as Error); - }); + await handleError(true, error as Error, more); + } + ); ipcRenderer.on(IPC_CHANNELS.CONSOLE_LOG, (_, ...stuff: unknown[]) => { if (!fyo.store.isDevelopment) {