I'd like to get a feel of general excepted exception-handling while using When().
I've got this init-code which creates the app and returns a promise when the app is done initializing.
The remainder of my code (the stuff depending on the app being initialized) goes in the body of the when, like so:
var app = new App().create(config);
when(app.createPromise,function(){
//everything runs here
}, function(err){
console.log(err)
});
While developing I'd like to throw the occasional exception around, which is caught in the controllers, the usual stuff. However, as everybody knows, sometimes you get an uncaught exception which bubbles up (hopefully only during dev), which normally is outputted on the console.
However, now I've got everything wrapped in a when() the thrown exception just disappears in void. It' s also not caught by the attached exception handler, which is pretty logical since I'm not doing a promise.reject().
What's the best way to approach this? I mean in this particular example, I'm just solving it without promises (go the oldfashioned callback route) , but I'd like to know the general way.
Or in short:
How to have a catch-all for exceptions when promises are involved ?
Thanks, Geert-Jan
var app = new App().create(config);
var resultPromise = when(app.createPromise, doSomething, handleError);
var app = new App().create(config);
var resultPromise = when(app.createPromise, doSomething);
// resultPromise represents either the successful result of doSomething
// OR ONE OF TWO POSSIBLE FAILURES:
// 1. The failure generated if app.createPromise rejected, or
// 2. The failure generated if doSomething throws an exception or returns a rejected promise
resultPromise.otherwise(handleError);
function init() {
return when(new App().create(config).createPromise, doSomething);
}
function otherCode() {
init()
.otherwise(
// As above, this will be called for one of two possible errors
// 1. The failure generated if app.createPromise rejected, or
// 2. The failure generated if doSomething throws an exception or returns a rejected promise
handleError
);
}
function init() {
return doSomething(new App().create(config).createdObject);
}
function otherCode() {
try {
init();
} catch(e) {
// As with promises, e again represents one of two possible failures!
// it is either an exception thrown by new App().create() or an exception
// thrown by doSomething.
handleError(e);
}
}