Skip to content

Commit

Permalink
Electron API: Add sentry capture and get electron version. (#175)
Browse files Browse the repository at this point in the history
* Electron API for version.

* Send error to sentry.
  • Loading branch information
robinjhuang authored Nov 5, 2024
1 parent 4d06e9b commit 5f628cf
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export const IPC_CHANNELS = {
CANCEL_DOWNLOAD: 'cancel-download',
DELETE_MODEL: 'delete-model',
GET_ALL_DOWNLOADS: 'get-all-downloads',
GET_ELECTRON_VERSION: 'get-electron-version',
SEND_ERROR_TO_SENTRY: 'send-error-to-sentry',
} as const;

export const COMFY_ERROR_MESSAGE =
Expand Down
50 changes: 35 additions & 15 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { spawn, ChildProcess } from 'node:child_process';
import * as fsPromises from 'node:fs/promises';
import fs from 'fs';
import axios from 'axios';
import path from 'node:path';
Expand Down Expand Up @@ -33,7 +32,7 @@ let mainWindow: BrowserWindow | null = null;
let store: Store<StoreType> | null = null;
const messageQueue: Array<any> = []; // Stores mesaages before renderer is ready.
let downloadManager: DownloadManager;

Sentry.captureMessage('Hello, world!');
log.initialize();

const comfySettings = new ComfySettings(app.getPath('documents'));
Expand Down Expand Up @@ -91,18 +90,24 @@ if (!gotTheLock) {
}
});

app.isPackaged &&
comfySettings.sendCrashStatistics &&
Sentry.init({
dsn: SENTRY_URL_ENDPOINT,
autoSessionTracking: false,
integrations: [
Sentry.childProcessIntegration({
breadcrumbs: ['abnormal-exit', 'killed', 'crashed', 'launch-failed', 'oom', 'integrity-failure'],
events: ['abnormal-exit', 'killed', 'crashed', 'launch-failed', 'oom', 'integrity-failure'],
}),
],
});
Sentry.init({
dsn: SENTRY_URL_ENDPOINT,
autoSessionTracking: false,
beforeSend(event, hint) {
if (event.extra?.comfyUIExecutionError) {
return event;
}

//TODO (use default pop up behavior).
return event;
},
integrations: [
Sentry.childProcessIntegration({
breadcrumbs: ['abnormal-exit', 'killed', 'crashed', 'launch-failed', 'oom', 'integrity-failure'],
events: ['abnormal-exit', 'killed', 'crashed', 'launch-failed', 'oom', 'integrity-failure'],
}),
],
});

graphics()
.then((graphicsInfo) => {
Expand Down Expand Up @@ -191,7 +196,6 @@ if (!gotTheLock) {
sendProgressUpdate('Setting up Python Environment...');
const pythonEnvironment = new PythonEnvironment(pythonInstallPath, appResourcesPath, spawnPythonAsync);
await pythonEnvironment.setup();

SetupTray(
mainWindow,
basePath,
Expand Down Expand Up @@ -221,6 +225,22 @@ if (!gotTheLock) {
}
}
);

ipcMain.handle(IPC_CHANNELS.GET_ELECTRON_VERSION, () => {
return app.getVersion();
});

ipcMain.handle(IPC_CHANNELS.SEND_ERROR_TO_SENTRY, async (_event, { error, extras }): Promise<string | null> => {
try {
return Sentry.captureMessage(error, {
level: 'error',
extra: { ...extras, comfyUIExecutionError: true },
});
} catch (err) {
log.error('Failed to send error to Sentry:', err);
return null;
}
});
});
}

Expand Down
19 changes: 19 additions & 0 deletions src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ export interface ElectronAPI {
deleteModel: (filename: string, path: string) => Promise<boolean>;
getAllDownloads: () => Promise<DownloadItem[]>;
};
/**
* Get the current Electron version
*/
getElectronVersion: () => Promise<string>;
/**
* Send an error message to Sentry
* @param error The error object or message to send
* @param extras Optional additional context/data to attach
*/
sendErrorToSentry: (error: string, extras?: Record<string, any>) => Promise<void>;
}

const electronAPI: ElectronAPI = {
Expand Down Expand Up @@ -126,6 +136,15 @@ const electronAPI: ElectronAPI = {
return ipcRenderer.invoke(IPC_CHANNELS.GET_ALL_DOWNLOADS);
},
},
getElectronVersion: () => {
return ipcRenderer.invoke(IPC_CHANNELS.GET_ELECTRON_VERSION);
},
sendErrorToSentry: (error: string, extras?: Record<string, any>) => {
return ipcRenderer.invoke(IPC_CHANNELS.SEND_ERROR_TO_SENTRY, {
error: error,
extras,
});
},
};

contextBridge.exposeInMainWorld(ELECTRON_BRIDGE_API, electronAPI);

0 comments on commit 5f628cf

Please sign in to comment.