Skip to content

Commit 5cececa

Browse files
committed
Merge pull request #368 from getsentry/pr/364
Enabling Raven.debug will log outbound request data (refs #364)
2 parents 4ab2609 + 441e55b commit 5cececa

File tree

2 files changed

+58
-34
lines changed

2 files changed

+58
-34
lines changed

src/raven.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,6 @@ var Raven = {
232232
* @return {Raven}
233233
*/
234234
captureException: function(ex, options) {
235-
if (!isSetup()) {
236-
return Raven;
237-
}
238-
239235
// If not an Error is passed through, recall as a message instead
240236
if (!isError(ex)) return Raven.captureMessage(ex, options);
241237

@@ -267,10 +263,6 @@ var Raven = {
267263
* @return {Raven}
268264
*/
269265
captureMessage: function(msg, options) {
270-
if (!isSetup()) {
271-
return Raven;
272-
}
273-
274266
// config() automagically converts ignoreErrors from a list to a RegExp so we need to test for an
275267
// early call; we'll error on the side of logging anything called before configuration since it's
276268
// probably something you should see:
@@ -699,8 +691,6 @@ function getHttpData() {
699691
}
700692

701693
function send(data) {
702-
if (!isSetup()) return;
703-
704694
var baseData = {
705695
project: globalProject,
706696
logger: globalOptions.logger,
@@ -757,13 +747,18 @@ function send(data) {
757747

758748

759749
function makeRequest(data) {
760-
var img = newImage(),
761-
src = globalServer + authQueryString + '&sentry_data=' + encodeURIComponent(JSON.stringify(data));
750+
var img,
751+
src;
752+
753+
logDebug('debug', 'Raven about to send:', data);
754+
755+
if (!isSetup()) return;
762756

757+
img = newImage();
758+
src = globalServer + authQueryString + '&sentry_data=' + encodeURIComponent(JSON.stringify(data));
763759
if (globalOptions.crossOrigin || globalOptions.crossOrigin === '') {
764760
img.crossOrigin = globalOptions.crossOrigin;
765761
}
766-
767762
img.onload = function success() {
768763
triggerEvent('success', {
769764
data: data,
@@ -786,10 +781,14 @@ function newImage() {
786781
return document.createElement('img');
787782
}
788783

784+
var ravenNotConfiguredError;
785+
789786
function isSetup() {
790787
if (!hasJSON) return false; // needs JSON support
791788
if (!globalServer) {
792-
logDebug('error', 'Error: Raven has not been configured.');
789+
if (!ravenNotConfiguredError)
790+
logDebug('error', 'Error: Raven has not been configured.');
791+
ravenNotConfiguredError = true;
793792
return false;
794793
}
795794
return true;

test/raven.test.js

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ function flushRavenState() {
1919
tags: {},
2020
extra: {}
2121
},
22-
startTime = 0
23-
;
22+
startTime = 0;
23+
ravenNotConfiguredError = undefined;
2424

2525
Raven.uninstall();
2626
}
@@ -286,17 +286,31 @@ describe('globals', function() {
286286
});
287287

288288
describe('isSetup', function() {
289+
beforeEach(function () {
290+
this.sinon.stub(window, 'logDebug');
291+
});
292+
289293
it('should return false with no JSON support', function() {
290294
globalServer = 'http://localhost/';
291295
hasJSON = false;
292296
assert.isFalse(isSetup());
293297
});
294298

295-
it('should return false when Raven is not configured', function() {
296-
hasJSON = true; // be explicit
299+
describe('when Raven is not configured', function () {
300+
it('should return false when Raven is not configured', function() {
301+
hasJSON = true; // be explicit
302+
globalServer = undefined;
303+
assert.isFalse(isSetup());
304+
});
305+
306+
it('should log an error message, the first time it is called', function () {
307+
hasJSON = true;
297308
globalServer = undefined;
298-
this.sinon.stub(window, 'logDebug');
299-
assert.isFalse(isSetup());
309+
isSetup();
310+
isSetup();
311+
assert.isTrue(window.logDebug.calledWith('error', 'Error: Raven has not been configured.'))
312+
assert.isTrue(window.logDebug.calledOnce);
313+
});
300314
});
301315

302316
it('should return true when everything is all gravy', function() {
@@ -327,7 +341,6 @@ describe('globals', function() {
327341
Raven.debug = true;
328342
this.sinon.stub(console, level);
329343
logDebug(level, message, {}, 'foo');
330-
assert.isTrue(console[level].calledOnce);
331344
});
332345
});
333346

@@ -825,15 +838,6 @@ describe('globals', function() {
825838
});
826839

827840
describe('send', function() {
828-
it('should check `isSetup`', function() {
829-
this.sinon.stub(window, 'isSetup').returns(false);
830-
this.sinon.stub(window, 'makeRequest');
831-
832-
send();
833-
assert.isTrue(window.isSetup.calledOnce);
834-
assert.isFalse(window.makeRequest.calledOnce);
835-
});
836-
837841
it('should build a good data payload', function() {
838842
this.sinon.stub(window, 'isSetup').returns(true);
839843
this.sinon.stub(window, 'makeRequest');
@@ -1096,6 +1100,25 @@ describe('globals', function() {
10961100
this.sinon.stub(window, 'newImage', function(){ var img = {}; imageCache.push(img); return img; });
10971101
})
10981102

1103+
it('should check `isSetup`', function() {
1104+
this.sinon.stub(window, 'isSetup').returns(false);
1105+
makeRequest({foo: 'bar'});
1106+
assert.isTrue(window.isSetup.called);
1107+
});
1108+
1109+
it('should not create the image if `isSetup` is false', function() {
1110+
this.sinon.stub(window, 'isSetup').returns(false);
1111+
makeRequest({foo: 'bar'});
1112+
assert.isFalse(window.newImage.called);
1113+
});
1114+
1115+
it('should log to console', function() {
1116+
this.sinon.stub(window, 'isSetup').returns(true);
1117+
this.sinon.stub(window, 'logDebug');
1118+
makeRequest({foo: 'bar'});
1119+
assert.isTrue(window.logDebug.called);
1120+
});
1121+
10991122
it('should load an Image', function() {
11001123
authQueryString = '?lol';
11011124
globalServer = 'http://localhost/';
@@ -1772,8 +1795,9 @@ describe('Raven (public API)', function() {
17721795
it('should not throw an error if not configured', function() {
17731796
this.sinon.stub(Raven, 'isSetup').returns(false);
17741797
this.sinon.stub(window, 'send')
1775-
Raven.captureMessage('foo');
1776-
assert.isFalse(window.send.called);
1798+
assert.doesNotThrow(function() {
1799+
Raven.captureMessage('foo');
1800+
});
17771801
});
17781802

17791803
});
@@ -1830,8 +1854,9 @@ describe('Raven (public API)', function() {
18301854
it('should not throw an error if not configured', function() {
18311855
this.sinon.stub(Raven, 'isSetup').returns(false);
18321856
this.sinon.stub(window, 'handleStackInfo')
1833-
Raven.captureException(new Error('err'));
1834-
assert.isFalse(window.handleStackInfo.called);
1857+
assert.doesNotThrow(function() {
1858+
Raven.captureException(new Error('err'));
1859+
});
18351860
});
18361861
});
18371862

0 commit comments

Comments
 (0)