From ee0868e0f7bc24e7ec04591d9bd0f3d096ff3d0c Mon Sep 17 00:00:00 2001 From: Michael Hayes Date: Tue, 22 Oct 2013 14:08:47 -0700 Subject: [PATCH 1/3] added an optional callback before handling an exception --- src/raven.js | 26 ++++++++++++++++++++++++-- test/raven.test.js | 21 +++++++++++++++++++-- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/raven.js b/src/raven.js index 742b56bc02b0..e437e7bb34a8 100644 --- a/src/raven.js +++ b/src/raven.js @@ -101,6 +101,10 @@ var Raven = { TK.remoteFetching = true; } + if (globalOptions.collectWindowErrors) { + TK.collectWindowErrors = true; + } + if (globalOptions.linesOfContext) { TK.linesOfContext = globalOptions.linesOfContext; } @@ -120,7 +124,7 @@ var Raven = { install: function() { if (!isSetup()) return; - TK.report.subscribe(handleStackInfo); + TK.report.subscribe(handleErrorReport); return Raven; }, @@ -182,7 +186,7 @@ var Raven = { * @return {Raven} */ uninstall: function() { - TK.report.unsubscribe(handleStackInfo); + TK.report.unsubscribe(handleErrorReport); return Raven; }, @@ -338,6 +342,24 @@ function getAuthQueryString() { return cachedAuth; } +function handleErrorReport(stackInfo, options) { + var cb = globalOptions.shouldReportErrorCallback, + logged = false; + + if (cb && cb(stackInfo, options, handleStack)) { + logged = true; + handleStackInfo(stackInfo, options); + } else { + handleStackInfo(stackInfo, options); + } + + function handleStack() { + if (!logged) { + handleStackInfo(stackInfo, options); + } + } +} + function handleStackInfo(stackInfo, options) { var frames = []; diff --git a/test/raven.test.js b/test/raven.test.js index 7e2fe4b2c6e0..8aab25782b17 100644 --- a/test/raven.test.js +++ b/test/raven.test.js @@ -765,6 +765,23 @@ describe('globals', function() { }); }); +describe('handleErrorReport', function() { + afterEach(function() { + flushRavenState(); + }); + + it('should pass args to handleStackInfo', function() { + var stackInfo = {}, + options = {} + + setupRaven(); + this.sinon.stub(window, 'handleStackInfo') + + handleErrorReport() + assert.isTrue(window.handleStackInfo.calledOnce) + }) +}) + describe('Raven (public API)', function() { afterEach(function() { flushRavenState(); @@ -868,7 +885,7 @@ describe('Raven (public API)', function() { this.sinon.stub(TK.report, 'subscribe'); assert.equal(Raven, Raven.install()); assert.isTrue(TK.report.subscribe.calledOnce); - assert.equal(TK.report.subscribe.lastCall.args[0], handleStackInfo); + assert.equal(TK.report.subscribe.lastCall.args[0], handleErrorReport); }); }); @@ -963,7 +980,7 @@ describe('Raven (public API)', function() { this.sinon.stub(TK.report, 'unsubscribe'); Raven.uninstall(); assert.isTrue(TK.report.unsubscribe.calledOnce); - assert.equal(TK.report.unsubscribe.lastCall.args[0], handleStackInfo); + assert.equal(TK.report.unsubscribe.lastCall.args[0], handleErrorReport); }); }); From 2a445b0b6870c992305cc72a3cf7b1be4863e281 Mon Sep 17 00:00:00 2001 From: Michael Hayes Date: Tue, 22 Oct 2013 14:46:35 -0700 Subject: [PATCH 2/3] more tests --- src/raven.js | 8 +++--- test/raven.test.js | 61 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/src/raven.js b/src/raven.js index e437e7bb34a8..3fe2896d4e57 100644 --- a/src/raven.js +++ b/src/raven.js @@ -347,14 +347,14 @@ function handleErrorReport(stackInfo, options) { logged = false; if (cb && cb(stackInfo, options, handleStack)) { - logged = true; - handleStackInfo(stackInfo, options); - } else { - handleStackInfo(stackInfo, options); + handleStack(); + } else if(!cb) { + handleStack(); } function handleStack() { if (!logged) { + logged = true; handleStackInfo(stackInfo, options); } } diff --git a/test/raven.test.js b/test/raven.test.js index 8aab25782b17..77e50e79502a 100644 --- a/test/raven.test.js +++ b/test/raven.test.js @@ -770,7 +770,7 @@ describe('handleErrorReport', function() { flushRavenState(); }); - it('should pass args to handleStackInfo', function() { + it('should pass call handleStackInfo', function() { var stackInfo = {}, options = {} @@ -780,6 +780,65 @@ describe('handleErrorReport', function() { handleErrorReport() assert.isTrue(window.handleStackInfo.calledOnce) }) + + it('should not call handleStackInfo if cb returns false', function() { + var cb = this.sinon.stub().returns(false) + this.sinon.stub(window, 'handleStackInfo') + Raven.config(SENTRY_DSN, { + shouldReportErrorCallback: cb + }) + handleErrorReport() + assert.isTrue(cb.calledOnce) + assert.equal(window.handleStackInfo.callCount, 0) + }) + + it('should call handleStackInfo if cb returns true', function() { + var cb = this.sinon.stub().returns(true) + this.sinon.stub(window, 'handleStackInfo') + Raven.config(SENTRY_DSN, { + shouldReportErrorCallback: cb + }) + handleErrorReport() + assert.isTrue(cb.calledOnce) + assert.isTrue(window.handleStackInfo.calledOnce) + }) + + it('should call handleStackInfo if cb returns true', function() { + var cb = this.sinon.stub().returns(true) + this.sinon.stub(window, 'handleStackInfo') + Raven.config(SENTRY_DSN, { + shouldReportErrorCallback: cb + }) + handleErrorReport() + assert.isTrue(cb.calledOnce) + assert.isTrue(window.handleStackInfo.calledOnce) + }) + + it('should allow cb call handleStackInfo through a callback', function() { + var cb = function(stackInfo, options, done) { + done() + } + this.sinon.stub(window, 'handleStackInfo') + Raven.config(SENTRY_DSN, { + shouldReportErrorCallback: cb + }) + handleErrorReport() + assert.isTrue(window.handleStackInfo.calledOnce) + }) + + it('handleStackInfo should only be calledOnce per error', function() { + var cb = function(stackInfo, options, done) { + done() + done() + return true + } + this.sinon.stub(window, 'handleStackInfo') + Raven.config(SENTRY_DSN, { + shouldReportErrorCallback: cb + }) + handleErrorReport() + assert.equal(window.handleStackInfo.callCount, 1) + }) }) describe('Raven (public API)', function() { From 30cd2438e91a51cccd96213bbdb9819878f62b88 Mon Sep 17 00:00:00 2001 From: Michael Hayes Date: Tue, 22 Oct 2013 14:55:34 -0700 Subject: [PATCH 3/3] accedentally duplicated a test --- test/raven.test.js | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/test/raven.test.js b/test/raven.test.js index 77e50e79502a..cbe5ef2903a5 100644 --- a/test/raven.test.js +++ b/test/raven.test.js @@ -803,17 +803,6 @@ describe('handleErrorReport', function() { assert.isTrue(window.handleStackInfo.calledOnce) }) - it('should call handleStackInfo if cb returns true', function() { - var cb = this.sinon.stub().returns(true) - this.sinon.stub(window, 'handleStackInfo') - Raven.config(SENTRY_DSN, { - shouldReportErrorCallback: cb - }) - handleErrorReport() - assert.isTrue(cb.calledOnce) - assert.isTrue(window.handleStackInfo.calledOnce) - }) - it('should allow cb call handleStackInfo through a callback', function() { var cb = function(stackInfo, options, done) { done()