I have a controller, which deletes a record:
export default Ember.Controller.extend({
actions: {
deleteGroup: function(group) {
group.destroyRecord().then(function() {
this.get('controller').transitionToRoute('groups');
}.bind(this), function(error) {
group.rollback();
throw 'error';
}.bind(this));
}
}
});
Related to that post: Handle errors with ember data I need to manually call rollback in order to tell ember data to explicitly rollback that failed server call, or the client gets out of sync with the server. However, my problem is with the next line, where explicitly an error is thrown. It is noted in the docs that these errors might be swallowed by the promise and one can use something like this to finally receive the notification:
Ember.RSVP.configure('onerror', function(error) {
console.log('RSVP error: ' + error);
});
This works as it should, however I would like to display a notification to the user, so he knows that something went wrong. I use a controller to manage my notifications. Now, how do I access a controller from this function?
I noticed that I both can’t use this.get('controller')
and this.controllerFor('notification')
. It appears it does not get picked up by an error route either. What would be the right way to handle errors?
After quite some googling I found this speakerdeck https://speakerdeck.com/elucid/ember-errors-and-you which claims that you can get the app controller from the main App variable by calling App.__container__.lookup('controller:application');
. However, I don’t seem to have a __container__
property within the App variable (probably that’s caused by ember cli?). Anyways, there is a quite nice quote on stackoverflow related to the use of that global variable, which probably explains the amount of underscores used [1]
One of the core developers said that whenever someone tries to use App.container, he would add another underscore.
Isn’t there such a thing as a generic error handler, which I can plug into after the server returned an error and display an appropriate message to the user, which allows me to delegate to a controller to handle the error? Ideally I would like to see a chain like: Did the model handle the error, if not does the route or the controller handle the error? If nothing handles it, is there a hook for the application?
[1] view - Is it ok to use App.__container__.lookup to access instances? - Stack Overflow