Skip to content

Commit b558746

Browse files
committed
Fail gracefully when Raven isnt configured
1 parent 4c5da1b commit b558746

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/raven.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ var Raven = {
231231
* @return {Raven}
232232
*/
233233
captureException: function(ex, options) {
234+
if (!isSetup()) {
235+
return Raven;
236+
}
237+
234238
// If not an Error is passed through, recall as a message instead
235239
if (!isError(ex)) return Raven.captureMessage(ex, options);
236240

@@ -262,6 +266,10 @@ var Raven = {
262266
* @return {Raven}
263267
*/
264268
captureMessage: function(msg, options) {
269+
if (!isSetup()) {
270+
return Raven;
271+
}
272+
265273
// config() automagically converts ignoreErrors from a list to a RegExp so we need to test for an
266274
// early call; we'll error on the side of logging anything called before configuration since it's
267275
// probably something you should see:

test/raven.test.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1680,17 +1680,21 @@ describe('Raven (public API)', function() {
16801680

16811681
describe('.captureMessage', function() {
16821682
it('should work as advertised', function() {
1683+
this.sinon.stub(window, 'isSetup').returns(true);
16831684
this.sinon.stub(window, 'send');
16841685
Raven.captureMessage('lol', {foo: 'bar'});
1686+
assert.isTrue(window.send.called);
16851687
assert.deepEqual(window.send.lastCall.args, [{
16861688
message: 'lol',
16871689
foo: 'bar'
16881690
}]);
16891691
});
16901692

16911693
it('should coerce message to a string', function() {
1694+
this.sinon.stub(window, 'isSetup').returns(true);
16921695
this.sinon.stub(window, 'send');
16931696
Raven.captureMessage({});
1697+
assert.isTrue(window.send.called);
16941698
assert.deepEqual(window.send.lastCall.args, [{
16951699
message: '[object Object]'
16961700
}]);
@@ -1716,6 +1720,7 @@ describe('Raven (public API)', function() {
17161720
});
17171721

17181722
it('should respect `ignoreErrors`', function() {
1723+
this.sinon.stub(window, 'isSetup').returns(true);
17191724
this.sinon.stub(window, 'send');
17201725

17211726
globalOptions.ignoreErrors = joinRegExp(['e1', 'e2']);
@@ -1726,25 +1731,36 @@ describe('Raven (public API)', function() {
17261731
Raven.captureMessage('Non-ignored error');
17271732
assert.isTrue(window.send.calledOnce);
17281733
});
1734+
1735+
it('should not throw an error if not configured', function() {
1736+
this.sinon.stub(Raven, 'isSetup').returns(false);
1737+
this.sinon.stub(window, 'send')
1738+
Raven.captureMessage('foo');
1739+
assert.isFalse(window.send.called);
1740+
});
1741+
17291742
});
17301743

17311744
describe('.captureException', function() {
17321745
it('should call handleStackInfo', function() {
17331746
var error = new Error('crap');
1747+
this.sinon.stub(window, 'isSetup').returns(true);
17341748
this.sinon.stub(window, 'handleStackInfo');
17351749
Raven.captureException(error, {foo: 'bar'});
17361750
assert.isTrue(window.handleStackInfo.calledOnce);
17371751
});
17381752

17391753
it('should store the last exception', function() {
17401754
var error = new Error('crap');
1755+
this.sinon.stub(window, 'isSetup').returns(true);
17411756
this.sinon.stub(window, 'handleStackInfo');
17421757
Raven.captureException(error);
17431758
assert.equal(Raven.lastException(), error);
17441759
});
17451760

1746-
it('shouldn\'t reraise the if the error is the same error', function() {
1761+
it('shouldn\'t reraise the if error is the same error', function() {
17471762
var error = new Error('crap');
1763+
this.sinon.stub(window, 'isSetup').returns(true);
17481764
this.sinon.stub(window, 'handleStackInfo').throws(error);
17491765
// this would raise if the errors didn't match
17501766
Raven.captureException(error, {foo: 'bar'});
@@ -1753,22 +1769,33 @@ describe('Raven (public API)', function() {
17531769

17541770
it('should reraise a different error', function() {
17551771
var error = new Error('crap1');
1772+
this.sinon.stub(window, 'isSetup').returns(true);
17561773
this.sinon.stub(window, 'handleStackInfo').throws(error);
17571774
assert.throws(function() {
17581775
Raven.captureException(new Error('crap2'));
17591776
}, error);
17601777
});
17611778

17621779
it('should capture as a normal message if a non-Error is passed', function() {
1780+
this.sinon.stub(window, 'isSetup').returns(true);
17631781
this.sinon.stub(Raven, 'captureMessage');
17641782
this.sinon.stub(window, 'handleStackInfo')
17651783
Raven.captureException('derp');
1784+
assert.isTrue(Raven.captureMessage.called);
17661785
assert.equal(Raven.captureMessage.lastCall.args[0], 'derp');
17671786
assert.isFalse(window.handleStackInfo.called);
17681787
Raven.captureException(true);
1788+
assert.isTrue(Raven.captureMessage.called);
17691789
assert.equal(Raven.captureMessage.lastCall.args[0], true);
17701790
assert.isFalse(window.handleStackInfo.called);
17711791
});
1792+
1793+
it('should not throw an error if not configured', function() {
1794+
this.sinon.stub(Raven, 'isSetup').returns(false);
1795+
this.sinon.stub(window, 'handleStackInfo')
1796+
Raven.captureException(new Error('err'));
1797+
assert.isFalse(window.handleStackInfo.called);
1798+
});
17721799
});
17731800

17741801
describe('.isSetup', function() {

0 commit comments

Comments
 (0)