Clarification on new willTransitionTo callback
I am upgrading to 0.12 and I am slightly confused about how to handle the upgrade from wait to callback. If I call transition.redirect during some asychronous behavior, do I need to call the callback or should I not? Here is a sample of what I am trying to upgrade:
authPromise.then(function () {
if (!someCondition) {
transition.redirect('other-route');
}
}).catch(function (err) {
SignIn.attemptedTransition = transition;
transition.redirect('sign-in');
});
transition.wait(authPromise);
Do I need to call the callback in the catch of the promise? In the then handler should I call the callback in an else block of the if (!someCondition) or can I just call it all the time in that then handler?
You must call the callback if you define this parameter (which you need to define for “waiting”).
Just chain it as last step:
authPromise.then(function () {
if (!someCondition) {
transition.redirect('other-route');
}
}).catch(function (err) {
SignIn.attemptedTransition = transition;
transition.redirect('sign-in');
}).then(callback);
(You can also do .then(callback, callback) if you want navigation to occur regardless of failures in your code.)
If you're writing only sync code, you can omit callback in parameter list and router will call it for you.
So just to clarify, even if I redirect the transition away using transition.redirect, the callback should still be called.
Yes. It would be more error-prone if we were more forgiving and interpreted transition methods as callback calls.
Closing as this appears to be resolved.
@gaearon I just realized that this ends up swallowing any errors that I have in my render methods of components that use this mixin. Do you have any suggestions about how to handle this?
I think you should be able to rethrow?
.catch(function (err) {
SignIn.attemptedTransition = transition;
transition.redirect('sign-in');
throw err;
});
I tried this, and nothing appeared in the console:
.catch(function (err) {
SignIn.attemptedTransition = transition;
transition.redirect('sign-in');
}).then(callback).catch(function (err) {
throw err;
});
However, if I console.log in that last catch it exists, but for some reason throw is not doing anything.
Depending on the promise library you're using (like es6-promise) a thrown error that isnt handled doesnt bubble up to window. I ended up switching to Q because of this.
Yea, I am using es6-promise so that explains why nothing was happening. There has to be some way to surface the error without every single error in render being swallowed.
You can use setTimeout:
.catch(function (err) {
setTimeout(function(){
throw err;
}, 0);
});
Or simply:
.catch(function (err) {
SignIn.attemptedTransition = transition;
transition.redirect('sign-in');
}).then(function(){
setTimeout(function(){
callback();
}, 0);
});
Some libraries also provide a promise.done() for cases like this, but it's not part of the spec.
Seems so janky, but also will fix the issue. Thanks for the tip. This is why some days I just really don't like promises haha.
I tried this, and nothing appeared in the console:
This is not exactly what I wrote. You're rethrowing after first catch. This won't work because you're swallowing the error in the first catch. You need to rethrow there.
My original code wasn't right either; it seems that you want to fire callback before rethrowing. Hmm. What is that error you're catching? Can you check its type in catch and only rethrow unfamiliar errors?
In development, I had a SyntaxError in one of my render functions. What happened was that the error was being swallowed by the .then(callback). It resulted in a completely white page and nothing logged out to the console.
If I place another catch after the transition callback the error exists in that catch when I use console.log. I've verified that the error exists, but throwing it does nothing. It seems that the reason is due to how es6-promise behaves as merk described above.
To clarify the process looks like this:
transition call's callback -> Route render() -> error in render() -> swallowed