1- import { app , ipcMain } from 'electron'
1+ import { app , dialog , ipcMain } from 'electron'
22import contextMenu from 'electron-context-menu'
33import debug from 'electron-debug'
44import pkg from 'electron-updater'
@@ -11,6 +11,7 @@ import { registerSessionHandlers } from './ipc/sessionHandlers.ts'
1111import { initializePaths } from './paths.ts'
1212import { createAuthWindow , createMainWindow } from './window.ts'
1313
14+ import type { AppPaths } from './paths.ts'
1415import type { BrowserWindow } from 'electron'
1516
1617const { autoUpdater } = pkg
@@ -22,31 +23,41 @@ debug({ showDevTools: false, isEnabled: true })
2223// Environment variables
2324const DEV_SERVER_URL = process . env . DEV_SERVER_URL
2425
25- // Initialize paths once
26- const paths = initializePaths ( )
27-
2826// Main window reference
2927let mainWindow : BrowserWindow | null = null
3028
29+ // Paths - initialized after app is ready
30+ let paths : AppPaths
31+
3132function getMainWindow ( ) {
3233 return mainWindow
3334}
3435
36+ /**
37+ * Shows an error dialog to the user and optionally quits the app
38+ */
39+ function showFatalError ( title : string , error : unknown , shouldQuit = true ) {
40+ const message = error instanceof Error ? error . message : String ( error )
41+ const detail = error instanceof Error ? error . stack : undefined
42+ console . error ( `${ title } :` , error )
43+
44+ dialog . showErrorBox ( title , detail ? `${ message } \n\n${ detail } ` : message )
45+
46+ if ( shouldQuit ) {
47+ app . quit ( )
48+ }
49+ }
50+
3551/**
3652 * Creates the main window and initializes the application
3753 */
3854async function initialize ( ) {
39- try {
40- await initializeFileSystem ( paths )
41- mainWindow = await createMainWindow ( autoUpdater , DEV_SERVER_URL )
55+ await initializeFileSystem ( paths )
56+ mainWindow = await createMainWindow ( autoUpdater , DEV_SERVER_URL )
4257
43- mainWindow . on ( 'closed' , ( ) => {
44- mainWindow = null
45- } )
46- } catch ( error ) {
47- console . error ( 'Failed to initialize application:' , error )
48- throw error
49- }
58+ mainWindow . on ( 'closed' , ( ) => {
59+ mainWindow = null
60+ } )
5061}
5162
5263/**
@@ -72,15 +83,19 @@ function registerIpcHandlers() {
7283 )
7384}
7485
75- // Setup auto-updater
86+ // Setup auto-updater (just registers event listeners, safe before ready)
7687setupAutoUpdater ( autoUpdater , getMainWindow )
7788
78- // Register IPC handlers
79- registerIpcHandlers ( )
80-
8189// App lifecycle handlers
8290app . on ( 'ready' , async ( ) => {
83- await initialize ( )
91+ try {
92+ // Initialize paths after app is ready to ensure app.getPath() works reliably
93+ paths = initializePaths ( )
94+ registerIpcHandlers ( )
95+ await initialize ( )
96+ } catch ( error ) {
97+ showFatalError ( 'Failed to initialize application' , error )
98+ }
8499} )
85100
86101app . on ( 'window-all-closed' , ( ) => {
@@ -91,6 +106,10 @@ app.on('window-all-closed', () => {
91106
92107app . on ( 'activate' , async ( ) => {
93108 if ( mainWindow === null ) {
94- await initialize ( )
109+ try {
110+ await initialize ( )
111+ } catch ( error ) {
112+ showFatalError ( 'Failed to create window' , error )
113+ }
95114 }
96115} )
0 commit comments