-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrorlogger.js
63 lines (53 loc) · 1.62 KB
/
errorlogger.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
/**
* JS Error logger
* Requires (optional) https://github.com/csnover/TraceKit
*
* @author igonkin-iv
* @since 11/14/12
*/
(function() {
function log(message, url) {
try {
if (window.XMLHttpRequest === undefined) { // IE 5.x-6.x:
window.XMLHttpRequest = function () {
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch(e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch(e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
throw new Error('No XHR.');
};
}
var request = new XMLHttpRequest();
request.open('POST', url);
request.setRequestHeader('Content-Type', 'application/json');
request.send(JSON.stringify(message));
}
catch (e) { console.error(e) }
}
function JSError(message, url, line) {
this.date = new Date();
this.message = message;
this.url = url;
this.line = line;
}
function handleError(message, url, line) {
var error;
if (arguments.length === 1) {
var stackInfo = arguments[0];
error = new JSError(stackInfo.message, stackInfo.stack[0].url, stackInfo.stack[0].line);
error.stack = stackInfo.stack;
} else {
error = new JSError(message, url, line);
}
log(error, '/jslog');
}
if (typeof TraceKit !== 'undefined') {
// Use TraceKit
TraceKit.report.subscribe(handleError);
} else {
// Fallback
if (typeof window.onerror !== 'undefined') {
window.onerror = handleError;
}
}
}).call(this);