Skip to content

Commit 62b2883

Browse files
committed
2 parents 3d85b36 + 68e86b9 commit 62b2883

File tree

6 files changed

+156
-71
lines changed

6 files changed

+156
-71
lines changed

example/browser/coderr.browser.js

+133-60
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
var coderr = (function (exports) {
22
'use strict';
33

4-
function toCollection(collectionName, anyObject) {
5-
var props = [];
4+
function toCollection(collectionName, anyObject, removeUndefined) {
5+
if (removeUndefined === void 0) { removeUndefined = false; }
6+
var props = {};
67
function addItem(name, value) {
78
if (Array.isArray(value)) {
89
value.forEach(function (item, index) {
910
addItem(name + "[" + index + "]", item);
1011
});
1112
}
13+
else if (typeof value === 'function') ;
1214
else if (typeof value === 'object') {
1315
for (var key in value) {
1416
if (!value.hasOwnProperty(key)) {
@@ -24,7 +26,20 @@ var coderr = (function (exports) {
2426
}
2527
}
2628
else {
27-
props.push({ name: name, value: value });
29+
var valueStr = void 0;
30+
if (typeof value === 'undefined') {
31+
if (removeUndefined) {
32+
return;
33+
}
34+
valueStr = 'undefined';
35+
}
36+
else if (value === null) {
37+
valueStr = 'null';
38+
}
39+
else {
40+
valueStr = value.toString();
41+
}
42+
props[name] = valueStr;
2843
}
2944
}
3045
addItem('', anyObject);
@@ -35,19 +50,25 @@ var coderr = (function (exports) {
3550
return false;
3651
}
3752
var result = false;
38-
var element = new Image();
39-
element.__defineGetter__('id', function () {
40-
result = true;
41-
});
42-
console.log(element);
53+
try {
54+
var element = new Image();
55+
element.__defineGetter__('id', function () {
56+
result = true;
57+
});
58+
console.log(element);
59+
}
60+
catch (_a) {
61+
// probably that the Image could not be created,
62+
// so ignore the error as we wont have dev tools then.
63+
}
4364
return result;
4465
}
4566
// source: https://stackoverflow.com/questions/3231459/create-unique-id-with-javascript
4667
function uniqueid() {
4768
// always start with a letter (for DOM friendlyness)
48-
var idstr = String.fromCharCode(Math.floor((Math.random() * 25) + 65));
69+
var idstr = String.fromCharCode(Math.floor(Math.random() * 25 + 65));
4970
do {
50-
var asciiCode = Math.floor((Math.random() * 42) + 48);
71+
var asciiCode = Math.floor(Math.random() * 42 + 48);
5172
if (asciiCode < 58 || asciiCode > 64) {
5273
// exclude all chars between : (58) and @ (64)
5374
idstr += String.fromCharCode(asciiCode);
@@ -82,25 +103,49 @@ var coderr = (function (exports) {
82103
return Configuration;
83104
}());
84105
var Reporter = /** @class */ (function () {
85-
function Reporter(configuration) {
106+
function Reporter(configuration, uploader) {
86107
this.configuration = configuration;
108+
this.uploader = uploader;
87109
if (this.configuration.environmentName === '' &&
88110
detectDeveloperTools()) {
89111
this.configuration.environmentName = 'Development';
90112
}
113+
Reporter.instance = this;
91114
}
92115
Reporter.prototype.reportErr = function (error, contextData) {
93116
var _this = this;
117+
if (contextData === void 0) { contextData = null; }
94118
var ctx = new VanillaContext(this, error);
95119
this.configuration.contextFactories.forEach(function (element) {
96120
if (!element.canHandle(_this, error)) {
97121
return;
98122
}
99123
ctx = element.createContext(_this, error);
100124
});
101-
if (contextData){
102-
var col = toCollection('ContextData', contextData);
103-
ctx.contextCollections.push(col);
125+
if (contextData) {
126+
if (Array.isArray(contextData)) {
127+
contextData.forEach(function (x, index) {
128+
if (!x.name || !x.properties) {
129+
if (index === 0) {
130+
x = toCollection('ContextData', x);
131+
}
132+
else {
133+
x = toCollection('ContextData' + (index + 1), x);
134+
}
135+
}
136+
ctx.contextCollections.push(x);
137+
});
138+
}
139+
else {
140+
var collection = void 0;
141+
if (!contextData.name || !contextData.properties) {
142+
collection = toCollection('ContextData', contextData);
143+
}
144+
else {
145+
collection = contextData;
146+
}
147+
ctx.contextCollections.push(collection);
148+
}
104149
}
105150
this.reportByContext(ctx);
106151
};
@@ -122,18 +167,33 @@ var coderr = (function (exports) {
122167
};
123168
Reporter.prototype.upload = function (report) {
124169
var url = this.configuration.serverUrl + "receiver/report/" + this.configuration.appKey + "/";
125-
var httpRequest = new XMLHttpRequest();
126-
httpRequest.open('POST', url, true);
127-
httpRequest.setRequestHeader('Content-type', 'application/json');
128-
httpRequest.setRequestHeader('X-Library', 'javascript');
129-
var data = JSON.stringify(report);
130-
httpRequest.send(data);
170+
var uploader = this.uploader || this.configuration.uploader;
171+
if (uploader) {
172+
uploader({
173+
appKey: this.configuration.appKey,
174+
report: report,
175+
url: this.configuration.serverUrl,
176+
});
177+
}
178+
else {
179+
var data = JSON.stringify(report);
180+
var httpRequest = new XMLHttpRequest();
181+
httpRequest.open('POST', url, true);
182+
httpRequest.setRequestHeader('Content-type', 'application/json');
183+
httpRequest.setRequestHeader('X-Library', 'javascript');
184+
httpRequest.send(data);
185+
}
131186
};
132187
Reporter.prototype.convertCollections = function (collections) {
133188
var dtos = [];
134189
collections.forEach(function (item) {
135190
var dict = {};
136-
item.properties.forEach(function (x) { return (dict[x.name] = x.value); });
191+
for (var key in item.properties) {
192+
if (item.properties.hasOwnProperty(key)) {
193+
var value = item.properties[key];
194+
dict[key] = value;
195+
}
196+
}
137197
var dto = {
138198
Name: item.name,
139199
Properties: dict,
@@ -1093,7 +1153,7 @@ var coderr = (function (exports) {
10931153
return DomContextFactory;
10941154
}());
10951155
function catchDomErrors(configuration) {
1096-
document.addEventListener('error', function (event) {
1156+
window.addEventListener('error', function (event) {
10971157
var domContext = new DomErrorContext(event.target, event.error, window.document, window);
10981158
Reporter.instance.reportByContext(domContext);
10991159
});
@@ -1117,40 +1177,53 @@ var coderr = (function (exports) {
11171177
if (!document) {
11181178
return [];
11191179
}
1120-
var doc = toCollection('document', {
1121-
baseURI: document.baseURI,
1122-
characterSet: document.characterSet,
1123-
charset: document.charset,
1124-
contentType: document.contentType,
1125-
cookie: document.cookie,
1126-
fullscreenEnabled: document.fullscreenEnabled,
1127-
compatMode: document.compatMode,
1128-
lastModified: document.lastModified,
1129-
location: document.location.href,
1130-
readyState: document.readyState,
1131-
referrer: document.referrer,
1132-
title: document.title,
1133-
});
1134-
1135-
if (!document.body){
1136-
console.log('no body', document);
1137-
}else{
1138-
doc.body = {
1139-
clientHeight: document.body.clientHeight,
1140-
clientLeft: document.body.clientLeft,
1141-
clientTop: document.body.clientTop,
1142-
clientWidth: document.body.clientWidth,
1143-
baseURI: document.body.baseURI,
1144-
draggable: document.body.draggable,
1145-
inputMode: document.body.inputMode,
1146-
offsetHeight: document.body.offsetHeight,
1147-
offsetLeft: document.body.offsetLeft,
1148-
offsetParent: document.body.offsetParent,
1149-
offsetTop: document.body.offsetTop,
1150-
offsetWidth: document.body.offsetWidth,
1151-
};
1180+
var doc = null;
1181+
if (document.body) {
1182+
doc = toCollection('document', {
1183+
baseURI: document.baseURI,
1184+
characterSet: document.characterSet,
1185+
charset: document.charset,
1186+
contentType: document.contentType,
1187+
cookie: document.cookie,
1188+
body: {
1189+
clientHeight: document.body.clientHeight,
1190+
clientLeft: document.body.clientLeft,
1191+
clientTop: document.body.clientTop,
1192+
clientWidth: document.body.clientWidth,
1193+
baseURI: document.body.baseURI,
1194+
draggable: document.body.draggable,
1195+
inputMode: document.body.inputMode,
1196+
offsetHeight: document.body.offsetHeight,
1197+
offsetLeft: document.body.offsetLeft,
1198+
offsetParent: document.body.offsetParent,
1199+
offsetTop: document.body.offsetTop,
1200+
offsetWidth: document.body.offsetWidth,
1201+
},
1202+
fullscreenEnabled: document.fullscreenEnabled,
1203+
compatMode: document.compatMode,
1204+
lastModified: document.lastModified,
1205+
location: document.location.href,
1206+
readyState: document.readyState,
1207+
referrer: document.referrer,
1208+
title: document.title,
1209+
});
1210+
}
1211+
else {
1212+
doc = toCollection('document', {
1213+
baseURI: document.baseURI,
1214+
characterSet: document.characterSet,
1215+
charset: document.charset,
1216+
contentType: document.contentType,
1217+
cookie: document.cookie,
1218+
fullscreenEnabled: document.fullscreenEnabled,
1219+
compatMode: document.compatMode,
1220+
lastModified: document.lastModified,
1221+
location: document.location.href,
1222+
readyState: document.readyState,
1223+
referrer: document.referrer,
1224+
title: document.title,
1225+
});
11521226
}
1153-
11541227
return [doc];
11551228
};
11561229
return DocumentCollectionProvider;
@@ -1179,24 +1252,24 @@ var coderr = (function (exports) {
11791252
browser: {
11801253
name: parser.getBrowser().name,
11811254
major: parser.getBrowser().major,
1182-
version: parser.getBrowser().version
1255+
version: parser.getBrowser().version,
11831256
},
11841257
OS: {
11851258
name: parser.getOS().name,
1186-
version: parser.getOS().version
1259+
version: parser.getOS().version,
11871260
},
11881261
CPU: {
11891262
architecture: parser.getCPU().architecture,
11901263
},
11911264
device: {
11921265
model: parser.getDevice().model,
11931266
type: parser.getDevice().type,
1194-
version: parser.getBrowser().version
1267+
version: parser.getBrowser().version,
11951268
},
11961269
engine: {
11971270
name: parser.getEngine().name,
1198-
version: parser.getEngine().version
1199-
}
1271+
version: parser.getEngine().version,
1272+
},
12001273
});
12011274
return [col, uaCollection];
12021275
};

example/browser/index.html

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22
<head>
33
<script src="coderr.browser.js"></script>
44
<script>
5-
var config = new coderr.Configuration("http://localhost:50473/", "f7aeacfdbe5447cdada6aa9ee21d24fb");
5+
var config = new coderr.Configuration("http://localhost:50473/", "8ffd506b153f4ca690daaf6abd5fdcdf");
66
var reporter = new coderr.Coderr(config);
77
reporter.catchDomErrors();
8+
9+
// automatic error detection
10+
one.true.error.one();
811

912
// Manual reporting
1013
try {
11-
var a = 10 / 0;
14+
one.true.error();
1215
}
1316
catch (err){
14-
reporter.report(err, {userName: "arne"});
17+
//reporter.report(err, {userName: "arne"});
1518
}
1619
</script>
1720
</head>

package-lock.json

+14-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"jsdom": "^15.1.1",
4545
"mocha": "^6.1.4",
4646
"prettier": "^1.18.2",
47-
"rollup": "^1.16.7",
47+
"rollup": "^1.26.0",
4848
"rollup-plugin-commonjs": "^9.1.8",
4949
"rollup-plugin-delete": "^1.0.0",
5050
"rollup-plugin-node-resolve": "^3.4.0",

src/dom.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class DomContextFactory implements ErrorReportContextFactory {
3838
}
3939

4040
export function catchDomErrors(configuration: Configuration) {
41-
document.addEventListener('error', (event: ErrorEvent) => {
41+
window.addEventListener('error', (event: ErrorEvent) => {
4242
const domContext = new DomErrorContext(
4343
event.target,
4444
event.error,

src/reporting.ts

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export class Reporter implements IReporter {
8383
) {
8484
this.configuration.environmentName = 'Development';
8585
}
86+
Reporter.instance = this;
8687
}
8788

8889
public reportErr(error: Error, contextData: any = null) {

0 commit comments

Comments
 (0)