Skip to content

Commit 1b531b1

Browse files
committed
Add unhandledRejection handler
1 parent c3fffb3 commit 1b531b1

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

lib/client.js

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ extend(Raven.prototype, {
7070
},
7171

7272
install: function install(cb) {
73-
patchGlobal(this, cb);
73+
registerGlobalHandlers(this, cb);
7474
return this;
7575
},
7676

@@ -390,18 +390,7 @@ defaultInstance.disableConsoleAlerts = utils.disableConsoleAlerts;
390390

391391
module.exports = defaultInstance;
392392

393-
function patchGlobal(client, cb) {
394-
// handle when the first argument is the callback, with no client specified
395-
if (typeof client === 'function') {
396-
cb = client;
397-
client = new Client();
398-
// first argument is a string DSN
399-
} else if (typeof client === 'string') {
400-
client = new Client(client);
401-
}
402-
// at the end, if we still don't have a Client, let's make one!
403-
!(client instanceof Raven) && (client = new Client());
404-
393+
function registerGlobalHandlers(client, cb) {
405394
var called = false;
406395
process.on('uncaughtException', function (err) {
407396
if (cb) { // bind event listeners only if a callback was supplied
@@ -423,11 +412,33 @@ function patchGlobal(client, cb) {
423412

424413
client.once('logged', onLogged);
425414
client.once('error', onError);
426-
}
427415

428-
called = true;
416+
called = true;
417+
}
429418

430419
var eventId = client.captureException(err);
431420
return utils.consoleAlert('uncaughtException: ' + eventId);
432421
});
422+
423+
process.on('unhandledRejection', function (reason) {
424+
var eventId = client.captureException(reason, function (sendErr) {
425+
cb && cb(!sendErr, reason);
426+
});
427+
return utils.consoleAlert('unhandledRejection: ' + eventId);
428+
});
429+
}
430+
431+
function patchGlobal(client, cb) {
432+
// handle when the first argument is the callback, with no client specified
433+
if (typeof client === 'function') {
434+
cb = client;
435+
client = new Client();
436+
// first argument is a string DSN
437+
} else if (typeof client === 'string') {
438+
client = new Client(client);
439+
}
440+
// at the end, if we still don't have a Client, let's make one!
441+
!(client instanceof Raven) && (client = new Client());
442+
443+
registerGlobalHandlers(client, cb);
433444
}

0 commit comments

Comments
 (0)