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.

1
  • Can you show more informantion about your code, like model mapping and the returned json response? Commented Aug 30, 2013 at 22:50

3 Answers 3

0

try:

becameError: function(object) { } 
Sign up to request clarification or add additional context in comments.

Comments

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/

1 Comment

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

2 Comments

Do you have the latest version of ember-data from builds.emberjs.com? Older versions might not have the rejection handler.
yeah, updating my app to use 1.0 beta of ember-data - will let you know what it fixes ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.