5
  1. What is the ember-data's current default expected response from the server if it got something other than 200, so it doesn't throw an uncaught exception but instead parses the errors for later use?

    e.g. {"errors":{jsonerror}} ???

  2. How should I handle server error responses (json format) in Ember?

Thanks!

wonderwall
  • 317
  • 1
  • 2
  • 10

2 Answers2

16

@colymba got it almost right, but I'll try to make a more elaborated answer so it helps you get up and running easier.

As already stated in the new router facelift when an attempt is made to transition into a route, any of the hooks beforeModel, model and afterModel may throw an error, or return a promise that rejects, in this case an error event will be fired on the partially entered route, allowing you to handle errors on a per route basis.

App.SomeRoute = Ember.Route.extend({
  model: function() {
    //assuming the model hook rejects due to an error from your backend
  },
  events: {
   // then this hook will be fired with the error and most importantly a Transition
   // object which you can use to retry the transition after you handled the error
   error: function(error, transition) {
     // handle the error
     console.log(error.message);
     // retry the transition
     transition.retry();
  }
});

In the case you want to have errors be handled application wide you can define your own global default error handler by overriding the error handler on the ApplicationRoute, this could look something like this:

App.ApplicationRoute = Ember.Route.extend({
  events: {
    error: function(error, transition) {
      // handle the error
      console.log(error.message);
    }
  }
});

Take into account that by default, an event will stop bubbling once a handler defined on the events hash handles it. But to continue bubbling the event all the way up to the ApplicationRoute you should return true from that handler, so it can notify more error handler from which you can then retry to make the transition to the route with transition.retry().

Update for ember 1.0 and upwards

Since the new ember release 1.0, the events hash was renamed to actions. A route's error handler should be in the actions hash now:

App.ApplicationRoute = Ember.Route.extend({
  actions: {
    error: function(error, transition) {
      // handle the error
      console.log(error.message);
    }
  }
});

Hope this helps you.

Community
  • 1
  • 1
intuitivepixel
  • 23,302
  • 3
  • 57
  • 51
  • Thanks for your help! I have one more question though, how do I access the json error in the 404 response from the server in ember though? – wonderwall Jul 13 '13 at 22:22
  • @wonderwall can you inspect the hole `error` object? might be in a different property stored – intuitivepixel Jul 13 '13 at 22:29
  • Class {id: "1", store: Class, _reference: Object, stateManager: Class, _changesToSync: Object…} __ember1373755186212: "ember380" __ember1373755186212_meta: Meta _changesToSync: Object _reference: Object _super: undefined id: "1" stateManager: Class store: Class toString: function () { return ret; } transaction: Class __proto__: Object – wonderwall Jul 13 '13 at 22:40
  • btw I am on Ember.js 1.0.0-rc6 – wonderwall Jul 13 '13 at 22:45
  • well, I digged around and lamentably with ember-data it's not yet possible to get to the errors returned by the backend... you will only be notified that there was an error at all, see also this answer for reference: http://stackoverflow.com/questions/15569778/how-to-handle-404-of-ember-data-in-route, Sorry :( – intuitivepixel Jul 13 '13 at 22:56
  • no worries, thanks a lot, I currently have my own hacky solution which involves overriding ember's restadapter ajax call to not throw an rejection but to parse into user object as normally would, while adding an extra field "exception" to my model, this way, I can actually handle the exception in my router however I see fit, do you immediately see ominous things from this solution? – wonderwall Jul 13 '13 at 23:04
  • Have you looked at `DS.rejectionHandler` I used it briefly in my project. Updated my answer accordingly... – colymba Jul 15 '13 at 16:47
  • @intuitivepixel `events` is now deprecated, could you update your example? – Jon Koops Nov 15 '13 at 04:49
  • @JonKoops, thanks for pointing that out, added an update to the answer. – intuitivepixel Nov 15 '13 at 07:23
2

In the latest Router, errors bubble up to the Router and can be caught in the

events: { error: function(error, transition){}}

hook on the Router which works well when used with the model hook on the Router... See https://gist.github.com/machty/5647589 for details and samples..

Edit:

With Ember-data I used a custom

DS.rejectionHandler = function(reason) {
  Ember.Logger.assert([reason, reason.message, reason.stack]);
  throw reason;
};

See https://github.com/emberjs/data/blob/master/packages/ember-data/lib/adapters/rest_adapter.js

The reason param gives you access to reason.responseText, reason.status, reason.statusText etc....

colymba
  • 2,644
  • 15
  • 15