I'm trying to write a handler for all failed routes in my Ember application.
The documentation here seems to suggest that I can make a ErrorRoute on my App object which will automatically be transitioned to when routing on another route failed. I want to use this to redirect the user to a login page if the reason for the routing failure is due to an authentication problem (such as token timeout).
The problem I have is that inside the ErrorRoute I don't seem to have any way to access the error returned by the route that failed. I want to check for sure that it was an authentication error before redirecting them to the login screen and I'm not sure how to do that.
Here is what I wrote for testing:
App.ErrorRoute = Ember.Route.extend({
activate: function() {
console.log("MODEL A", this.modelFor('error'));
setInterval(function() {
console.log("MODEL B", this.modelFor('error'));
}.bind(this), 1000);
}
});
When the route is activated, the console logs MODEL A undefined as it tries to access the model for the App.ErrorController which isn't set yet for some reason. After a second the MODEL B console log fires and the error model has been set up.
If I can't access the error in the Route's activate method then where can I access it? Presumably I'm not supposed to wrap my logic in a timeout.
You could manage the error in any route of your current active hierarchy.
Normally, you setup your error handler at the application route to perform your app error logic.
App.AccountRoute = Ember.Route.extend({
afterModel: function() {
return new Em.RSVP.Promise(function(resolve, reject){
throw new AuthenticatedError('error message');
});
}
});
App.ApplicationRoute = Ember.Route.extend({
actions: {
error: function(error) {
if ( error instanceof AuthenticatedError ) {
this.transitionTo('login');
} else {
// if return true, the event will bubble and transition to error
return true;
}
}
}
});
http://emberjs.jsbin.com/cisur/1/edit
The error model is passed to the error route's renderTemplate(controller, model) method.
If you don't care about landing on a *.error route substate, if you're going to redirect anyway, #ppcano's answer is sufficient. But sometimes you want to encapsulate all error handling in a dedicated error route object. renderTemplate then is your place to handle things.
I agree though, it would be nice if its hooks had the correct modelFor(...) available.
Related
I'd like to render message about server error. I create special route that present error message view. At the same time I don't want redirect to that route's url to be able to reload current page. So I decided to use function render in application route.
// application/route.js
actions: {
error: function(error, transition) {
if (error.errors) {
return this.render('server-error.internal');
}
return true;
}
}
It's working correctly when I redirect do page with error from other application's route that load correctly. When I type manually url of page with error I take error
Cannot read property 'push' of undefined
As I checked it's about uninitialized route's property named 'connections'. I found similar issues on github, but I couldn't find solution. I'll be grateful for any help.
I'm using ember-cli: 2.4.3
I found workaround for this issue, that could be useful for somebody with similar issue. Instead of using error event I used error substate. See documentation. Theoretically it should work the same, but in this case it doesn't.
I created new route error and inside renderTemplate method I'm setting target url, checking type of error (with custom function) and rendering proper template:
// app/pods/error/route.js
export default Ember.Route.extend({
renderTemplate: function(controller, model) {
const transition = this.get('router.router.activeTransition');
const url = this.get("router").generate(transition.targetName);
this.get("router").router.updateURL(url);
if (model.errors) {
if (is404Error(model.errors)) {
return this.render('server-error.not-found');
} else if (is500Error(model.errors)) {
return this.render('server-error.maintenance');
}
}
return true;
}
});
I have the following in my user route (Ember 1.9) so that I can preemptively avoid having ember data make a call to my server when a non-int parameter is in the url:
MyApp.UserRoute = Ember.Route.extend({
model: function (params, transition) {
var identifier = params.identifier;
if (isNaN(parseInt(identifier))) {
this.intermediateTransitionTo("error", new Error("oops"));
return true;
}
else {
return this.store.find('user', identifier);
}
}
});
I'm returning true because that's apparently what will make error events bubble up. This works well enough but it gets odd since the transition isn't completely interrupted or aborted, so then it continues on to my UserIndexRoute:
MyApp.UserIndexRoute = Ember.Route.extend({
redirect: function(model){
model.get('someprop'); //blows up because get is undefined on true
}
});
Is there a "standard" return value that will act just like a failed promise? I would think an intermediateTransitionTo("error", ...) plus a failed promise in the UserRoute would immediately have Ember transition to error without calling redirect on the AccountIndexController.
I've seen examples in error handling tutorials where to make a promise reject, a new Ember.RSVP.Promise is returned with a reject("error") inside, but I don't know if that's suitable for production.
I may be using a totally incorrect approach for my problem and if so please tell me
My Meteor app collects emails address and emails them a link to a download page with a token. This download page is a Iron Router route and the token is the ID of an item in a collection. The token is checked for prior use and then a download will be initiated [that part not written yet]. So I have this route:
this.route('download', {
path: '/download/:_id',
template: 'capture_download',
waitOn: function () {
return Meteor.subscribe('captures');
},
data: function() { return Captures.findOne(this.params._id); }
});
So I need to trigger a call to my server method that does the checking logic as soon as this route is loaded. And I need the ID value to make that call. So I have this:
Template.capture_download.rendered = function(template) {
Meteor.call('claimDownload', this.data._id, function(err, result) {
// callback logic here
});
}
What I don't understand is that this only sometimes works. Sometimes the call happens with the ID value correct. Other times I get:
Exception from Deps afterFlush function function: TypeError: Cannot read property '_id' of null
So I'm thinking that either my template event [rendered] is wrong [I can't find in the docs a list of template events anywhere], or that I need to do something to wait for a valid this value, or that my approach is totally wrong. How would I fix this occasional lack of data in the view when rendered?
Use onBeforeAction within your Iron Router route, rather than a rendered method in the template:
this.route('download', {
path: '/download/:_id',
template: 'capture_download',
waitOn: function () {
return Meteor.subscribe('captures');
},
data: function() { return Captures.findOne(this.params._id); },
onBeforeAction: function() {
Meteor.call('claimDownload', this.params._id, function(err, result) {
// callback logic here
});
}
});
See https://github.com/EventedMind/iron-router/blob/dev/DOCS.md#before-and-after-hooks. Your “checking for token prior use” sounds a lot like the “checking that the user is logged in” example in the docs, which is solved with onBeforeAction.
I've run into an annoying issue when loading data asynchronously in an ember route's model callback. The issue seems to be that if the model method of my route returns a promise which is rejected then the route will never attempt to re-evaluate that route model. It just automatically returns the same rejected promise the next time it tries to go to that route without even trying to re-fetch the data!
I understand from this answer that an ember route will only call it's model method when trying to convert the url into a model. I'm guessing that in the case of routes with dynamic segments it may be called if it has never encountered that particular dynamic segment before.
Here is what I've got in my router setup.
window.App = Ember.Application.create({
LOG_TRANSITIONS: true,
LOG_TRANSITIONS_INTERNAL: true
});
App.Router.map(function() {
this.route('login');
this.resource('users', { path: '/users' }, function() {
this.resource('user', { path: '/:user_id' });
this.route('create', { path: '/create' });
});
});
And this is my route.
App.UserRoute = Ember.Route.extend({
model: function(params) {
// This returns a promise
return App.User.fetch(params.user_id);
}
});
I have some special handling for errors in my application route so that routes which fail due to authentication exceptions redirect the user to the login screen.
App.ApplicationRoute = Ember.Route.extend({
actions: {
sessionExpired: function() {
this.controllerFor('login').set("tokenExpired", true);
this.transitionTo('login');
},
error: function(err) {
if (err.type === "TokenException") {
this.send('sessionExpired');
}
}
}
});
The Problem
I navigate to the /users route
For some reason my token expires (inactivity, whatever...)
I navigate to the /users/1 route
The route's model method returns a promise which rejects and I am kicked out to the login screen
I log back in and try to navigate back to the /users/1 route
The route automatically just returns the same failed promise it did last time and I'm kicked out to the login screen. :(
I'm thinking that what I want is some way to clear all the evaluated route models after a user logs in. If this was a multi-user system and one user logs out and another user logs in on the same computer without refreshing the page then that new user shouldn't have routes automatically resolved from the previous user's session.
This seems to me like it would be a common problem yet I can't find any sort of app-wide invalidate cache method. How should I solve this?
I'm not sure where ember data stands on the cache clearing feature, but here is one way to do it
clearCache: function (type) {
var map = App.store.typeMapFor(type);
map.idToCid = {};
map.clientIds = [];
map.recordArrays = [];
map.findAllCache = null;
}
And here is an example as to how the ember firebase library handles a fail find using cache clearing.
delete store.typeMapFor(store.modelFor('user')).idToRecord[username];
Full example here:
https://github.com/firebase/emberFire/blob/master/examples/blog/js/app.js
For anyone else who finds this - I never found a way to reset the ember application and cause it to forget all resolved routes. I did find a few other work-arounds.
In the end, I opted to just window.reload() any time that a user logged out of the system or had their authentication token expire.
Authenticated URLs
Another reasonable approach would be to put a random unique id in the hash state. Essentially just do this.
Instead of a route like:
#/contacts/1
prefix every authenticated route with some kind of unique id
#/PyUE4E+JEdOaDAMF6CwzAQ/contacts/1
App.reset
I tried tried a number of things. One of the more promising things I tried was redirecting to the login screen and using the Application's reset method on my global App object. http://emberjs.com/api/classes/Ember.Application.html#method_reset
That didn't work though, it seems that even a reset Application remember's the models of any routes that it has resolved - weird.
New to ember.js. What I'm trying to do is: create a transitional route that has no path, that I can pass an AJAX promise to as the model when I transition to it, and then it makes a redirect decision once the promise completes. I want the LoadingRoute view to be invoked while this is happening. I've tried to accomplish that with the following route:
App.LoginUserRoute = Ember.Route.extend({
setupController: function(controller, model) {
//Model should be a promise
model.then(this.success.bind(this),this.failure.bind(this));
},
success: function (data) {
if (data.success) {
App.loggedInUser = App.User.create(data);
console.log(App.loggedInUser);
//Make redirection decision
}
else {
alert(data.error);
}
},
failure: function () {
//Failure code
}
});
However, when I try to pass the promise to the route like follows:
var request = $.post("api.php", {action: 'create_user', username: this.username, password: this.password});
this.transitionToRoute('loginUser',request);
I get the error "More context objects were passed than there are dynamic segments for the route: loginUser" because I'm trying to create a pathless route and Ember requires that the model be serialized into the path when passed using transitionToRoute().
The reason I need this is:
The login event can happen from multiple controllers (when the user registers, when they login using the login screen, or when the application first loads if the right cookie is detected) and I don't want to duplicate the login logic across all those controllers.
After the login completes, there's multiple different routes the user could then be directed to, depending on the nature of the returned user object.
I want the LoadingRoute to be invoked while the request is completing.
I assume the solution to my problem is not to use routing, but rather something else. However, I'm not sure what the "something else" would be.
You'll want to take advantage of a mixin, and hooking into the transition route.
The following SO answer will work for all of your needs:
Ember Client Side Authentication, route authentication
In the end I achieved what I was trying to do by adding the following code to ApplicationController
loginUser: function (request) {
this.transitionToRoute('loading');
request.then(this.loginRequestSuccess.bind(this),this.loginRequestFailure.bind(this));
},
loginRequestSuccess: function (data) {
if (data.success) {
App.loggedInUser = App.User.create(data.user);
console.log(App.loggedInUser);
//Route transition decision
}
else {
alert(data.error);
}
},
loginRequestFailure: function () {
//Failure code
}
And then calling the loginUser() function with the jqXHR object from wherever in the code a login request was made.