Looks like we've hit a roadblock here.
You have limited options
While some libraries offer features to handle uncaught promise rejections automatically, Q lacks this functionality. In Q, you need to use .done
or switch to a different promise library. Even native promises are expected to address this soon.
Solution for Q:
In Q, your best bet is to utilize .done
instead of then
as it allows you to throw exceptions without suppression. Remember to always end chains with done
:
myObj.loadDataSet(handleSuccess, handleError).done(); // will throw on rejection
Until Q resolves these issues, I hesitate to recommend it to others.
Embracing modern libraries and native promises
I have proposed a specification inspired by the work of Domenic and Petka for better error handling in promise libraries. Some libraries like bluebird and when already support this. Domenic is also working on a specification for web browsers.
Currently, supported or upcoming libraries include: bluebird, when, es6-promise, rsvp, and native promises in io.
// log any unhandled promise rejections
process.on('unhandledRejection', function(reason, p){
console.log("Possibly Unhandled Rejection at: Promise ", p, " reason: ", reason);
// application specific logging here
});
For browsers, a similar approach can be taken:
window.addEventListener("unhandledrejection", function(e) {
var reason = e.detail.reason;
var promise = e.detail.promise;
console.log("Unhandled rejection", promise, reason);
});
Although less common, efforts are being made to integrate this protocol into native promises. Currently, Firefox's native promises report unhandled rejections, and Chrome aims to do so as well, although browser hooks are still in progress.
Exciting developments in promise debugging tools are underway. Following discussions with Paul Irish, promising advancements in browser tooling for debugging promises are expected, potentially bringing native promises closer to the debuggable level of bluebird promises.