Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
  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!

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

@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.

share|improve this answer
 
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 is there nothing in error.message? –  intuitivepixel Jul 13 '13 at 22:24
 
nope it's undefined –  wonderwall Jul 13 '13 at 22:26
 
@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
show 6 more comments

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....

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.