mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-25 19:15:18 +02:00
refactor(desktop): extract error handling into modules/errors.ts
This commit is contained in:
parent
e7b5b37404
commit
dff3440f72
2 changed files with 36 additions and 30 deletions
|
|
@ -1,37 +1,10 @@
|
||||||
import { app, BrowserWindow, shell, ipcMain, session, dialog, clipboard, Menu } from 'electron';
|
import { app, BrowserWindow, shell, ipcMain, session, dialog, Menu } from 'electron';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { getPort } from 'get-port-please';
|
import { getPort } from 'get-port-please';
|
||||||
import { autoUpdater } from 'electron-updater';
|
import { autoUpdater } from 'electron-updater';
|
||||||
|
import { registerGlobalErrorHandlers, showErrorDialog } from './modules/errors';
|
||||||
|
|
||||||
function showErrorDialog(title: string, error: unknown): void {
|
registerGlobalErrorHandlers();
|
||||||
const err = error instanceof Error ? error : new Error(String(error));
|
|
||||||
console.error(`${title}:`, err);
|
|
||||||
|
|
||||||
if (app.isReady()) {
|
|
||||||
const detail = err.stack || err.message;
|
|
||||||
const buttonIndex = dialog.showMessageBoxSync({
|
|
||||||
type: 'error',
|
|
||||||
buttons: ['OK', process.platform === 'darwin' ? 'Copy Error' : 'Copy error'],
|
|
||||||
defaultId: 0,
|
|
||||||
noLink: true,
|
|
||||||
message: title,
|
|
||||||
detail,
|
|
||||||
});
|
|
||||||
if (buttonIndex === 1) {
|
|
||||||
clipboard.writeText(`${title}\n${detail}`);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
dialog.showErrorBox(title, err.stack || err.message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
process.on('uncaughtException', (error) => {
|
|
||||||
showErrorDialog('Unhandled Error', error);
|
|
||||||
});
|
|
||||||
|
|
||||||
process.on('unhandledRejection', (reason) => {
|
|
||||||
showErrorDialog('Unhandled Promise Rejection', reason);
|
|
||||||
});
|
|
||||||
|
|
||||||
const isDev = !app.isPackaged;
|
const isDev = !app.isPackaged;
|
||||||
let mainWindow: BrowserWindow | null = null;
|
let mainWindow: BrowserWindow | null = null;
|
||||||
|
|
|
||||||
33
surfsense_desktop/src/modules/errors.ts
Normal file
33
surfsense_desktop/src/modules/errors.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
import { app, clipboard, dialog } from 'electron';
|
||||||
|
|
||||||
|
export function showErrorDialog(title: string, error: unknown): void {
|
||||||
|
const err = error instanceof Error ? error : new Error(String(error));
|
||||||
|
console.error(`${title}:`, err);
|
||||||
|
|
||||||
|
if (app.isReady()) {
|
||||||
|
const detail = err.stack || err.message;
|
||||||
|
const buttonIndex = dialog.showMessageBoxSync({
|
||||||
|
type: 'error',
|
||||||
|
buttons: ['OK', process.platform === 'darwin' ? 'Copy Error' : 'Copy error'],
|
||||||
|
defaultId: 0,
|
||||||
|
noLink: true,
|
||||||
|
message: title,
|
||||||
|
detail,
|
||||||
|
});
|
||||||
|
if (buttonIndex === 1) {
|
||||||
|
clipboard.writeText(`${title}\n${detail}`);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
dialog.showErrorBox(title, err.stack || err.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerGlobalErrorHandlers(): void {
|
||||||
|
process.on('uncaughtException', (error) => {
|
||||||
|
showErrorDialog('Unhandled Error', error);
|
||||||
|
});
|
||||||
|
|
||||||
|
process.on('unhandledRejection', (reason) => {
|
||||||
|
showErrorDialog('Unhandled Promise Rejection', reason);
|
||||||
|
});
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue