Skip to content

Commit f54870b

Browse files
Thomas Graingerbenvinegar
authored andcommitted
detect DOMExceptions accross contexts as errors in isError
1 parent 249341b commit f54870b

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

src/utils.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ function isObject(what) {
44
return typeof what === 'object' && what !== null;
55
}
66

7-
// Sorta yanked from https://github.com/joyent/node/blob/aa3b4b4/lib/util.js#L560
7+
// Yanked from https://git.io/vS8DV re-used under CC0
88
// with some tiny modifications
9-
function isError(what) {
10-
var toString = {}.toString.call(what);
11-
return isObject(what) &&
12-
toString === '[object Error]' ||
13-
toString === '[object Exception]' || // Firefox NS_ERROR_FAILURE Exceptions
14-
what instanceof Error;
9+
function isError(value) {
10+
switch ({}.toString.call(value)) {
11+
case '[object Error]': return true;
12+
case '[object Exception]': return true;
13+
case '[object DOMException]': return true;
14+
default: return value instanceof Error;
15+
}
1516
}
1617

1718
module.exports = {

test/utils.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,39 @@ describe('utils', function () {
6666
});
6767

6868
describe('isError', function() {
69+
function testErrorFromDifferentContext(createError) {
70+
var iframe = document.createElement('iframe');
71+
document.body.appendChild(iframe);
72+
try {
73+
return createError(iframe.contentWindow);
74+
} finally {
75+
iframe.parentElement.removeChild(iframe);
76+
}
77+
}
78+
79+
function fromContext(win) {
80+
return new win.Error();
81+
}
82+
83+
function domException(win) {
84+
try {
85+
win.document.querySelectorAll('');
86+
} catch(e) {
87+
return e;
88+
}
89+
}
90+
6991
it('should work as advertised', function() {
7092
assert.isTrue(isError(new Error()));
7193
assert.isTrue(isError(new ReferenceError()));
7294
assert.isTrue(isError(new RavenConfigError()));
95+
assert.isTrue(isError(testErrorFromDifferentContext(fromContext)));
96+
assert.isTrue(isError(testErrorFromDifferentContext(domException)));
7397
assert.isFalse(isError({}));
98+
assert.isFalse(isError({
99+
message: 'A fake error',
100+
stack: 'no stack here'
101+
}));
74102
assert.isFalse(isError(''));
75103
assert.isFalse(isError(true));
76104
});

0 commit comments

Comments
 (0)