Skip to content

Commit 6746dfd

Browse files
shcallawaykamilogorek
authored andcommitted
feat: Treat ErrorEvents like Errors
1 parent 9c2cfab commit 6746dfd

File tree

4 files changed

+56
-13
lines changed

4 files changed

+56
-13
lines changed

src/raven.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var utils = require('./utils');
88
var isError = utils.isError;
99
var isObject = utils.isObject;
1010
var isObject = utils.isObject;
11-
var isError = utils.isError;
11+
var isErrorEvent = utils.isErrorEvent;
1212
var isUndefined = utils.isUndefined;
1313
var isFunction = utils.isFunction;
1414
var isString = utils.isString;
@@ -393,8 +393,12 @@ Raven.prototype = {
393393
* @return {Raven}
394394
*/
395395
captureException: function(ex, options) {
396-
// If not an Error is passed through, recall as a message instead
397-
if (!isError(ex)) {
396+
// Cases for sending ex as a message, rather than an exception
397+
var isNotError = !isError(ex);
398+
var isNotErrorEvent = !isErrorEvent(ex);
399+
var isErrorEventWithoutError = isErrorEvent(ex) && !ex.error;
400+
401+
if ((isNotError && isNotErrorEvent) || isErrorEventWithoutError) {
398402
return this.captureMessage(
399403
ex,
400404
objectMerge(
@@ -407,6 +411,9 @@ Raven.prototype = {
407411
);
408412
}
409413

414+
// Get actual Error from ErrorEvent
415+
if (isErrorEvent(ex)) ex = ex.error;
416+
410417
// Store the raw exception object for potential debugging and introspection
411418
this._lastCapturedException = ex;
412419

src/utils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ function isError(value) {
2222
}
2323
}
2424

25+
function isErrorEvent(value) {
26+
return {}.toString.call(value) === '[object ErrorEvent]';
27+
}
28+
2529
function isUndefined(what) {
2630
return what === void 0;
2731
}
@@ -348,6 +352,7 @@ function fill(obj, name, replacement, track) {
348352
module.exports = {
349353
isObject: isObject,
350354
isError: isError,
355+
isErrorEvent: isErrorEvent,
351356
isUndefined: isUndefined,
352357
isFunction: isFunction,
353358
isString: isString,

test/raven.test.js

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,7 +1739,7 @@ describe('globals', function() {
17391739
this.sinon.stub(Raven, '_makeRequest');
17401740
var stackInfo = {
17411741
name: 'Error',
1742-
message: 'crap',
1742+
message: 'pickleRick',
17431743
url: 'http://example.com',
17441744
lineno: 10,
17451745
stack: [
@@ -1774,7 +1774,7 @@ describe('globals', function() {
17741774
},
17751775
exception: {
17761776
type: 'Error',
1777-
value: 'crap'
1777+
value: 'pickleRick'
17781778
},
17791779
stacktrace: {
17801780
frames: [{
@@ -1798,7 +1798,7 @@ describe('globals', function() {
17981798
}]
17991799
},
18001800
culprit: 'http://example.com',
1801-
message: 'Error: crap',
1801+
message: 'Error: pickleRick',
18021802
foo: 'bar'
18031803
}]);
18041804
*/
@@ -2347,7 +2347,7 @@ describe('Raven (public API)', function() {
23472347
});
23482348

23492349
it('should capture the exception with options', function() {
2350-
var error = new Error('crap');
2350+
var error = new Error('pickleRick');
23512351
var broken = function() {
23522352
throw error;
23532353
};
@@ -2365,7 +2365,7 @@ describe('Raven (public API)', function() {
23652365
});
23662366

23672367
it('should capture the exception without options', function() {
2368-
var error = new Error('crap');
2368+
var error = new Error('pickleRick');
23692369
var broken = function() {
23702370
throw error;
23712371
};
@@ -2770,24 +2770,47 @@ describe('Raven (public API)', function() {
27702770
});
27712771

27722772
describe('.captureException', function() {
2773+
it('should treat ErrorEvents similarly to Errors', function() {
2774+
var error = new ErrorEvent('pickleRick', {error: new Error('pickleRick')});
2775+
this.sinon.stub(Raven, 'isSetup').returns(true);
2776+
this.sinon.stub(Raven, '_handleStackInfo');
2777+
Raven.captureException(error, {foo: 'bar'});
2778+
assert.isTrue(Raven._handleStackInfo.calledOnce);
2779+
});
2780+
2781+
it('should send ErrorEvents without Errors as messages', function() {
2782+
var error = new ErrorEvent('pickleRick');
2783+
this.sinon.stub(Raven, 'isSetup').returns(true);
2784+
this.sinon.stub(Raven, 'captureMessage');
2785+
Raven.captureException(error, {foo: 'bar'});
2786+
assert.isTrue(Raven.captureMessage.calledOnce);
2787+
});
2788+
2789+
it('should send non-Errors as messages', function() {
2790+
this.sinon.stub(Raven, 'isSetup').returns(true);
2791+
this.sinon.stub(Raven, 'captureMessage');
2792+
Raven.captureException({}, {foo: 'bar'});
2793+
assert.isTrue(Raven.captureMessage.calledOnce);
2794+
});
2795+
27732796
it('should call handleStackInfo', function() {
2774-
var error = new Error('crap');
2797+
var error = new Error('pickleRick');
27752798
this.sinon.stub(Raven, 'isSetup').returns(true);
27762799
this.sinon.stub(Raven, '_handleStackInfo');
27772800
Raven.captureException(error, {foo: 'bar'});
27782801
assert.isTrue(Raven._handleStackInfo.calledOnce);
27792802
});
27802803

27812804
it('should store the last exception', function() {
2782-
var error = new Error('crap');
2805+
var error = new Error('pickleRick');
27832806
this.sinon.stub(Raven, 'isSetup').returns(true);
27842807
this.sinon.stub(Raven, '_handleStackInfo');
27852808
Raven.captureException(error);
27862809
assert.equal(Raven.lastException(), error);
27872810
});
27882811

27892812
it("shouldn't reraise the if error is the same error", function() {
2790-
var error = new Error('crap');
2813+
var error = new Error('pickleRick');
27912814
this.sinon.stub(Raven, 'isSetup').returns(true);
27922815
this.sinon.stub(Raven, '_handleStackInfo').throws(error);
27932816
// this would raise if the errors didn't match
@@ -2796,11 +2819,11 @@ describe('Raven (public API)', function() {
27962819
});
27972820

27982821
it('should reraise a different error', function() {
2799-
var error = new Error('crap1');
2822+
var error = new Error('pickleRick1');
28002823
this.sinon.stub(Raven, 'isSetup').returns(true);
28012824
this.sinon.stub(Raven, '_handleStackInfo').throws(error);
28022825
assert.throws(function() {
2803-
Raven.captureException(new Error('crap2'));
2826+
Raven.captureException(new Error('pickleRick2'));
28042827
}, error);
28052828
});
28062829

test/utils.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var isString = utils.isString;
1111
var isObject = utils.isObject;
1212
var isEmptyObject = utils.isEmptyObject;
1313
var isError = utils.isError;
14+
var isErrorEvent = utils.isErrorEvent;
1415
var wrappedCallback = utils.wrappedCallback;
1516
var joinRegExp = utils.joinRegExp;
1617
var objectMerge = utils.objectMerge;
@@ -65,6 +66,13 @@ describe('utils', function() {
6566
});
6667
});
6768

69+
describe('isErrorEvent', function() {
70+
it('should work as advertised', function() {
71+
assert.isFalse(isErrorEvent(new Error()));
72+
assert.isTrue(isErrorEvent(new ErrorEvent('')));
73+
});
74+
});
75+
6876
describe('isError', function() {
6977
function testErrorFromDifferentContext(createError) {
7078
var iframe = document.createElement('iframe');

0 commit comments

Comments
 (0)