From ee96db9b51c6de1b7999d8301fe55fa79220040c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benoit=20Gr=C3=A9goire?= Date: Mon, 26 Jan 2015 14:42:42 -0500 Subject: [PATCH 1/2] Log to console what would be sent to the server when raven isn't configured. Helps with local debugging of applications using raven.js Conflicts: src/raven.js --- src/raven.js | 17 ++++++----------- test/raven.test.js | 24 ++++++++++++++++++++---- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/raven.js b/src/raven.js index a9979e9b1b6b..7f2549f1d597 100644 --- a/src/raven.js +++ b/src/raven.js @@ -232,10 +232,6 @@ var Raven = { * @return {Raven} */ captureException: function(ex, options) { - if (!isSetup()) { - return Raven; - } - // If not an Error is passed through, recall as a message instead if (!isError(ex)) return Raven.captureMessage(ex, options); @@ -267,10 +263,6 @@ var Raven = { * @return {Raven} */ captureMessage: function(msg, options) { - if (!isSetup()) { - return Raven; - } - // config() automagically converts ignoreErrors from a list to a RegExp so we need to test for an // early call; we'll error on the side of logging anything called before configuration since it's // probably something you should see: @@ -699,8 +691,6 @@ function getHttpData() { } function send(data) { - if (!isSetup()) return; - var baseData = { project: globalProject, logger: globalOptions.logger, @@ -752,7 +742,12 @@ function send(data) { // Set lastEventId after we know the error should actually be sent lastEventId = data.event_id || (data.event_id = uuid4()); - makeRequest(data); + if (isSetup()) { + makeRequest(data); + } + else { + console.log("If configured, raven.js would send: ", data); + } } diff --git a/test/raven.test.js b/test/raven.test.js index 6b8044e8ae30..7b912974bb00 100644 --- a/test/raven.test.js +++ b/test/raven.test.js @@ -1086,6 +1086,20 @@ describe('globals', function() { extra: {'session:duration': 100} }); }); + + it('should log to console if not configured', function() { + this.sinon.stub(window, 'isSetup').returns(false); + this.sinon.stub(console, 'log'); + send({foo: 'bar'}); + assert.isTrue(console.log.called); + }); + + it('should NOT log to console if configured', function() { + this.sinon.stub(window, 'isSetup').returns(true); + this.sinon.stub(console, 'log'); + send({foo: 'bar'}); + assert.isFalse(console.log.called); + }); }); describe('makeRequest', function() { @@ -1772,8 +1786,9 @@ describe('Raven (public API)', function() { it('should not throw an error if not configured', function() { this.sinon.stub(Raven, 'isSetup').returns(false); this.sinon.stub(window, 'send') - Raven.captureMessage('foo'); - assert.isFalse(window.send.called); + assert.doesNotThrow(function() { + Raven.captureMessage('foo'); + }); }); }); @@ -1830,8 +1845,9 @@ describe('Raven (public API)', function() { it('should not throw an error if not configured', function() { this.sinon.stub(Raven, 'isSetup').returns(false); this.sinon.stub(window, 'handleStackInfo') - Raven.captureException(new Error('err')); - assert.isFalse(window.handleStackInfo.called); + assert.doesNotThrow(function() { + Raven.captureException(new Error('err')); + }); }); }); From 1101cb7835b3a519bad2d4d1c4bbc4fe2e67b22e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benoit=20Gr=C3=A9goire?= Date: Mon, 17 Aug 2015 16:06:16 -0400 Subject: [PATCH 2/2] Implement changes discussed in https://github.com/getsentry/raven-js/pull/364 --- src/raven.js | 26 +++++++++++++++++--------- test/raven.test.js | 43 +++++++++++++++++++------------------------ 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/src/raven.js b/src/raven.js index 7f2549f1d597..e2b7e456ed37 100644 --- a/src/raven.js +++ b/src/raven.js @@ -742,23 +742,31 @@ function send(data) { // Set lastEventId after we know the error should actually be sent lastEventId = data.event_id || (data.event_id = uuid4()); - if (isSetup()) { - makeRequest(data); - } - else { - console.log("If configured, raven.js would send: ", data); - } + makeRequest(data); } function makeRequest(data) { - var img = newImage(), - src = globalServer + authQueryString + '&sentry_data=' + encodeURIComponent(JSON.stringify(data)); + var img, + src; + if (isSetup()) { + logDebug('debug', 'Raven about to send:', data); + } + else { + var ravenDebugOriginal = Raven.debug; + //Ugly, but now that logDebug supports variadic arguments, there is little other choice + //except duplicating the logDebug function. + Raven.debug = true; + logDebug('log', 'If configured, Raven would send:', data); + Raven.debug = ravenDebugOriginal; + return; + } + img = newImage(); + src = globalServer + authQueryString + '&sentry_data=' + encodeURIComponent(JSON.stringify(data)); if (globalOptions.crossOrigin || globalOptions.crossOrigin === '') { img.crossOrigin = globalOptions.crossOrigin; } - img.onload = function success() { triggerEvent('success', { data: data, diff --git a/test/raven.test.js b/test/raven.test.js index 7b912974bb00..49a57700cea6 100644 --- a/test/raven.test.js +++ b/test/raven.test.js @@ -327,7 +327,6 @@ describe('globals', function() { Raven.debug = true; this.sinon.stub(console, level); logDebug(level, message, {}, 'foo'); - assert.isTrue(console[level].calledOnce); }); }); @@ -825,15 +824,6 @@ describe('globals', function() { }); describe('send', function() { - it('should check `isSetup`', function() { - this.sinon.stub(window, 'isSetup').returns(false); - this.sinon.stub(window, 'makeRequest'); - - send(); - assert.isTrue(window.isSetup.calledOnce); - assert.isFalse(window.makeRequest.calledOnce); - }); - it('should build a good data payload', function() { this.sinon.stub(window, 'isSetup').returns(true); this.sinon.stub(window, 'makeRequest'); @@ -1086,20 +1076,6 @@ describe('globals', function() { extra: {'session:duration': 100} }); }); - - it('should log to console if not configured', function() { - this.sinon.stub(window, 'isSetup').returns(false); - this.sinon.stub(console, 'log'); - send({foo: 'bar'}); - assert.isTrue(console.log.called); - }); - - it('should NOT log to console if configured', function() { - this.sinon.stub(window, 'isSetup').returns(true); - this.sinon.stub(console, 'log'); - send({foo: 'bar'}); - assert.isFalse(console.log.called); - }); }); describe('makeRequest', function() { @@ -1110,6 +1086,25 @@ describe('globals', function() { this.sinon.stub(window, 'newImage', function(){ var img = {}; imageCache.push(img); return img; }); }) + it('should check `isSetup`', function() { + this.sinon.stub(window, 'isSetup').returns(false); + makeRequest({foo: 'bar'}); + assert.isTrue(window.isSetup.called); + }); + + it('should not create the image if `isSetup` is false', function() { + this.sinon.stub(window, 'isSetup').returns(false); + makeRequest({foo: 'bar'}); + assert.isFalse(window.newImage.called); + }); + + it('should log to console', function() { + this.sinon.stub(window, 'isSetup').returns(true); + this.sinon.stub(window, 'logDebug'); + makeRequest({foo: 'bar'}); + assert.isTrue(window.logDebug.called); + }); + it('should load an Image', function() { authQueryString = '?lol'; globalServer = 'http://localhost/';