Skip to content

Commit 96a194f

Browse files
Simone PrimarosaTapppi
authored andcommitted
feat: add unhandledRejectionHandler (#3)
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.
1 parent c9605dd commit 96a194f

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ add.uncaughtExceptionHandler = function (hook) {
149149

150150
if (errHooks.length === 1) {
151151
process.once('uncaughtException', exit.bind(null, true, 1));
152+
}
153+
};
154+
155+
// Add an unhandled rejection handler
156+
add.unhandledRejectionHandler = function (hook) {
157+
errHooks.push(hook);
158+
159+
if (errHooks.length === 1) {
152160
process.once('unhandledRejection', exit.bind(null, true, 1));
153161
}
154162
};

readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ exitHook.uncaughtExceptionHandler(err => {
6767
console.error(err);
6868
});
6969

70+
// You can hook unhandled rejections with unhandledRejectionHandler()
71+
exitHook.unhandledRejectionHandler(err => {
72+
console.error(err);
73+
});
74+
7075
// You can add multiple uncaught error handlers
7176
// Add the second parameter (callback) to indicate async hooks
7277
exitHook.uncaughtExceptionHandler((err, callback) => {

test/cases/unhandled-promise.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ exitHook(function () {
1414
stub.called();
1515
});
1616

17-
exitHook.uncaughtExceptionHandler(function (err, cb) {
17+
exitHook.unhandledRejectionHandler(function (err, cb) {
1818
setTimeout(function () {
1919
stub.called();
2020
cb();
2121
}, 50);
2222
if (!err || err.message !== 'test-promise') {
23-
stub.reject(`No error passed to uncaughtExceptionHandler, or message not test-promise - ${err.message}`);
23+
stub.reject(`No error passed to unhandledRejectionHandler, or message not test-promise - ${err.message}`);
2424
}
2525
stub.called();
2626
});

0 commit comments

Comments
 (0)