Skip to content

Commit 5540c4a

Browse files
committed
refactor: add centralized error logging and handling across main and renderer process
1 parent d46d90b commit 5540c4a

5 files changed

Lines changed: 85 additions & 17 deletions

File tree

src/main/db/index.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Database from 'better-sqlite3'
55
import { format } from 'date-fns'
66
import fs from 'fs-extra'
77
import { store } from '../store'
8+
import { log } from '../utils'
89

910
const DB_NAME = 'massCode.db'
1011
const isDev = process.env.NODE_ENV === 'development'
@@ -136,7 +137,7 @@ export function useDB() {
136137
return db
137138
}
138139
catch (error) {
139-
console.error('Database initialization failed:', error)
140+
log('Database initialization failed', error)
140141
throw error
141142
}
142143
}
@@ -167,7 +168,7 @@ export function reloadDB() {
167168
console.log(`Database successfully reloaded: ${dbPath}`)
168169
}
169170
catch (error) {
170-
console.error('Error while reloading the database:', error)
171+
log('Error while reloading the database', error)
171172
throw error
172173
}
173174
}
@@ -201,7 +202,7 @@ export function clearDB() {
201202
stmt()
202203
}
203204
catch (error) {
204-
console.error('Error while clearing the database:', error)
205+
log('Error while clearing the database', error)
205206
throw error
206207
}
207208
}
@@ -228,7 +229,7 @@ export async function moveDB(path: string) {
228229
reloadDB()
229230
}
230231
catch (error) {
231-
console.error('Error while moving the database:', error)
232+
log('Error while moving the database', error)
232233
throw error
233234
}
234235
}
@@ -256,7 +257,7 @@ export async function createBackup(manual = false) {
256257
return backupFilePath
257258
}
258259
catch (error) {
259-
console.error('Error creating database backup:', error)
260+
log('Error creating database backup', error)
260261
throw error
261262
}
262263
}
@@ -338,12 +339,12 @@ export async function startAutoBackup() {
338339
}
339340
}
340341
catch (error) {
341-
console.error('Error during scheduled backup:', error)
342+
log('Error during scheduled backup', error)
342343
}
343344
}, intervalMs)
344345
}
345346
catch (error) {
346-
console.error('Error starting auto backup:', error)
347+
log('Error starting auto backup', error)
347348
}
348349
}
349350

@@ -377,7 +378,7 @@ export async function restoreFromBackup(backupFilePath: string) {
377378
console.warn(`Database restored from backup: ${backupFilePath}`)
378379
}
379380
catch (error) {
380-
console.error('Error restoring database from backup:', error)
381+
log('Error restoring database from backup', error)
381382
throw error
382383
}
383384
}
@@ -415,7 +416,7 @@ export async function getBackupList() {
415416
)
416417
}
417418
catch (error) {
418-
console.error('Error getting backup list:', error)
419+
log('Error getting backup list', error)
419420
return []
420421
}
421422
}
@@ -454,7 +455,7 @@ export async function moveBackupStorage(newPath: string) {
454455
store.preferences.set('backup.path', newPath)
455456
}
456457
catch (error) {
457-
console.error('Error while moving backup storage:', error)
458+
log('Error while moving backup storage', error)
458459
throw error
459460
}
460461
}

src/main/index.ts

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { registerIPC } from './ipc'
99
import { mainMenu } from './menu/main'
1010
import { store } from './store'
1111
import { checkForUpdates } from './updates'
12+
import { log } from './utils'
1213

1314
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = 'true' // Отключаем security warnings
1415

@@ -73,12 +74,41 @@ if (!gotTheLock) {
7374
app.quit()
7475
}
7576
else {
76-
app.whenReady().then(() => {
77-
createWindow()
78-
registerIPC()
79-
initApi()
80-
checkForUpdates()
81-
startAutoBackup()
77+
app.whenReady().then(async () => {
78+
try {
79+
createWindow()
80+
}
81+
catch (error) {
82+
log('Error creating window', error)
83+
}
84+
85+
try {
86+
registerIPC()
87+
}
88+
catch (error) {
89+
log('Error registering IPC', error)
90+
}
91+
92+
try {
93+
initApi()
94+
}
95+
catch (error) {
96+
log('Error initializing API', error)
97+
}
98+
99+
try {
100+
checkForUpdates()
101+
}
102+
catch (error) {
103+
log('Error checking for updates', error)
104+
}
105+
106+
try {
107+
await startAutoBackup()
108+
}
109+
catch (error) {
110+
log('Error starting auto backup', error)
111+
}
82112

83113
if (store.app.get('isAutoMigratedFromJson')) {
84114
return
@@ -92,7 +122,7 @@ else {
92122
store.app.set('isAutoMigratedFromJson', true)
93123
}
94124
catch (err) {
95-
console.error('Error on auto migration JSON to SQLite:', err)
125+
log('Error on auto migration JSON to SQLite', err)
96126
}
97127
})
98128

@@ -126,4 +156,22 @@ else {
126156
app.on('open-url', (_, url) => {
127157
BrowserWindow.getFocusedWindow()?.webContents.send('system:deep-link', url)
128158
})
159+
160+
// Global error handlers
161+
process.on('uncaughtException', (err) => {
162+
BrowserWindow.getFocusedWindow()?.webContents.send('system:error', {
163+
source: 'main',
164+
message: err.message,
165+
stack: err.stack,
166+
})
167+
})
168+
169+
process.on('unhandledRejection', (reason) => {
170+
const err = reason instanceof Error ? reason : new Error(String(reason))
171+
BrowserWindow.getFocusedWindow()?.webContents.send('system:error', {
172+
source: 'main',
173+
message: err.message,
174+
stack: err.stack,
175+
})
176+
})
129177
}

src/main/types/ipc.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type SystemAction =
4141
| 'open-external'
4242
| 'deep-link'
4343
| 'update-available'
44+
| 'error'
4445
type PrettierAction = 'format'
4546
type FsAction = 'assets'
4647

src/main/utils/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { BrowserWindow } from 'electron'
2+
3+
export function log(context: string, error: unknown): void {
4+
const message = error instanceof Error ? error.message : String(error)
5+
const stack = error instanceof Error ? error.stack : undefined
6+
7+
console.error(`[${context}] ${message}`, error)
8+
9+
BrowserWindow.getFocusedWindow()?.webContents.send('system:error', {
10+
context,
11+
message,
12+
stack,
13+
})
14+
}

src/renderer/ipc/listeners/system.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,8 @@ export function registerSystemListeners() {
4848
},
4949
})
5050
})
51+
52+
ipc.on('system:error', (_, payload) => {
53+
console.error(`[system][${payload.context}]`, payload)
54+
})
5155
}

0 commit comments

Comments
 (0)