Skip to content

Commit 8801eaf

Browse files
committed
chore: add crash dump via reporter
1 parent 9243b1f commit 8801eaf

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

ts/mains/main_node.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import {
88
app,
99
BrowserWindow,
10+
crashReporter,
1011
protocol as electronProtocol,
1112
ipcMain as ipc,
1213
ipcMain,
@@ -20,7 +21,7 @@ import {
2021
} from 'electron';
2122

2223
import crypto from 'crypto';
23-
import fs from 'fs';
24+
import fs, { appendFileSync } from 'fs';
2425
import os from 'os';
2526
import path, { join } from 'path';
2627
import { platform as osPlatform } from 'process';
@@ -152,6 +153,12 @@ if (!process.mas) {
152153
}
153154
}
154155

156+
crashReporter.start({
157+
submitURL: '', // leave empty if you don’t want to upload
158+
uploadToServer: false,
159+
compress: true,
160+
});
161+
155162
const windowFromUserConfig = userConfig.get('window');
156163
const windowFromEphemeral = ephemeralConfig.get('window');
157164
let windowConfig = windowFromEphemeral || windowFromUserConfig;
@@ -360,6 +367,17 @@ async function createWindow() {
360367
mainWindow.on('focus', setWindowFocus);
361368
mainWindow.on('blur', setWindowFocus);
362369
mainWindow.once('ready-to-show', setWindowFocus);
370+
function logCrash(type: string, details: any) {
371+
const crashLogPath = path.join(app.getPath('userData'), 'crash-log.txt');
372+
const logLine = `[${new Date().toISOString()}] ${type} crash: ${JSON.stringify(details)}\n`;
373+
appendFileSync(crashLogPath, logLine, 'utf8');
374+
}
375+
376+
mainWindow.webContents.on('render-process-gone', (_event, details) => {
377+
// details.reason can be: 'crashed', 'killed', 'oom', etc.
378+
logCrash('renderer', details);
379+
});
380+
363381
// This is a fallback in case we drop an event for some reason.
364382
global.setInterval(setWindowFocus, 5000);
365383

ts/node/global_errors.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import path from 'node:path';
2+
import { appendFileSync } from 'node:fs';
13
import { app, dialog, clipboard } from 'electron';
24
import os from 'node:os';
35
import { reallyJsonStringify } from '../util/reallyJsonStringify';
@@ -8,6 +10,12 @@ import { redactAll } from '../util/privacy';
810
const quitText = 'Quit';
911
const copyErrorAndQuitText = 'Copy error and quit';
1012

13+
function logCrash(type: string, details: any) {
14+
const crashLogPath = path.join(app.getPath('userData'), 'crash-log.txt');
15+
const logLine = `[${new Date().toISOString()}] ${type} crash: ${JSON.stringify(details)}\n`;
16+
appendFileSync(crashLogPath, logLine, 'utf8');
17+
}
18+
1119
function handleError(prefix: string, error: Error): void {
1220
const formattedError = Errors.toString(error);
1321
if (console._error) {
@@ -53,10 +61,14 @@ export const addHandler = (): void => {
5361
// Note: we could maybe add a handler for when the renderer process died here?
5462
// (but also ignore the valid death like on restart/quit)
5563
process.on('uncaughtException', (reason: unknown) => {
64+
logCrash('main', { reason: 'uncaughtException', error: reason });
65+
5666
handleError('Unhandled Error', _getError(reason));
5767
});
5868

5969
process.on('unhandledRejection', (reason: unknown) => {
70+
logCrash('main', { reason: 'unhandledRejection', error: reason });
71+
6072
handleError('Unhandled Promise Rejection', _getError(reason));
6173
});
6274
};

0 commit comments

Comments
 (0)