err-reporter.js
1.39 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
var cookies = require('../cookie');
var reporter = require('./reporter');
var errorKey = '_errLog';
var errReporter = {
writeError: function(msg, source, lineno, colno, error) {
var errorList = JSON.parse(cookies(errorKey) || '[]');
errorList.push({
tp: 'err',
msg: msg,
sc: source,
ln: lineno,
cn: colno,
pt: encodeURIComponent(location.href),
u: reporter.getUid(),
ud: reporter.getUdid(),
rid: reporter.getReqId(),
st: JSON.stringify(error && error.stack),
r: reporter.getRoute()
});
cookies(errorKey, JSON.stringify(errorList));
if (errorList.length >= 5) {
this.reportError();
}
},
clearError: function() {
cookies(errorKey, '[]');
},
reportError: function() {
var self = this;
var errorList = JSON.parse(cookies(errorKey) || '[]');
var errStr = reporter.stringify(errorList);
reporter.report(errStr, function() {
self.clearError();
});
},
init: function() {
var self = this;
window.onerror = function(msg, source, lineno, colno, error) {
self.writeError(msg, source, lineno, colno, error);
};
// 上报未上报的事件
this.reportError();
}
};
module.exports = errReporter;