0

I'm using ember-data, and want to trap and display any errors returned by the rest adapter. I looked at the question here

I added the following code to my model definition:

 becameInvalid: function(errors) {
        alert ("here" + errors);
    },

and the rest adapter returns a 422 (Unprocessable Entity) code

however, the alert doesn't show. Am I missing something, or just being a real newbie numpty?

update #1:

making some progress. The rest server returns the following Json:

{"errors":{ "lastName": ["LastName cannot be blank"] }} 

the model has

becameInvalid: function(errors) { console.log(JSON.stringify(errors)); }, 

however, the console now has the following:

{"email":"jmls@foo,com","firstName":"Julian","id":"aa7c4b42-df64-8fb8-d213-0ad81‌​c9bc213","lastName":"","notes":"ccc"}

which seems to be the json of the record itself, not of the errors.

How can I get to the errors? I have tried

console.log(errors.get("errors.lastName")

but get undefined.

Cœur
  • 32,421
  • 21
  • 173
  • 232
jmls
  • 2,597
  • 4
  • 27
  • 50

3 Answers3

0

try:

becameError: function(object) {

}
fanta
  • 1,479
  • 13
  • 15
0

I think that your are missing something, using becameInvalid worked for me.

For example:

App.Person = DS.Model.extend({
    name: DS.attr('string') ,
    becameInvalid: function(errors) {        
        alert(errors.get('errors.name').join(','));
    }
});

Update

Following the suggestion of @fanta, in the commend. Maybe your problem is in the returned json, the expected is:

{
  errors: {
    field_a: ['error a', 'error b'],
    field_b: ['error c']
  }
}

Where field_a must be some field mapped on DS.attr(field_a).

Give a look in that sample http://jsfiddle.net/marciojunior/8maNq/

Marcio Junior
  • 18,813
  • 4
  • 41
  • 47
  • it might be possible that he is not returning the name of the field with the error, or the name does not match the one defined in Ember Model, names should match. – fanta Aug 30 '13 at 22:45
0

Try using the DS.rejectionHandler:

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

  throw reason;
};

This should catch all errors from the adapter.

Firpo
  • 51
  • 3
  • Do you have the latest version of ember-data from http://builds.emberjs.com/? Older versions might not have the rejection handler. – Firpo Sep 01 '13 at 19:14
  • yeah, updating my app to use 1.0 beta of ember-data - will let you know what it fixes ;) – jmls Sep 03 '13 at 05:28