I am using Ember.js and ember-cli v2.7.0.
As stated in the Ember Guide on Loading and Error Substates one can react to an error that occurs when processing the beforeModel
, model
, and/or afterModel
, hooks by creating an error
action, e.g.:
export default Ember.Route.extend({
actions: {
error(err, transitionTo) {
if (err) {
return this.transitionTo('error');
}
}
}
...
}
If I place that error action in the routes/application.js route I will supposedly be able to react to errors that occur in any of those hooks.
I would like the default behavior of the application to render a full page error, ideally allowing for child routes to redefine this behaviour and render the error into a flash message region or a modal, as examples of alternate error handling depending on the interaction model. When displaying a full page error, I would prefer to do so without transitioning to a new URL. For example if someone requests: http://ift.tt/2bz8dXi
and that path errors (for any reason), I would like the default behavior to be to display either a 404, 500, in the main of the application.
Now, it seems like this.transitionTo
is not what I want, because that will take me to the error page route I have defined (routes/error.js) and as far as I can tell it transitions without an implicit error model in the above form. Attempting to pass err
as the second argument causes errors so I don't know how to properly pass the error context from the error action to the new route.
Also, as far as I can tell, there is no guarantee what form the error, err
, will take within the error action. If you follow the JSONAPI spec you might end up with one form of error (a JSON object) but if you invoke invalid javascript code, e.g. null.boom
it would appear from my testing that the error is different. How does one account for this? I would like to be able to know--whether the error comes from the client or the server--if I should be trying to render a 404, a 500 page, or something else.
(As a bonus, is there any way to get Ember to throw a 404, or similar, error when a user attempts to navigate to a URL that doesn't exist in the router? Globbing maybe?)
I would also like to know if my assumption that I can have some child route, e.g. some.child.route pre-empt the default error handling in favor of say an alert or modal, is correct.
I have spent a fair amount of time over the last few months learning Ember.js but this subject seems to have very little knowledge shared on it.
Finally, are there any good articles or tutorials out there that really dissect the topic of error routing and error handling in Ember.js, particularly in the 2.x release series?
Help?
(Thanks in advance!)
Aucun commentaire:
Enregistrer un commentaire