Skip to content

Commit

Permalink
Better handling of uncaught exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
felixge committed Jul 20, 2011
1 parent 65eb868 commit 6429b45
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
8 changes: 5 additions & 3 deletions lib/airbrake.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,17 @@ Airbrake.createClient = function(key, env) {
Airbrake.prototype.handleExceptions = function() {
var self = this;
process.on('uncaughtException', function(err) {
self.log('Airbrake: Uncaught Exception:');
self.log(err.stack);

self.notify(err, function(notifyErr, url) {
if (notifyErr) {
self.log('Uncaught Exception: Could not notify Airbreak!');
self.log('Airbrake: Could not notify service.');
self.log(notifyErr.stack);
} else {
self.log('Uncaught Exception: Airbreak was notified: ' + url);
self.log('Airbrake: Notified service: ' + url);
}

self.log(err.stack);
process.exit(1);
});
});
Expand Down
11 changes: 7 additions & 4 deletions test/fast/test-handle-exceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var sinon = require('sinon');
(function testNotifyForUnhandledExceptions() {
sinon.stub(process, 'on');
sinon.stub(airbrake, 'notify');
sinon.stub(airbrake, 'log');

airbrake.handleExceptions();

Expand All @@ -20,6 +21,10 @@ var sinon = require('sinon');
handler(err);

assert.ok(airbrake.notify.calledWith(err));
assert.ok(airbrake.log.calledWith('Airbrake: Uncaught Exception:'));
assert.ok(airbrake.log.calledWith(err.stack));

airbrake.log.restore();

var notifyCb = airbrake.notify.args[0][1];

Expand All @@ -29,8 +34,7 @@ var sinon = require('sinon');

notifyCb();

assert.ok(/uncaught exception: airbreak was notified/i.test(airbrake.log.args[0][0]));
assert.strictEqual(airbrake.log.args[1][0], err.stack);
assert.ok(/notified service/i.test(airbrake.log.args[0][0]));
assert.ok(process.exit.calledWith(1));

process.exit.restore();
Expand All @@ -44,9 +48,8 @@ var sinon = require('sinon');

notifyCb(notifyErr);

assert.ok(/uncaught exception: could not notify airbreak/i.test(airbrake.log.args[0][0]));
assert.ok(/could not notify service/i.test(airbrake.log.args[0][0]));
assert.strictEqual(airbrake.log.args[1][0], notifyErr.stack);
assert.strictEqual(airbrake.log.args[2][0], err.stack);
assert.ok(process.exit.calledWith(1));

process.exit.restore();
Expand Down

0 comments on commit 6429b45

Please sign in to comment.