forked from sindresorhus/electron-unhandled
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (95 loc) · 2.72 KB
/
index.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
'use strict';
const electron = require('electron');
const cleanStack = require('clean-stack');
const ensureError = require('ensure-error');
const debounce = require('lodash.debounce');
const isDev = require('electron-is-dev');
const app = electron.app || electron.remote.app;
const dialog = electron.dialog || electron.remote.dialog;
const clipboard = electron.clipboard || electron.remote.clipboard;
const appName = 'name' in app ? app.name : app.getName();
// The dialog.showMessageBox method has been split into a sync and an async variant in Electron 6.0.0
const showMessageBox = dialog.showMessageBoxSync || dialog.showMessageBox;
let installed = false;
let options = {
logger: console.error,
showDialog: !isDev
};
const handleError = (title, error) => {
error = ensureError(error);
try {
options.logger(error);
} catch (loggerError) { // eslint-disable-line unicorn/catch-error-name
dialog.showErrorBox('The `logger` option function in electron-unhandled threw an error', ensureError(loggerError).stack);
return;
}
if (options.showDialog) {
const stack = cleanStack(error.stack);
if (app.isReady()) {
const buttons = [
'OK',
process.platform === 'darwin' ? 'Copy Error' : 'Copy error'
];
if (options.reportButton) {
buttons.push('Report…');
}
// Intentionally not using the `title` option as it's not shown on macOS
const buttonIndex = showMessageBox({
type: 'error',
buttons,
defaultId: 0,
noLink: true,
message: title,
detail: cleanStack(error.stack, {pretty: true})
});
if (buttonIndex === 1) {
clipboard.writeText(`${title}\n${stack}`);
}
if (buttonIndex === 2) {
options.reportButton(error);
}
} else {
dialog.showErrorBox(title, stack);
}
}
};
module.exports = inputOptions => {
if (installed) {
return;
}
installed = true;
options = {
...options,
...inputOptions
};
if (process.type === 'renderer') {
const errorHandler = debounce(error => {
handleError('Unhandled Error', error);
}, 200);
window.addEventListener('error', event => {
event.preventDefault();
errorHandler(event.error || event);
});
const rejectionHandler = debounce(reason => {
handleError('Unhandled Promise Rejection', reason);
}, 200);
window.addEventListener('unhandledrejection', event => {
event.preventDefault();
rejectionHandler(event.reason);
});
} else {
process.on('uncaughtException', error => {
handleError('Unhandled Error', error);
});
process.on('unhandledRejection', error => {
handleError('Unhandled Promise Rejection', error);
});
}
};
module.exports.logError = (error, options) => {
options = {
title: `${appName} encountered an error`,
...options
};
handleError(options.title, error);
};