# Handling errors with Ember Data using the JSON API standard

**URL:** <https://discuss.emberjs.com/t/handling-errors-with-ember-data-using-the-json-api-standard/8023>\
**Category:** Ember Data\
**Created:** [May 17, 2015, 8:48am UTC](https://discuss.emberjs.com/t/handling-errors-with-ember-data-using-the-json-api-standard/8023 "2015-05-17T08:48:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![danr1979](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/danr1979/32/11254_2.png) [@danr1979](https://discuss.emberjs.com/u/danr1979)\
**Post date:** [May 17, 2015, 8:48am UTC](https://discuss.emberjs.com/t/handling-errors-with-ember-data-using-the-json-api-standard/8023/1 "2015-05-17T08:48:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wzowee](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/wzowee/32/10892_2.png) [@wzowee](https://discuss.emberjs.com/u/wzowee)\
**Post date:** [May 21, 2015, 10:19am UTC](https://discuss.emberjs.com/t/handling-errors-with-ember-data-using-the-json-api-standard/8023/2 "2015-05-21T10:19:11Z")

</div>

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](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.](https://github.com/cerebris/jsonapi-resources) 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.

---

<div class="post-metadata">

**Author:** ![danr1979](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/danr1979/32/11254_2.png) [@danr1979](https://discuss.emberjs.com/u/danr1979)\
**Post date:** [May 21, 2015, 6:34pm UTC](https://discuss.emberjs.com/t/handling-errors-with-ember-data-using-the-json-api-standard/8023/3 "2015-05-21T18:34:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![engwan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/engwan/32/10434_2.png) [@engwan](https://discuss.emberjs.com/u/engwan)\
**Post date:** [May 23, 2015, 6:30am UTC](https://discuss.emberjs.com/t/handling-errors-with-ember-data-using-the-json-api-standard/8023/4 "2015-05-23T06:30:09Z")

</div>

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](http://emberjs.com/api/data/classes/DS.RESTAdapter.html#method_ajaxError)

That’s actually what ActiveModelAdapter implements.

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

---

<div class="post-metadata">

**Author:** ![danr1979](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/danr1979/32/11254_2.png) [@danr1979](https://discuss.emberjs.com/u/danr1979)\
**Post date:** [May 25, 2015, 6:42am UTC](https://discuss.emberjs.com/t/handling-errors-with-ember-data-using-the-json-api-standard/8023/5 "2015-05-25T06:42:04Z")

</div>

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.
