-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patherror-handler.js
96 lines (77 loc) · 2.55 KB
/
error-handler.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
(function () {
'use strict';
var url = (document.currentScript && document.currentScript.dataset && document.currentScript.dataset.reportUrl) ||
((window.BASE_URL || '') + '/rest/V1/fredden/javascript-error-reporting');
var sendDelay = 0;
var sendQueue = [];
var sendTimer;
var sendError = function () {
var xhr;
window.clearTimeout(sendTimer);
if (!sendQueue.length) {
return;
}
xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify(sendQueue.shift()));
sendDelay += 10;
sendTimer = window.setTimeout(sendError, sendDelay);
};
// Override console.error method so these errors are also logged
var originalConsole = {
error: console.error
};
function logToQueue(type, args) {
if (sendQueue.length > 100) {
// That's a lot of errors! Let's not overwhelm the server with more.
return;
}
if (!sendQueue.length) {
sendTimer = window.setTimeout(sendError, sendDelay);
}
sendQueue.push({
browser: {
height: window.innerHeight,
url: window.location.href,
width: window.innerWidth,
},
event: {
type: type,
message: Array.from(args).join(' '),
timer: (performance.now() / 1000).toFixed(2),
},
});
}
console.error = function() {
logToQueue('error', arguments);
originalConsole.error.apply(console, arguments);
};
window.addEventListener('error', function (event) {
if (!event) {
return;
}
if (sendQueue.length > 100) {
// That's a lot of errors! Let's not overwhelm the server with more.
return;
}
if (!sendQueue.length) {
sendTimer = window.setTimeout(sendError, sendDelay);
}
sendQueue.push({
browser: {
height: window.innerHeight,
url: window.location.href,
width: window.innerWidth,
},
event: {
colno: event.colno,
filename: event.filename,
lineno: event.lineno,
message: event.message,
stack: event.error && event.error.stack,
timer: (performance.now() / 1000).toFixed(2),
},
});
});
}());