Skip to content

Commit 4302b9e

Browse files
author
Tapani Moilanen
committed
fix: unhandled rejections now handled
1 parent 802173a commit 4302b9e

File tree

4 files changed

+64
-7
lines changed

4 files changed

+64
-7
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ add.uncaughtExceptionHandler = function (hook) {
148148

149149
if (errHooks.length === 1) {
150150
process.once('uncaughtException', exit.bind(null, true, 1));
151-
process.once('uncaughtRejection', exit.bind(null, true, 1));
151+
process.once('unhandledRejection', exit.bind(null, true, 1));
152152
}
153153
};
154154

test/cases/stub.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,41 @@ exports.addCheck = function (num) {
2828

2929
// Only call exit once, and save uncaught errors
3030
var called = false;
31-
var ucErr;
31+
var ucErrStr;
3232

3333
// Save errors that do not start with 'test'
3434
process.on('uncaughtException', function (err) {
3535
if (err.message.indexOf('test') !== 0) {
36-
ucErr = err;
36+
ucErrStr = err.stack;
37+
}
38+
});
39+
// Save rejections that do not start with 'test'
40+
process.on('unhandledRejection', function (reason) {
41+
if ((reason.message || reason).indexOf('test') !== 0) {
42+
ucErrStr = reason.message || reason;
3743
}
3844
});
3945

4046
// Check that there were no unexpected errors and all callbacks were called
41-
process.once('exit', function () {
47+
function onExitCheck(timeout) {
4248
if (called) {
4349
return;
4450
}
4551
called = true;
4652

47-
if (ucErr) {
48-
exports.reject(ucErr.stack);
53+
if (timeout) {
54+
exports.reject('Test timed out');
55+
} else if (ucErrStr) {
56+
exports.reject(ucErrStr);
4957
} else if (c === num) {
5058
exports.done();
5159
} else {
5260
exports.reject('Expected ' + num + ' callback calls, but ' + c + ' received');
5361
}
54-
});
62+
}
63+
64+
process.once('exit', onExitCheck.bind(null, null));
65+
setTimeout(onExitCheck.bind(null, true), 10000);
5566
};
5667

5768
// If the check isn't added, throw on exit

test/cases/unhandled-promise.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
var exitHook = require('./../../index');
3+
var stub = require('./stub');
4+
5+
exitHook(function (cb) {
6+
setTimeout(function () {
7+
stub.called();
8+
cb();
9+
}, 50);
10+
stub.called();
11+
});
12+
13+
exitHook(function () {
14+
stub.called();
15+
});
16+
17+
exitHook.uncaughtExceptionHandler(function (err, cb) {
18+
setTimeout(function () {
19+
stub.called();
20+
cb();
21+
}, 50);
22+
if (!err || err.message !== 'test-promise') {
23+
stub.reject(`No error passed to uncaughtExceptionHandler, or message not test-promise - ${err.message}`);
24+
}
25+
stub.called();
26+
});
27+
28+
process.on('unhandledRejection', function () {
29+
// All uncaught rejection handlers should be called even though the exit hook handler was registered
30+
stub.called();
31+
});
32+
33+
stub.addCheck(6);
34+
35+
(() => {
36+
return Promise.reject(new Error('test-promise'));
37+
})();

test/tests.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,12 @@ test('async exit timeout', t => {
103103
t.is(code, 0);
104104
});
105105
});
106+
107+
test('unhandled promise rejection', t => {
108+
t.plan(2);
109+
return testInSub('unhandled-promise')
110+
.then(([code, output]) => {
111+
t.is(output, 'SUCCESS');
112+
t.is(code, 0);
113+
});
114+
});

0 commit comments

Comments
 (0)