feat: add Sentry error capture methods to ElectronAPI for renderer and main processes

This commit is contained in:
sudipnext 2026-04-06 11:29:08 +05:45
parent b1e3d826dd
commit b37aae4987
18 changed files with 1319 additions and 13 deletions

View file

@ -16,6 +16,7 @@ import { getPuppeteerExecutablePath, isChromeInstalled } from "./utils/puppeteer
import { getLiteParseRunnerPath } from "./utils/liteparse-check";
import { getImageMagickBinaryPath, isImageMagickInstalled } from "./utils/imagemagick-check";
import { startUpdateChecker, stopUpdateChecker } from "./utils/update-checker";
import { captureMainSentryTestError, initMainSentry } from "./sentry/main";
var win: BrowserWindow | undefined;
@ -30,6 +31,11 @@ const startupStatus: Record<string, string> = {
// Allow renderer to query initial startup status as soon as it loads.
ipcMain.handle("startup:get-status", () => startupStatus);
ipcMain.handle("sentry:test-main-error", (_event, message?: string) => {
return captureMainSentryTestError(message);
});
initMainSentry();
app.commandLine.appendSwitch('gtk-version', '3');
@ -68,8 +74,23 @@ const createWindow = () => {
show: false, // Shown after LibreOffice check so "Skip" doesn't quit the app
icon: path.join(baseDir, "resources/ui/assets/images/presenton_short_filled.png"),
webPreferences: {
webSecurity: false,
preload: path.join(__dirname, 'preloads/index.js'),
webSecurity: false,
// Ensure a known preload path and explicit isolation settings so
// the `contextBridge` API is exposed reliably to renderer pages.
contextIsolation: true,
nodeIntegration: false,
sandbox: false,
preload: (() => {
const p = path.join(__dirname, 'preloads/index.js');
try {
if (!fs.existsSync(p)) {
console.warn(`[Presenton] Preload not found at ${p}`);
}
} catch (e) {
console.warn('[Presenton] Failed to stat preload path', e);
}
return p;
})(),
},
});

View file

