From 2cfc943229e5b9f273bee94f653a1bd8b5875125 Mon Sep 17 00:00:00 2001 From: fullex <0xfullex@gmail.com> Date: Thu, 15 May 2025 15:44:01 +0800 Subject: [PATCH] feat: implement global error handling for uncaught exceptions and unhandled rejections in production mode --- src/main/index.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index 41c48cd977..d4cd944d8a 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -5,6 +5,7 @@ import { app, BrowserWindow, ipcMain } from 'electron' import installExtension, { REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS } from 'electron-devtools-installer' import Logger from 'electron-log' +import { isDev, isMac, isWin } from './constant' import { registerIpc } from './ipc' import { configManager } from './services/ConfigManager' import mcpService from './services/MCPService' @@ -21,6 +22,19 @@ import { setUserDataDir } from './utils/file' Logger.initialize() +// in production mode, handle uncaught exception and unhandled rejection globally +if (!isDev) { + // handle uncaught exception + process.on('uncaughtException', (error) => { + Logger.error('Uncaught Exception:', error) + }) + + // handle unhandled rejection + process.on('unhandledRejection', (reason, promise) => { + Logger.error('Unhandled Rejection at:', promise, 'reason:', reason) + }) +} + // Check for single instance lock if (!app.requestSingleInstanceLock()) { app.quit() @@ -63,13 +77,13 @@ if (!app.requestSingleInstanceLock()) { // Setup deep link for AppImage on Linux await setupAppImageDeepLink() - if (process.env.NODE_ENV === 'development') { + if (isDev) { installExtension([REDUX_DEVTOOLS, REACT_DEVELOPER_TOOLS]) .then((name) => console.log(`Added Extension: ${name}`)) .catch((err) => console.log('An error occurred: ', err)) } ipcMain.handle(IpcChannel.System_GetDeviceType, () => { - return process.platform === 'darwin' ? 'mac' : process.platform === 'win32' ? 'windows' : 'linux' + return isMac ? 'mac' : isWin ? 'windows' : 'linux' }) ipcMain.handle(IpcChannel.System_GetHostname, () => {