From f36197727c25e399c37ed15b8839f319cbe54863 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 --- src/raven.js | 29 ++++++++++++++++------------- test/raven.test.js | 39 +++++++++++++++++++++++++-------------- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/src/raven.js b/src/raven.js index a9979e9b1b6b..e2b7e456ed37 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, @@ -757,13 +747,26 @@ function send(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 6b8044e8ae30..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'); @@ -1096,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/'; @@ -1772,8 +1781,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 +1840,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 441e55bd256aecd280c9c493ec8cc0527a182be7 Mon Sep 17 00:00:00 2001 From: Ben Vinegar Date: Wed, 19 Aug 2015 11:02:27 -0700 Subject: [PATCH 2/2] Enabling Raven.debug will log outbound request data --- src/raven.js | 22 +++++++++------------- test/raven.test.js | 26 ++++++++++++++++++++------ 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/raven.js b/src/raven.js index e2b7e456ed37..a293844885d6 100644 --- a/src/raven.js +++ b/src/raven.js @@ -750,18 +750,10 @@ function makeRequest(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; - } + logDebug('debug', 'Raven about to send:', data); + + if (!isSetup()) return; + img = newImage(); src = globalServer + authQueryString + '&sentry_data=' + encodeURIComponent(JSON.stringify(data)); if (globalOptions.crossOrigin || globalOptions.crossOrigin === '') { @@ -789,10 +781,14 @@ function newImage() { return document.createElement('img'); } +var ravenNotConfiguredError; + function isSetup() { if (!hasJSON) return false; // needs JSON support if (!globalServer) { - logDebug('error', 'Error: Raven has not been configured.'); + if (!ravenNotConfiguredError) + logDebug('error', 'Error: Raven has not been configured.'); + ravenNotConfiguredError = true; return false; } return true; diff --git a/test/raven.test.js b/test/raven.test.js index 49a57700cea6..c8aa1d2b991b 100644 --- a/test/raven.test.js +++ b/test/raven.test.js @@ -19,8 +19,8 @@ function flushRavenState() { tags: {}, extra: {} }, - startTime = 0 - ; + startTime = 0; + ravenNotConfiguredError = undefined; Raven.uninstall(); } @@ -286,17 +286,31 @@ describe('globals', function() { }); describe('isSetup', function() { + beforeEach(function () { + this.sinon.stub(window, 'logDebug'); + }); + it('should return false with no JSON support', function() { globalServer = 'http://localhost/'; hasJSON = false; assert.isFalse(isSetup()); }); - it('should return false when Raven is not configured', function() { - hasJSON = true; // be explicit + describe('when Raven is not configured', function () { + it('should return false when Raven is not configured', function() { + hasJSON = true; // be explicit + globalServer = undefined; + assert.isFalse(isSetup()); + }); + + it('should log an error message, the first time it is called', function () { + hasJSON = true; globalServer = undefined; - this.sinon.stub(window, 'logDebug'); - assert.isFalse(isSetup()); + isSetup(); + isSetup(); + assert.isTrue(window.logDebug.calledWith('error', 'Error: Raven has not been configured.')) + assert.isTrue(window.logDebug.calledOnce); + }); }); it('should return true when everything is all gravy', function() {