I am struggling to handle an errors response using Ember Data and the ActiveModelAdapter. I have Googled around and the consensus is that the json format needs to be {errors: {field_name: [“My error message”, “another error”]}} but although I am returning this in a HTTP 500 response my model’s “errors” property remains an empty Ember object.
The controller action that performs the save on the ember data model looks like this:
changeStatus: function(statusId) {
this.set('statusIsLoading', true);
var myModel= this.get('model');
self = this;
myModel.set('statusId', statusId);
myModel.save().then(function(myModel){
self.set('statusIsLoading', false);
self.get('ui').setMessage('The status of this application was updated successfully.', '', 'green', 'checkmark');
}, function(response) {
self.set('statusIsLoading', false);
console.log(myModel.get('errors'));
self.set('errors', response.responseJSON.errors);
self.get('ui').setMessage('The status could not be updated because of the following errors:', '', 'red', 'warning');
});
},
When the error callback is called the “response” parameter contains the JSON “errors” key from the server but when I try to console log the “errors” key of the “application” model I get an empty Ember object. I cannot get the “errors” key of te model to populate at all and so I am currently having to get the error response using “response.responseJSON.errors” which is not really the “Ember way” as far as I can see.
Also, from what I have read Ember Data is compatible with the JSON API standard (created by Ember’s Yehuda Katz) so I’d like to return errors in the following format if possible:
http://jsonapi.org/format/#errors
This would allow me to return a “title”, “code” etc. However, all of the examples that I can find of Ember Data handling errors do not demonstrate this format being used and they seem to limit the error format to a field name key and one or more message strings. Am I mistaken in thinking that the ActiveModelAdapter can handle this type of response?
If anyone could help me with this I’d be grateful.
Thanks.