Skip to content

Commit 78b9747

Browse files
committed
Replace isinstanceof Error with a better isError check
Fixes #237 (hopefully)
1 parent 2cf8531 commit 78b9747

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/raven.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ var _Raven = window.Raven,
2222
extra: {}
2323
},
2424
authQueryString,
25-
isRavenInstalled = false;
25+
isRavenInstalled = false,
26+
27+
objectPrototype = Object.prototype;
2628

2729
/*
2830
* The core Raven singleton
@@ -232,7 +234,7 @@ var Raven = {
232234
*/
233235
captureException: function(ex, options) {
234236
// If not an Error is passed through, recall as a message instead
235-
if (!(ex instanceof Error)) return Raven.captureMessage(ex, options);
237+
if (!isError(ex)) return Raven.captureMessage(ex, options);
236238

237239
// Store the raw exception object for potential debugging and introspection
238240
lastCapturedException = ex;
@@ -399,11 +401,24 @@ function isString(what) {
399401
return typeof what === 'string';
400402
}
401403

404+
function isObject(what) {
405+
return typeof what === 'object' && what !== null;
406+
}
407+
402408
function isEmptyObject(what) {
409+
if (!isObject(what)) return false;
403410
for (var k in what) return false;
404411
return true;
405412
}
406413

414+
// Sorta yanked from https://github.com/joyent/node/blob/aa3b4b4/lib/util.js#L560
415+
// with some tiny modifications
416+
function isError(what) {
417+
return isObject(what) &&
418+
objectPrototype.toString.call(what) === '[object Error]' ||
419+
what instanceof Error;
420+
}
421+
407422
/**
408423
* hasKey, a better form of hasOwnProperty
409424
* Example: hasKey(MainHostObject, property) === true/false
@@ -412,7 +427,7 @@ function isEmptyObject(what) {
412427
* @param {string} key to check
413428
*/
414429
function hasKey(object, key) {
415-
return Object.prototype.hasOwnProperty.call(object, key);
430+
return objectPrototype.hasOwnProperty.call(object, key);
416431
}
417432

418433
function each(obj, callback) {

test/raven.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,32 @@ describe('globals', function() {
185185
});
186186
});
187187

188+
describe('isObject', function() {
189+
it('should do as advertised', function() {
190+
assert.isTrue(isObject({}));
191+
assert.isTrue(isObject(new Error()))
192+
assert.isFalse(isObject(''));
193+
});
194+
});
195+
188196
describe('isEmptyObject', function() {
189197
it('should work as advertised', function() {
190198
assert.isTrue(isEmptyObject({}));
191199
assert.isFalse(isEmptyObject({foo: 1}));
192200
});
193201
});
194202

203+
describe('isError', function() {
204+
it('should work as advertised', function() {
205+
assert.isTrue(isError(new Error()));
206+
assert.isTrue(isError(new ReferenceError()));
207+
assert.isTrue(isError(new RavenConfigError()));
208+
assert.isFalse(isError({}));
209+
assert.isFalse(isError(''));
210+
assert.isFalse(isError(true));
211+
});
212+
});
213+
195214
describe('objectMerge', function() {
196215
it('should work as advertised', function() {
197216
assert.deepEqual(objectMerge({}, {}), {});

0 commit comments

Comments
 (0)