@ -1,4 +1,5 @@
import { contextBridge, ipcRenderer } from 'electron';
import { captureRendererSentryTestError } from './sentry';
contextBridge.exposeInMainWorld('env', {
NEXT_PUBLIC_FAST_API: process.env.NEXT_PUBLIC_FAST_API || '',
@ -35,4 +36,6 @@ contextBridge.exposeInMainWorld('electron', {
onStartupStatus: (callback: (payload: { name: string; status: string }) => void) =>
ipcRenderer.on("startup:status", (_event, payload) => callback(payload)),
getStartupStatus: () => ipcRenderer.invoke("startup:get-status"),
captureSentryRendererTestError: (message?: string) => captureRendererSentryTestError(message),
captureSentryMainTestError: (message?: string) => ipcRenderer.invoke("sentry:test-main-error", message),
});

View file

@ -1,4 +1,5 @@
import { contextBridge, ipcRenderer } from "electron";
import "./sentry";
contextBridge.exposeInMainWorld("loInstaller", {
startInstall: () => ipcRenderer.invoke("lo:start-install"),

View file

@ -1,5 +1,6 @@
// Preload script for PPTX export browser window
// This script runs before the page loads and injects environment variables
import './sentry';
// Expose environment variables to the window
(window as any).env = {

View file

@ -0,0 +1,86 @@
import * as Sentry from '@sentry/electron/renderer';
let isSentryInitialized = false;
function parseBoolean(value: string | undefined, defaultValue: boolean): boolean {
if (value === undefined) {
return defaultValue;
}
return !['0', 'false', 'no', 'off'].includes(value.trim().toLowerCase());
}
function parseSampleRate(value: string | undefined, defaultValue: number): number {
if (!value) {
return defaultValue;
}
const parsed = Number.parseFloat(value);
if (Number.isNaN(parsed)) {
return defaultValue;
}
return Math.max(0, Math.min(1, parsed));
}
export function initRendererSentry(): void {
if (isSentryInitialized) {
return;
}
const dsn = 'https://48b091ed88ae147c0957a46a823c1449@o4509882707410944.ingest.us.sentry.io/4511171070394368';
const isEnabled = parseBoolean(process.env.SENTRY_ENABLED, true);
if (!isEnabled) {
return;
}
const enableTracing = parseBoolean(process.env.SENTRY_ENABLE_TRACING, true);
const enableReplay = parseBoolean(process.env.SENTRY_ENABLE_REPLAY, false);
const enableFeedback = parseBoolean(process.env.SENTRY_ENABLE_FEEDBACK, false);
const integrations: any[] = [];
if (enableTracing) {
integrations.push(Sentry.browserTracingIntegration());
}
if (enableReplay) {
integrations.push(Sentry.replayIntegration());
}
if (enableFeedback) {
integrations.push(
Sentry.feedbackIntegration({
colorScheme: 'system',
}),
);
}
Sentry.init({
dsn,
enableLogs: parseBoolean(process.env.SENTRY_ENABLE_LOGS, true),
sendDefaultPii: parseBoolean(process.env.SENTRY_SEND_DEFAULT_PII, false),
tracesSampleRate: enableTracing
? parseSampleRate(process.env.SENTRY_TRACES_SAMPLE_RATE, 1.0)
: undefined,
replaysSessionSampleRate: enableReplay
? parseSampleRate(process.env.SENTRY_REPLAYS_SESSION_SAMPLE_RATE, 0.1)
: undefined,
replaysOnErrorSampleRate: enableReplay
? parseSampleRate(process.env.SENTRY_REPLAYS_ON_ERROR_SAMPLE_RATE, 1.0)
: undefined,
integrations,
});
isSentryInitialized = true;
Sentry.setTag('process.type', 'renderer');
}
export function captureRendererSentryTestError(message?: string): string | null {
if (!isSentryInitialized) {
return null;
}
const error = new Error(message || 'Sentry test error in renderer process');
return Sentry.captureException(error);
}
initRendererSentry();

View file

@ -1,4 +1,5 @@
import { contextBridge, ipcRenderer } from "electron";
import "./sentry";
contextBridge.exposeInMainWorld("setupInstaller", {
getStatus: () => ipcRenderer.invoke("setup:get-status"),

View file

@ -0,0 +1,92 @@
import { app } from "electron";
import * as Sentry from "@sentry/electron/main";
let isSentryInitialized = false;
function parseBoolean(value: string | undefined, defaultValue: boolean): boolean {
if (value === undefined) {
return defaultValue;
}
return !["0", "false", "no", "off"].includes(value.trim().toLowerCase());
}
function parseSampleRate(value: string | undefined, defaultValue: number): number {
if (!value) {
return defaultValue;
}
const parsed = Number.parseFloat(value);
if (Number.isNaN(parsed)) {
return defaultValue;
}
return Math.max(0, Math.min(1, parsed));
}
function getEnvironment(): string {
if (process.env.SENTRY_ENVIRONMENT) {
return process.env.SENTRY_ENVIRONMENT;
}
if (process.env.NODE_ENV) {
return process.env.NODE_ENV;
}
return app.isPackaged ? "production" : "development";
}
function getRelease(): string {
if (process.env.SENTRY_RELEASE) {
return process.env.SENTRY_RELEASE;
}
return `presenton-electron@${app.getVersion()}`;
}
export function initMainSentry(): void {
if (isSentryInitialized) {
return;
}
const dsn = "https://48b091ed88ae147c0957a46a823c1449@o4509882707410944.ingest.us.sentry.io/4511171070394368";
const isEnabled = parseBoolean(process.env.SENTRY_ENABLED, true);
if (!isEnabled) {
return;
}
const tracesSampleRate = parseSampleRate(
process.env.SENTRY_TRACES_SAMPLE_RATE,
app.isPackaged ? 0.2 : 1.0,
);
try {
Sentry.init({
dsn,
enabled: true,
release: getRelease(),
environment: getEnvironment(),
debug: parseBoolean(process.env.SENTRY_DEBUG, false),
sendDefaultPii: parseBoolean(process.env.SENTRY_SEND_DEFAULT_PII, false),
enableLogs: parseBoolean(process.env.SENTRY_ENABLE_LOGS, true),
tracesSampleRate,
integrations: [Sentry.startupTracingIntegration()],
});
isSentryInitialized = true;
Sentry.setTag("process.type", "main");
console.log("[Sentry] Initialized in Electron main process.");
} catch (error) {
console.error("[Sentry] Failed to initialize in Electron main process:", error);
}
}
export function captureMainSentryTestError(message?: string): string | null {
if (!isSentryInitialized) {
return null;
}
const error = new Error(message || "Sentry test error in main process");
return Sentry.captureException(error);
}

View file

@ -33,6 +33,8 @@ export function getUserConfig(): UserConfig {
export function setupEnv(fastApiPort: number, nextjsPort: number) {
const { app } = require('electron');
process.env.APP_VERSION = app.getVersion();
process.env.SENTRY_RELEASE = process.env.SENTRY_RELEASE || `presenton-electron@${process.env.APP_VERSION}`;
process.env.SENTRY_ENVIRONMENT = process.env.SENTRY_ENVIRONMENT || (app.isPackaged ? 'production' : 'development');
process.env.NEXT_PUBLIC_FAST_API = `${localhost}:${fastApiPort}`;
process.env.TEMP_DIRECTORY = tempDir;
process.env.NEXT_PUBLIC_USER_CONFIG_PATH = userConfigPath;

File diff suppressed because it is too large Load diff

View file

@ -53,6 +53,7 @@
"dependencies": {
"@llamaindex/liteparse": "^1.4.0",
"@puppeteer/browsers": "^1.9.1",
"@sentry/electron": "^7.10.0",
"@tailwindcss/cli": "^4.1.5",
"@types/uuid": "^10.0.0",
"dotenv": "^16.5.0",

View file

@ -11,12 +11,13 @@ from api.v1.webhook.router import API_V1_WEBHOOK_ROUTER
from api.v1.mock.router import API_V1_MOCK_ROUTER
from utils.get_env import (
get_app_data_directory_env,
get_sentry_dsn_env,
get_sentry_send_default_pii_env,
get_sentry_traces_sample_rate_env,
)
from utils.path_helpers import get_resource_path
FASTAPI_SENTRY_DSN = "https://a7831b44cb7096645e4b7569f53d070c@o4509882707410944.ingest.us.sentry.io/4511171447947264"
@ -41,7 +42,7 @@ def _get_sentry_send_default_pii() -> bool:
sentry_sdk.init(
dsn=get_sentry_dsn_env(),
dsn=FASTAPI_SENTRY_DSN,
send_default_pii=_get_sentry_send_default_pii(),
traces_sample_rate=_get_sentry_traces_sample_rate(),
)

View file

@ -1310,6 +1310,7 @@ dependencies = [
{ name = "pytest" },
{ name = "python-pptx" },
{ name = "redis" },
{ name = "sentry-sdk" },
{ name = "sqlmodel" },
]
@ -1336,6 +1337,7 @@ requires-dist = [
{ name = "pytest", specifier = ">=8.4.1" },
{ name = "python-pptx", specifier = ">=1.0.2" },
{ name = "redis", specifier = ">=6.2.0" },
{ name = "sentry-sdk", specifier = ">=2.34.1" },
{ name = "sqlmodel", specifier = ">=0.0.24" },
]

View file

@ -2,11 +2,17 @@
import React, { useEffect, useState } from "react";
import { Switch } from "@/components/ui/switch";
import { setTelemetryEnabled } from "@/utils/mixpanel";
import { getFastAPIUrl } from "@/utils/api";
import { Loader2 } from "lucide-react";
import { Button } from "@/components/ui/button";
import { notify } from "@/components/ui/sonner";
const PrivacySettings = () => {
const [trackingEnabled, setTrackingEnabled] = useState<boolean | null>(null);
const [saving, setSaving] = useState(false);
const [testingMainSentry, setTestingMainSentry] = useState(false);
const [testingNextjsSentry, setTestingNextjsSentry] = useState(false);
const [testingFastapiSentry, setTestingFastapiSentry] = useState(false);
useEffect(() => {
async function fetchStatus() {
@ -52,6 +58,73 @@ const PrivacySettings = () => {
}
};
const handleSentryMainTest = async () => {
if (!window.electron?.captureSentryMainTestError) {
notify.error(
"Sentry test unavailable",
"Electron preload API is missing. Restart the desktop app and try again.",
);
return;
}
setTestingMainSentry(true);
try {
const eventId = await window.electron.captureSentryMainTestError("test error");
if (eventId) {
notify.success(
"Sentry test sent",
`Main process test event submitted. Event ID: ${eventId}`,
);
} else {
notify.info(
"Sentry test attempted",
"No event ID returned. Check main-process Sentry initialization logs.",
);
}
} catch (error) {
const message =
error instanceof Error ? error.message : "Could not send Sentry test event.";
notify.error("Sentry test failed", message);
} finally {
setTestingMainSentry(false);
}
};
const handleSentryNextjsTest = async () => {
setTestingNextjsSentry(true);
try {
await fetch("/api/sentry-example-api", { cache: "no-store" });
notify.info(
"Next.js test triggered",
"If Sentry is configured, the Next.js API error event should appear shortly.",
);
} catch (error) {
const message =
error instanceof Error ? error.message : "Could not call Next.js Sentry test route.";
notify.error("Next.js test failed", message);
} finally {
setTestingNextjsSentry(false);
}
};
const handleSentryFastapiTest = async () => {
setTestingFastapiSentry(true);
try {
const baseUrl = getFastAPIUrl();
await fetch(`${baseUrl}/sentry-debug`, { cache: "no-store" });
notify.info(
"FastAPI test triggered",
"If Sentry is configured, the FastAPI error event should appear shortly.",
);
} catch (error) {
const message =
error instanceof Error ? error.message : "Could not call FastAPI Sentry test route.";
notify.error("FastAPI test failed", message);
} finally {
setTestingFastapiSentry(false);
}
};
if (trackingEnabled === null) {
return (
<div className="w-full bg-[#F9F8F8] p-7 rounded-[20px] flex items-center justify-center min-h-[200px]">
@ -99,6 +172,27 @@ const PrivacySettings = () => {
</div>
</div>
</div>
<div className="bg-[#F9F8F8] p-7 rounded-[20px]">
<h4 className="text-sm font-semibold text-[#191919] mb-1">
Sentry Integration Test
</h4>
<p className="text-xs text-[#6B7280] mb-6 leading-relaxed max-w-lg">
Trigger test failures from Electron main, Next.js, and FastAPI to verify
all Sentry pipelines are reporting events.
</p>
<div className="flex flex-wrap gap-3">
<Button onClick={handleSentryMainTest} disabled={testingMainSentry}>
{testingMainSentry ? "Sending Main Event..." : "Test Electron Main"}
</Button>
<Button onClick={handleSentryNextjsTest} disabled={testingNextjsSentry} variant="outline">
{testingNextjsSentry ? "Triggering Next.js..." : "Test Next.js"}
</Button>
<Button onClick={handleSentryFastapiTest} disabled={testingFastapiSentry} variant="outline">
{testingFastapiSentry ? "Triggering FastAPI..." : "Test FastAPI"}
</Button>
</div>
</div>
</div>
);
};

View file

@ -5,7 +5,7 @@
import * as Sentry from "@sentry/nextjs";
Sentry.init({
dsn: "https://0413392c73be9147cb3952a1d44d1e79@o4511154530222080.ingest.de.sentry.io/4511154543919184",
dsn: "https://f44638f4ce2c8ed04a087939e3a540ad@o4509882707410944.ingest.us.sentry.io/4511171444146176",
// Define how likely traces are sampled. Adjust this value in production, or use tracesSampler for greater control.
tracesSampleRate: 1,

View file

@ -6,7 +6,7 @@
import * as Sentry from "@sentry/nextjs";
Sentry.init({
dsn: "https://0413392c73be9147cb3952a1d44d1e79@o4511154530222080.ingest.de.sentry.io/4511154543919184",
dsn: "https://f44638f4ce2c8ed04a087939e3a540ad@o4509882707410944.ingest.us.sentry.io/4511171444146176",
// Define how likely traces are sampled. Adjust this value in production, or use tracesSampler for greater control.
tracesSampleRate: 1,

View file

@ -5,7 +5,7 @@
import * as Sentry from "@sentry/nextjs";
Sentry.init({
dsn: "https://0413392c73be9147cb3952a1d44d1e79@o4511154530222080.ingest.de.sentry.io/4511154543919184",
dsn: "https://f44638f4ce2c8ed04a087939e3a540ad@o4509882707410944.ingest.us.sentry.io/4511171444146176",
// Define how likely traces are sampled. Adjust this value in production, or use tracesSampler for greater control.
tracesSampleRate: 1,

File diff suppressed because one or more lines are too long

View file

@ -33,6 +33,8 @@ interface ElectronAPI {
hasRequiredKey: () => Promise<{ hasKey: boolean }>;
telemetryStatus: () => Promise<{ telemetryEnabled: boolean }>;
getTemplates: () => Promise<Array<{ templateName: string; templateID: string; files: string[]; settings: any }>>;
captureSentryRendererTestError: (message?: string) => Promise<string | null> | string | null;
captureSentryMainTestError: (message?: string) => Promise<string | null>;
}
interface Window {