-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
onuncaughtexception.ts
72 lines (65 loc) · 2.37 KB
/
onuncaughtexception.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { getCurrentHub } from '@sentry/core';
import { NodeClient } from '@sentry/node';
import { Event, Integration, Severity } from '@sentry/types';
import { isError } from '@sentry/utils';
import { dialog } from 'electron';
/** Capture unhandled erros. */
export class OnUncaughtException implements Integration {
/**
* @inheritDoc
*/
public static id: string = 'OnUncaughtException';
/**
* @inheritDoc
*/
public name: string = OnUncaughtException.id;
/**
* @inheritDoc
*/
public constructor(
private readonly _options: {
/** Fatal Error callback */
onFatalError?(firstError: Error, secondError?: Error): void;
} = {},
) {}
/**
* @inheritDoc
*/
public setupOnce(): void {
global.process.on('uncaughtException', (error: Error) => {
const self = getCurrentHub().getIntegration(OnUncaughtException);
if (self) {
getCurrentHub().withScope(async scope => {
scope.addEventProcessor(async (event: Event) => ({
...event,
level: Severity.Fatal,
}));
let theError = error;
if (!isError(error) && error.stack) {
theError = new Error();
theError.message = error.message;
theError.stack = error.stack;
theError.name = error.name;
}
const nodeClient = getCurrentHub().getClient() as NodeClient;
nodeClient.captureException(theError, { originalException: error }, getCurrentHub().getScope());
await nodeClient.flush(nodeClient.getOptions().shutdownTimeout || 2000);
if (this._options.onFatalError) {
this._options.onFatalError(error);
} else if (global.process.listenerCount('uncaughtException') <= 2) {
// In addition to this handler there is always one in Electron
// The dialog is only shown if there are no other handlers
// eslint-disable-next-line no-console
console.error('Uncaught Exception:');
// eslint-disable-next-line no-console
console.error(error);
const ref = error.stack;
const stack = ref !== undefined ? ref : `${error.name}: ${error.message}`;
const message = `Uncaught Exception:\n${stack}`;
dialog.showErrorBox('A JavaScript error occurred in the main process', message);
}
});
}
});
}
}