Skip to content

Commit 8b6ab9e

Browse files
committed
Merge pull request #9 from wavesoftware/bugfix/7-proper-inheritance-and-stacktrace
Fix #7: Proper handling for IE (tested on ie11), Edge, Firefox, Chrome and NodeJS
2 parents c5279b7 + 19d3163 commit 8b6ab9e

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

dist/browser/index.html

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
@import url(https://fonts.googleapis.com/css?family=PT+Sans|Source+Code+Pro);
1212
body {
1313
font-family: 'PT Sans', sans-serif;
14-
font-size: 1.3em;
14+
font-size: 1em;
1515
padding: 1em;
1616
}
1717
pre, code {
@@ -65,7 +65,7 @@ <h3>Code:</h3>
6565
log.error(e.stack);
6666
if (e instanceof EidRuntimeException) {
6767
// or better save your exceptions!
68-
logGateway.put(e, {eid: e.eid});
68+
logGateway.put(e, inspectable({eid: e.eid}));
6969
}
7070
}
7171
</code></pre>
@@ -96,9 +96,21 @@ <h3>Console:</h3>
9696
logentry('ERROR', message);
9797
}
9898
};
99+
var inspectable = function(obj) {
100+
obj.toString = function() {
101+
var parts = [];
102+
for (var k in obj) {
103+
if (obj.hasOwnProperty(k) && k !== 'toString') {
104+
parts.push(k.toString() + ': ' + obj[k].toString());
105+
}
106+
}
107+
return '{ ' + parts.join(', ') + ' }';
108+
};
109+
return obj;
110+
};
99111
var logGateway = {
100112
put: function(ex, extra) {
101-
logentry('INFO', 'FakeLogz.io PUT an exception: `' + ex.toString() + '` with extra data of: `' + JSON.stringify(extra) + '`');
113+
logentry('INFO', 'FakeLogz.io PUT exception: `' + ex.toString() + '`, payload: `' + extra.toString() + '`');
102114
}
103115
};
104116
var code = document.getElementById('code').textContent;

gulp/tests.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ module.exports = {
4444
var src = prependToAll('..', config.sources);
4545
var stream = gulp.src(testSrc, { read: false })
4646
.pipe(cover.instrument({
47-
pattern: src,
48-
debugDirectory: 'debug'
47+
pattern: src
4948
}))
5049
.pipe(mocha())
5150
.pipe(cover.gather());

lib/eid/exceptions.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,27 @@
3737
modern: function(target, constructor) {
3838
Error.captureStackTrace(target, constructor);
3939
},
40-
legancy: function(target, constructor, exception) {
41-
target.stack = (new exception()).stack;
40+
fixLegancyStackTrace: function(stack, stringified) {
41+
var st = stack;
42+
if (typeof(stack) !== 'string') {
43+
// On IE the stack field is empty so setting to toString() :-/
44+
st = stringified;
45+
}
46+
// Hands down, if all fails, just replace Error with nice Eid string
47+
if (st.indexOf(stringified) < 0) {
48+
st = st.replace(/^(?:Error\n)?/, stringified + "\n").trim();
49+
}
50+
return st;
51+
},
52+
legancy: function(target, constructor, exceptionConstructor) {
53+
// Hacks for IE an Edge
54+
var stringified = target.toString();
55+
// try to fetch stacktrace from JS standard Error
56+
var st = (new exceptionConstructor()).stack;
57+
target.stack = CorrectStackTrace.fixLegancyStackTrace(st, stringified);
4258
}
4359
};
60+
CorrectStackTrace.fixLegancyStackTrace('Error\n');
4461
CorrectStackTrace.legancy({}, null, Object);
4562

4663
var correctStackTrace = function(target, constructor) {
@@ -73,13 +90,14 @@
7390
var tmp = Error.apply(this, [message]);
7491
tmp.name = this.name = 'EidRuntimeException';
7592
this.message = tmp.message;
93+
this.eid = eid;
7694
correctStackTrace(this, EidRuntimeException);
7795

7896
return this;
7997
}
80-
var IntermediateInheritor = function() {};
81-
IntermediateInheritor.prototype = Error.prototype;
82-
EidRuntimeException.prototype = new IntermediateInheritor();
98+
EidRuntimeException.prototype = Object.create(Error.prototype, {
99+
constructor: { value: EidRuntimeException }
100+
});
83101

84102
/**
85103
* <strong>This class shouldn't be used in any public API or library.</strong> It is designed to be used for in-house development

0 commit comments

Comments
 (0)