-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Detect uncaught rejection in Promise #1926
Comments
We could take advantage of https://nodejs.org/api/process.html#process_event_unhandledrejection in newer versions of node as well |
I'd like to see support for tracking if (typeof process !== 'undefined') {
process.on('unhandledRejection', function (reason) {
throw reason;
});
} else if (typeof window !== 'undefined') {
// 2016-02-01: No browsers support this natively, however bluebird, when.js,
// and probably other libraries do.
if (typeof window.addEventListener === 'function') {
window.addEventListener('unhandledrejection', function (evt) {
throw evt.detail.reason;
});
} else {
var oldOHR = window.onunhandledrejection;
window.onunhandledrejection = function (evt) {
if (typeof oldOHR === 'function') oldOHR.apply(this, arguments);
throw evt.detail.reason;
};
}
} else if (typeof console !== 'undefined' &&
typeof (console.error || console.log) === 'function') {
(console.error || console.log)('Unhandled rejections will be ignored!');
} |
I agree that it would be great to have this support, especially in addition to the timeout message. Today when an error unintentionally gets caught in a Promise, eg. because an error was thrown from within callback that wasn't expected to be called from a Promise chain, one just gets a timeout error and no indication on what the cause may be. Adding any unhandled rejection data to at least the timeout error would be very helpful:
(The possibility of generally considering unhandled rejections to be test failures would also be kind of nice) |
@voxpelli I've written some stuff around the capability to detect and properly handle uncaught async exceptions here, but probably haven't covered this use-case. Originally, I had wanted to even detect async execution via |
Are this and #2640 duplicates of each other? |
closing, see duplicate #2640 |
Based on my node-project-template. To work around mochajs/mocha#1926 and mochajs/mocha#2640. Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
If I trigger an event from a promise
onFulfilled
function and an error is thrown from the handler of the event mocha doesn't detect this Error.I create a fiddle where this can be seen: http://jsfiddle.net/dsrhmvLz/3/.
AFAIK there is no standard way of detecting this unhandled promise rejection but using bluebird it can be detected with the
unhandledrejection
event as seen in the fiddle. Could it make sense adding support for this in mocha? There is another way of detecting this? If I'm not wrong currently something like this is done in mocha with theuncaughtException
event.With this little change (http://jsfiddle.net/dsrhmvLz/4/) the error is detected by mocha but it would seem nicer seeing the uncaught rejection in mocha without that trouble.
The text was updated successfully, but these errors were encountered: