-
Notifications
You must be signed in to change notification settings - Fork 0
/
errplane_exceptions.js
68 lines (62 loc) · 2.25 KB
/
errplane_exceptions.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
function ErrplaneExceptions(options) {
this.apiKey = options.apiKey;
this.appKey = options.appKey;
this.envKey = options.envKey;
this.baseUrl = options.baseUrl;
this.customData = options.customData || {}; // will be included with every exception logged. good place to put user ids.
this.errplaneApi = new ErrplaneApi(options);
this.catchOnError();
}
/* Initializes Errplane to catch all exceptions from window.onerror
*/
ErrplaneExceptions.prototype.catchOnError = function() {
var _this = this;
window.onerror = function(message, file, line) {
var spaceIndex = message.indexOf(" ")
var colonIndex = message.indexOf(":")
var exceptionClass = message;
var exceptionMessage = message;
if (colonIndex > spaceIndex) {
exceptionClass = message.slice(spaceIndex + 1, colonIndex);
} else if (colonIndex > 0) {
exceptionClass = message.slice(0, colonIndex);
}
if (colonIndex > 0) {
exceptionMessage = message.slice(colonIndex + 2, message.length);
}
var backtrace = ["@" + file + ":" + line];
var timeInSeconds = Math.floor((new Date() - 0) / 1000);
var customData = _this.customData;
customData.url = window.location.href;
var exceptionData = {
message: message,
exception_class: exceptionClass,
backtrace: backtrace,
time: timeInSeconds,
language: "JavaScript",
custom_data: customData
};
_this.errplaneApi.reportException(exceptionData);
}
}
/* Logs an exception to the Errplane api.
*
* @params exception A javascript exception object
* @params options An object that can include a hash which identifies an exception and
overrides default Errplane exception groupings. The rest is sent through
as custom data.
*/
ErrplaneExceptions.prototype.logToErrplane = function(exception, options) {
var customData = $.extend({}, this.customData, options);
customData.url = window.location.href;
var timeInSeconds = Math.floor((new Date() - 0) / 1000);
var exceptionData = {
message: exception.message,
exception_class: exception.name,
backtrace: exception.stack.split("\n"),
time: timeInSeconds,
language: "JavaScript",
custom_data: customData
};
this.errplaneApi.reportException(exceptionData);
}