Skip to content

Commit

Permalink
feat: add unhandledRejectionHandler (#3)
Browse files Browse the repository at this point in the history
Removes handling of rejected promises from the unhandled
exception handler and adds an unhandled rejection handler
to replace the functionality. This allows users to distinguish
between the two situations.

BREAKING CHANGE: unhandledExceptionHandler no longer
catches rejections.
  • Loading branch information
Simone Primarosa authored and Tapppi committed Aug 3, 2017
1 parent c9605dd commit 96a194f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ add.uncaughtExceptionHandler = function (hook) {

if (errHooks.length === 1) {
process.once('uncaughtException', exit.bind(null, true, 1));
}
};

// Add an unhandled rejection handler
add.unhandledRejectionHandler = function (hook) {
errHooks.push(hook);

if (errHooks.length === 1) {
process.once('unhandledRejection', exit.bind(null, true, 1));
}
};
Expand Down
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ exitHook.uncaughtExceptionHandler(err => {
console.error(err);
});

// You can hook unhandled rejections with unhandledRejectionHandler()
exitHook.unhandledRejectionHandler(err => {
console.error(err);
});

// You can add multiple uncaught error handlers
// Add the second parameter (callback) to indicate async hooks
exitHook.uncaughtExceptionHandler((err, callback) => {
Expand Down
4 changes: 2 additions & 2 deletions test/cases/unhandled-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ exitHook(function () {
stub.called();
});

exitHook.uncaughtExceptionHandler(function (err, cb) {
exitHook.unhandledRejectionHandler(function (err, cb) {
setTimeout(function () {
stub.called();
cb();
}, 50);
if (!err || err.message !== 'test-promise') {
stub.reject(`No error passed to uncaughtExceptionHandler, or message not test-promise - ${err.message}`);
stub.reject(`No error passed to unhandledRejectionHandler, or message not test-promise - ${err.message}`);
}
stub.called();
});
Expand Down

0 comments on commit 96a194f

Please sign in to comment.