-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathlogerr.js
163 lines (139 loc) · 4.77 KB
/
logerr.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* logerr
*
* @category logerr
* @author Vaibhav Mehta <[email protected]>
* @copyright Copyright (c) 2016 Vaibhav Mehta <https://github.com/i-break-codes>
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 1.2 Stable
*/
var Logerr = function() {
'use strict';
var setConfig;
function init(userConfig) {
if(!userConfig) userConfig = {};
// Default configuration
var config = {
detailedErrors: true,
remoteLogging: false,
remoteSettings: {
url: null,
additionalParams: null,
successCallback: null,
errorCallback: null
}
};
// Override with user config
setConfig = Object.assign(config, userConfig);
//Remove current listener
window.removeEventListener('error', _listener);
// Listen to errors
window.addEventListener('error', _listener);
}
// NOTE: Private
function _listener(e) {
if(setConfig.detailedErrors) {
_detailedErrors(e);
}
if(setConfig.remoteLogging) {
_remoteLogging(e, setConfig.remoteSettings);
}
}
function _detailedErrors(e) {
var i = _errorData(e);
var helpPath = encodeURI("https://stackoverflow.com/search?q=" + i.error.split(' ').join('+'));
var str = [
"%cType: %c" + i.type,
"%cError: %c" + i.error,
"%cStackTrace: %c" + i.stackTrace,
"%cFile Name: %c" + i.filename,
"%cPath: %c" + i.path,
"%cLine: %c" + i.line,
"%cColumn: %c" + i.column,
"%cDate: %c" + i.datetime,
"%cDebug : %c" + i.path + ':' + i.line,
"%cGet Help: " + "%c" + helpPath
].join("\n");
if(window.chrome) {
console.log(str, "font-weight: bold;", "color: #e74c3c;", "font-weight: bold;", "font-weight: normal; color: #e74c3c;","font-weight: bold;", "font-weight: normal; color: #e74c3c;", "font-weight: bold;", "font-weight: normal;", "font-weight: bold;", "font-weight: normal;", "font-weight: bold;", "font-weight: normal;", "font-weight: bold;", "font-weight: normal;", "font-weight: bold;", "font-weight: normal;", "font-weight: bold;", "font-weight: normal;", "font-weight: bold;", "font-weight: normal; color: #3498db;");
} else {
console.log(str.replace(/%c/gi, ''));
}
}
function _remoteLogging(e, remoteSettings) {
if(!remoteSettings.url) {
throw new Error('Provide remote URL to log errors remotely');
} else if(remoteSettings.additionalParams && typeof remoteSettings.additionalParams !== 'object') {
throw new Error('Invalid data type, additionalParams should be a valid object');
}
var http = new XMLHttpRequest();
var url = remoteSettings.url;
var data = _errorData(e);
var setData = Object.assign(data, remoteSettings.additionalParams);
var params = _serializeData(setData);
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send(params);
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
if (http.readyState == XMLHttpRequest.DONE) {
if(remoteSettings.successCallback) {
remoteSettings.successCallback();
}
} else {
if(remoteSettings.errorCallback) {
remoteSettings.errorCallback();
} else {
throw new Error('Remote error logging failed!');
}
}
}
};
}
function _serializeData(params) {
return Object.keys(params).map(function(k) {
return encodeURIComponent(k) + "=" + encodeURIComponent(params[k]);
}).join('&');
}
function _errorData(e) {
var filename = e.filename.lastIndexOf('/');
var datetime = new Date().toString();
/**
* userAgent only for POST request purposes, not required in pretty print
*/
return {
type: e.type,
path: e.filename,
filename: e.filename.substring(++filename),
line: e.lineno,
column: e.colno,
error: e.message,
stackTrace: ((e.error) ? e.error.stack.toString().replace(/(\r\n|\n|\r)/gm,"") : ""),
datetime: datetime,
userAgent: navigator.userAgent || window.navigator.userAgent
};
}
//Polyfill for Object.assign
if (typeof Object.assign != 'function') {
Object.assign = function(target) {
if (target === null) {
throw new TypeError('Cannot convert undefined or null to object');
}
target = Object(target);
for (var index = 1; index < arguments.length; index++) {
var source = arguments[index];
if (source !== null) {
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
}
return target;
};
}
return {
init: init
};
}();