Skip to content

Commit f361977

Browse files
benoitgbenvinegar
authored andcommitted
Log to console what would be sent to the server when raven isn't configured. Helps with local debugging of applications using raven.js
1 parent 4ab2609 commit f361977

File tree

2 files changed

+41
-27
lines changed

2 files changed

+41
-27
lines changed

src/raven.js

Lines changed: 16 additions & 13 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,26 @@ 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;
762752

753+
if (isSetup()) {
754+
logDebug('debug', 'Raven about to send:', data);
755+
}
756+
else {
757+
var ravenDebugOriginal = Raven.debug;
758+
//Ugly, but now that logDebug supports variadic arguments, there is little other choice
759+
//except duplicating the logDebug function.
760+
Raven.debug = true;
761+
logDebug('log', 'If configured, Raven would send:', data);
762+
Raven.debug = ravenDebugOriginal;
763+
return;
764+
}
765+
img = newImage();
766+
src = globalServer + authQueryString + '&sentry_data=' + encodeURIComponent(JSON.stringify(data));
763767
if (globalOptions.crossOrigin || globalOptions.crossOrigin === '') {
764768
img.crossOrigin = globalOptions.crossOrigin;
765769
}
766-
767770
img.onload = function success() {
768771
triggerEvent('success', {
769772
data: data,

test/raven.test.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,6 @@ describe('globals', function() {
327327
Raven.debug = true;
328328
this.sinon.stub(console, level);
329329
logDebug(level, message, {}, 'foo');
330-
assert.isTrue(console[level].calledOnce);
331330
});
332331
});
333332

@@ -825,15 +824,6 @@ describe('globals', function() {
825824
});
826825

827826
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-
837827
it('should build a good data payload', function() {
838828
this.sinon.stub(window, 'isSetup').returns(true);
839829
this.sinon.stub(window, 'makeRequest');
@@ -1096,6 +1086,25 @@ describe('globals', function() {
10961086
this.sinon.stub(window, 'newImage', function(){ var img = {}; imageCache.push(img); return img; });
10971087
})
10981088

1089+
it('should check `isSetup`', function() {
1090+
this.sinon.stub(window, 'isSetup').returns(false);
1091+
makeRequest({foo: 'bar'});
1092+
assert.isTrue(window.isSetup.called);
1093+
});
1094+
1095+
it('should not create the image if `isSetup` is false', function() {
1096+
this.sinon.stub(window, 'isSetup').returns(false);
1097+
makeRequest({foo: 'bar'});
1098+
assert.isFalse(window.newImage.called);
1099+
});
1100+
1101+
it('should log to console', function() {
1102+
this.sinon.stub(window, 'isSetup').returns(true);
1103+
this.sinon.stub(window, 'logDebug');
1104+
makeRequest({foo: 'bar'});
1105+
assert.isTrue(window.logDebug.called);
1106+
});
1107+
10991108
it('should load an Image', function() {
11001109
authQueryString = '?lol';
11011110
globalServer = 'http://localhost/';
@@ -1772,8 +1781,9 @@ describe('Raven (public API)', function() {
17721781
it('should not throw an error if not configured', function() {
17731782
this.sinon.stub(Raven, 'isSetup').returns(false);
17741783
this.sinon.stub(window, 'send')
1775-
Raven.captureMessage('foo');
1776-
assert.isFalse(window.send.called);
1784+
assert.doesNotThrow(function() {
1785+
Raven.captureMessage('foo');
1786+
});
17771787
});
17781788

17791789
});
@@ -1830,8 +1840,9 @@ describe('Raven (public API)', function() {
18301840
it('should not throw an error if not configured', function() {
18311841
this.sinon.stub(Raven, 'isSetup').returns(false);
18321842
this.sinon.stub(window, 'handleStackInfo')
1833-
Raven.captureException(new Error('err'));
1834-
assert.isFalse(window.handleStackInfo.called);
1843+
assert.doesNotThrow(function() {
1844+
Raven.captureException(new Error('err'));
1845+
});
18351846
});
18361847
});
18371848

0 commit comments

Comments
 (0)