diff --git a/main.ts b/main.ts index 81611149..371c0c3c 100644 --- a/main.ts +++ b/main.ts @@ -139,16 +139,6 @@ export class Main { this.mainWindow.on('closed', () => { this.mainWindow = null; }); - - this.mainWindow.webContents.on('did-finish-load', () => { - if (this.mainWindow === null) { - return; - } - - this.mainWindow.webContents.send(IPC_CHANNELS.STORE_ON_WINDOW, { - isDevelopment: this.isDevelopment, - }); - }); } } diff --git a/main/registerIpcMainActionListeners.ts b/main/registerIpcMainActionListeners.ts index ea6438b5..659347c4 100644 --- a/main/registerIpcMainActionListeners.ts +++ b/main/registerIpcMainActionListeners.ts @@ -116,10 +116,6 @@ export default function registerIpcMainActionListeners(main: Main) { return await getUrlAndTokenString(); }); - ipcMain.handle(IPC_ACTIONS.GET_VERSION, (_) => { - return app.getVersion(); - }); - ipcMain.handle(IPC_ACTIONS.DELETE_FILE, async (_, filePath) => { await fs.unlink(filePath); }); @@ -129,6 +125,14 @@ export default function registerIpcMainActionListeners(main: Main) { return await getConfigFilesWithModified(files); }); + ipcMain.handle(IPC_ACTIONS.GET_ENV, async (_) => { + return { + isDevelopment: main.isDevelopment, + platform: process.platform, + version: app.getVersion(), + }; + }); + /** * Database Related Actions */ diff --git a/main/registerProcessListeners.ts b/main/registerProcessListeners.ts index c1519a01..8bb9394e 100644 --- a/main/registerProcessListeners.ts +++ b/main/registerProcessListeners.ts @@ -1,4 +1,5 @@ import { app } from 'electron'; +import { IPC_CHANNELS } from 'utils/messages'; import { Main } from '../main'; export default function registerProcessListeners(main: Main) { @@ -15,4 +16,13 @@ export default function registerProcessListeners(main: Main) { }); } } + + process.on('unhandledRejection', (error) => { + main.mainWindow!.webContents.send(IPC_CHANNELS.MAIN_PROCESS_ERROR, error); + }); + + process.on('uncaughtException', (error) => { + main.mainWindow!.webContents.send(IPC_CHANNELS.MAIN_PROCESS_ERROR, error); + setTimeout(() => process.exit(1), 10000); + }); } diff --git a/src/errorHandling.ts b/src/errorHandling.ts index 374eb45e..dff40664 100644 --- a/src/errorHandling.ts +++ b/src/errorHandling.ts @@ -21,7 +21,7 @@ function shouldNotStore(error: Error) { ); } -async function reportError(errorLogObj: ErrorLog, cb?: Function) { +async function reportError(errorLogObj: ErrorLog) { if (!errorLogObj.stack) { return; } @@ -38,10 +38,9 @@ async function reportError(errorLogObj: ErrorLog, cb?: Function) { } await ipcRenderer.invoke(IPC_ACTIONS.SEND_ERROR, JSON.stringify(body)); - cb?.(); } -function getToastProps(errorLogObj: ErrorLog, cb?: Function) { +function getToastProps(errorLogObj: ErrorLog) { const props: ToastOptions = { message: errorLogObj.name ?? t`Error`, type: 'error', @@ -68,8 +67,7 @@ export function getErrorLogObject( export async function handleError( shouldLog: boolean, error: Error, - more?: Record, - cb?: Function + more?: Record ) { if (shouldLog) { console.error(error); @@ -81,8 +79,8 @@ export async function handleError( const errorLogObj = getErrorLogObject(error, more ?? {}); - await reportError(errorLogObj, cb); - const toastProps = getToastProps(errorLogObj, cb); + await reportError(errorLogObj); + const toastProps = getToastProps(errorLogObj); await showToast(toastProps); } diff --git a/src/renderer.ts b/src/renderer.ts index cac61c41..e112583f 100644 --- a/src/renderer.ts +++ b/src/renderer.ts @@ -2,7 +2,6 @@ import { ipcRenderer } from 'electron'; import { ConfigKeys } from 'fyo/core/types'; import { DateTime } from 'luxon'; import { IPC_ACTIONS } from 'utils/messages'; -import { Version } from 'utils/version'; import { App as VueApp, createApp } from 'vue'; import App from './App.vue'; import Badge from './components/Badge.vue'; @@ -21,12 +20,18 @@ import { setLanguageMap } from './utils/language'; await setLanguageMap(language); } - setOnWindow(); - ipcRenderer.send = getErrorHandled(ipcRenderer.send); ipcRenderer.invoke = getErrorHandled(ipcRenderer.invoke); registerIpcRendererListeners(); + const { isDevelopment, platform, version } = (await ipcRenderer.invoke( + IPC_ACTIONS.GET_ENV + )) as { isDevelopment: boolean; platform: string; version: string }; + + fyo.store.isDevelopment = isDevelopment; + fyo.store.appVersion = version; + + setOnWindow(isDevelopment); const app = createApp({ template: '', @@ -45,7 +50,7 @@ import { setLanguageMap } from './utils/language'; return fyo; }, platform() { - switch (process.platform) { + switch (platform) { case 'win32': return 'Windows'; case 'darwin': @@ -63,7 +68,6 @@ import { setLanguageMap } from './utils/language'; }, }); - fyo.store.appVersion = await ipcRenderer.invoke(IPC_ACTIONS.GET_VERSION); app.mount('body'); })(); @@ -88,25 +92,17 @@ function setErrorHandlers(app: VueApp) { handleError(false, err as Error, more); console.error(err, vm, info); }; - - process.on('unhandledRejection', (error) => { - handleError(true, error as Error); - }); - - process.on('uncaughtException', (error) => { - handleError(true, error, {}, () => process.exit(1)); - }); } -function setOnWindow() { - if (process.env.NODE_ENV === 'development') { - // @ts-ignore - window.router = router; - // @ts-ignore - window.fyo = fyo; - // @ts-ignore - window.DateTime = DateTime; - // @ts-ignore - window.Version = Version; +function setOnWindow(isDevelopment: boolean) { + if (!isDevelopment) { + return; } + + // @ts-ignore + window.router = router; + // @ts-ignore + window.fyo = fyo; + // @ts-ignore + window.DateTime = DateTime; } diff --git a/src/renderer/registerIpcRendererListeners.ts b/src/renderer/registerIpcRendererListeners.ts index 1c5c5ed7..14dd0d8c 100644 --- a/src/renderer/registerIpcRendererListeners.ts +++ b/src/renderer/registerIpcRendererListeners.ts @@ -5,10 +5,6 @@ import { showToast } from 'src/utils/ui'; import { IPC_CHANNELS, IPC_MESSAGES } from 'utils/messages'; export default function registerIpcRendererListeners() { - ipcRenderer.on(IPC_CHANNELS.STORE_ON_WINDOW, (event, message) => { - Object.assign(fyo.store, message); - }); - ipcRenderer.on(IPC_CHANNELS.CHECKING_FOR_UPDATE, (_) => { showToast({ message: fyo.t`Checking for updates` }); }); @@ -48,9 +44,15 @@ export default function registerIpcRendererListeners() { }); }); - ipcRenderer.on(IPC_CHANNELS.UPDATE_ERROR, (_, error) => { + ipcRenderer.on(IPC_CHANNELS.UPDATE_ERROR, async (_, error) => { error.name = 'Updation Error'; - handleError(true, error); + await handleError(true, error as Error); + }); + + ipcRenderer.on(IPC_CHANNELS.MAIN_PROCESS_ERROR, async (_, error) => { + console.error('main process error'); + console.error(error); + await handleError(true, error as Error); }); document.addEventListener('visibilitychange', function () { diff --git a/utils/messages.ts b/utils/messages.ts index 1ac7fcad..0ca4ee8d 100644 --- a/utils/messages.ts +++ b/utils/messages.ts @@ -17,6 +17,7 @@ export enum IPC_ACTIONS { GET_OPEN_FILEPATH = 'open-dialog', GET_SAVE_FILEPATH = 'save-dialog', GET_DIALOG_RESPONSE = 'show-message-box', + GET_ENV = 'get-env', SAVE_HTML_AS_PDF = 'save-html-as-pdf', SAVE_DATA = 'save-data', SHOW_ERROR = 'show-error', @@ -25,7 +26,6 @@ export enum IPC_ACTIONS { CHECK_FOR_UPDATES = 'check-for-updates', GET_FILE = 'get-file', GET_CREDS = 'get-creds', - GET_VERSION = 'get-version', GET_DB_LIST = 'get-db-list', DELETE_FILE = 'delete-file', // Database messages @@ -38,12 +38,12 @@ export enum IPC_ACTIONS { // ipcMain.send(...) export enum IPC_CHANNELS { - STORE_ON_WINDOW = 'store-on-window', CHECKING_FOR_UPDATE = 'checking-for-update', UPDATE_AVAILABLE = 'update-available', UPDATE_NOT_AVAILABLE = 'update-not-available', UPDATE_DOWNLOADED = 'update-downloaded', UPDATE_ERROR = 'update-error', + MAIN_PROCESS_ERROR = 'main-process-error', } export enum DB_CONN_FAILURE {