Handling errors with Ember Data using the JSON API standard

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.

Ember Data and the ActiveModelAdapter are not compatible with the current version of the JSON API spec.

There is an adapter being developed https://github.com/kurko/ember-json-api to match the spec though currently it is also a couple versions behind, with pending pull requests to bring it closer to the latest RC of JSON API.

If you’re not already using it you should take a look at GitHub - cerebris/jsonapi-resources: A resource-focused Rails library for developing JSON:API compliant servers. which combines with Rails 4+ to simplify the creation of servers that conform to the standard.

If you’re still stuck I’d suggest taking a look at those projects, I can only assume the model’s errors are not populated as the standard serializer doesn’t know where to find them. I’m only 1 week into the ember journey myself so others may have more insight.

Hi.

Thanks for your advice. However, I’m using a Laravel PHP back end rather than Rails & as you pointed out the jsonapi-resources project is out of date now.

It’s one of those things in Ember that you think will be easy & then find your self slogging away trying to get it working! Oh well, I’ll keep at it.

Thanks again.

You said you’re returning the errors inside the “errors” key, but looking at the way you’re getting the error, you’re probably storing it under a “responseJSON” key?

If you’re using Laravel or something else and your error messages are not in the same format as ActiveModelSerializers, you can always implement your own ember-data error handler.

Here’s the doc: RESTAdapter - 4.6 - Ember API Documentation

That’s actually what ActiveModelAdapter implements.

Maybe your error is because you returned the status code 500 instead of 422?

Thanks for your reply.

The responseJSON key is an Ember “thing” because I am not returning it as part if the JSON from my API & Laravel does not wrap its JSON responses in that key either.

I have tried using error 422 but with no luck. I appreciate I must be doing something wrong but I’m not sure what!

Thanks again.