catchErrors.js
2.35 KB
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
'use strict';
/**
* Some ideas from sindresorhus/electron-unhandled
*/
var electronApi = require('./electronApi');
var queryString = require('querystring');
var isAttached = false;
module.exports = function catchErrors(options) {
if (isAttached) return { stop: stop };
isAttached = true;
if (process.type === 'renderer') {
window.addEventListener('error', onRendererError);
window.addEventListener('unhandledrejection', onRendererRejection);
} else {
process.on('uncaughtException', onError);
process.on('unhandledRejection', onRejection);
}
return { stop: stop };
function onError(e) {
try {
if (typeof options.onError === 'function') {
var versions = electronApi.getVersions();
if (options.onError(e, versions, createIssue) === false) {
return;
}
}
options.log(e);
if (options.showDialog && e.name.indexOf('UnhandledRejection') < 0) {
var type = process.type || 'main';
electronApi.showErrorBox(
'A JavaScript error occurred in the ' + type + ' process',
e.stack
);
}
} catch (logError) {
// eslint-disable-next-line no-console
console.error(e);
}
}
function onRejection(reason) {
if (reason instanceof Error) {
try {
Object.defineProperty(reason, 'name', {
value: 'UnhandledRejection ' + reason.name,
});
} catch (e) {
// Can't redefine error name, but who cares?
}
onError(reason);
return;
}
var error = new Error(JSON.stringify(reason));
error.name = 'UnhandledRejection';
onError(error);
}
function onRendererError(event) {
event.preventDefault();
onError(event.error);
}
function onRendererRejection(event) {
event.preventDefault();
onRejection(event.reason);
}
function stop() {
isAttached = false;
if (process.type === 'renderer') {
window.removeEventListener('error', onRendererError);
window.removeEventListener('unhandledrejection', onRendererRejection);
} else {
process.removeListener('uncaughtException', onError);
process.removeListener('unhandledRejection', onRejection);
}
}
function createIssue(pageUrl, queryParams) {
var issueUrl = pageUrl + '?' + queryString.stringify(queryParams);
electronApi.openUrl(issueUrl, options.log);
}
};