Skip to content

Commit

Permalink
Implement changes discussed in getsentry#364
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitg committed Aug 17, 2015
1 parent 21526f8 commit 26e04d7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 31 deletions.
28 changes: 18 additions & 10 deletions src/raven.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,19 +741,27 @@ 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()) {
//In two calls so data is formatted
logDebug('debug', 'Raven about to send:');
logDebug('debug', data);
}
else {
//In two calls so data is formatted
logDebug('log', 'If configured, Raven would send:', true);
logDebug('log', data, true);
return;
}
img = newImage();
src = globalServer + authQueryString + '&sentry_data=' + encodeURIComponent(JSON.stringify(data));
img.crossOrigin = 'anonymous';
img.onload = function success() {
triggerEvent('success', {
Expand Down Expand Up @@ -841,8 +849,8 @@ function uuid4() {
}
}

function logDebug(level, message) {
if (window.console && console[level] && Raven.debug) {
function logDebug(level, message, force) {
if (window.console && console[level] && (Raven.debug || force)) {
console[level](message);
}
}
Expand Down
54 changes: 33 additions & 21 deletions test/raven.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,13 @@ describe('globals', function() {
logDebug(level, message);
assert.isTrue(console[level].calledOnce);
});

it('should write to console when force is true, even if Raven.debug is false', function() {
Raven.debug = false;
this.sinon.stub(console, level);
logDebug(level, message, true);
assert.isTrue(console[level].calledOnce);
});
});

describe('setAuthQueryString', function() {
Expand Down Expand Up @@ -817,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');
Expand Down Expand Up @@ -1078,23 +1076,37 @@ describe('globals', function() {
extra: {'session:duration': 100}
});
});

it('should log to console if not configured', function() {
});

describe('makeRequest', function() {
it('should check `isSetup`', function() {
this.sinon.stub(window, 'isSetup').returns(false);
this.sinon.stub(console, 'log');
send({foo: 'bar'});
assert.isTrue(console.log.called);
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);
this.sinon.stub(window, 'newImage');
makeRequest({foo: 'bar'});
assert.isFalse(window.newImage.called);
});
it('should NOT log to console if configured', function() {

it('should log to console', function() {
this.sinon.stub(window, 'isSetup').returns(true);
this.sinon.stub(console, 'log');
send({foo: 'bar'});
assert.isFalse(console.log.called);
this.sinon.stub(window, 'logDebug');
makeRequest({foo: 'bar'});
assert.isTrue(window.logDebug.called);
});

it('should log to console, forcing output if isSetup is false', function() {
this.sinon.stub(window, 'isSetup').returns(false);
this.sinon.stub(window, 'logDebug');
makeRequest({foo: 'bar'});
assert.isTrue(window.logDebug.called);
assert.isTrue(window.logDebug.args[0][2], 'logDebug should have been called with force=true');
});
});

describe('makeRequest', function() {
it('should load an Image', function() {
authQueryString = '?lol';
globalServer = 'http://localhost/';
Expand Down

0 comments on commit 26e04d7

Please sign in to comment.