Skip to content

Commit

Permalink
Merge pull request #84 from seegno/bugfix/recursion-patch-global
Browse files Browse the repository at this point in the history
Fix rare recursion condition in patchGlobal
  • Loading branch information
mattrobenolt committed Sep 9, 2014
2 parents 897c59c + 6815af6 commit 025188f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,31 @@ module.exports.patchGlobal = function patchGlobal(client, cb) {
// at the end, if we still don't have a Client, let's make one!
!(client instanceof Client) && (client = new Client());

var called = false;
process.on('uncaughtException', function(err) {
if(cb) { // bind event listeners only if a callback was supplied
client.once('logged', function() {
var onLogged = function onLogged() {
called = false;
cb(true, err);
});
client.once('error', function() {
};

var onError = function onError() {
called = false;
cb(false, err);
});
};

if(called) {
client.removeListener('logged', onLogged);
client.removeListener('error', onError);
return cb(false, err);
}

client.once('logged', onLogged);
client.once('error', onError);
}

called = true;

client.captureError(err, function(result) {
node_util.log('uncaughtException: '+client.getIdent(result));
});
Expand Down
27 changes: 27 additions & 0 deletions test/raven.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,33 @@ describe('raven.Client', function(){
});
process.emit('uncaughtException', new Error('derp'));
});

it('should not enter in recursion when an error is thrown on client request', function(done){
// remove existing uncaughtException handlers
var uncaughtBefore = process._events.uncaughtException;
process.removeAllListeners('uncaughtException');

var transportBefore = client.transport.send;

client.transport.send = function() {
throw new Error('foo');
};

client.patchGlobal(function(success, err){
success.should.eql(false);
err.should.be.instanceOf(Error);
err.message.should.equal('foo');

// restore things to how they were
process._events.uncaughtException = uncaughtBefore;
client.transport.send = transportBefore;

done();
});


process.emit('uncaughtException', new Error('derp'));
});
});

describe('#process()', function(){
Expand Down

0 comments on commit 025188f

Please sign in to comment.