Data: JSON-API AssertionError with JSONSerializer

Created on 21 Oct 2016  路  7Comments  路  Source: emberjs/data

I've switched my serializer by adding the application serializer file -> app/serializers/application.js

import DS from 'ember-data';

export default DS.JSONSerializer.extend({
});

and I'm using the RESTAdapter, customized at app/adapters/application.js

export default DS.RESTAdapter.extend({
   host: 'http://60.60.60.60:3000/'
});

But when my server fails with validation errors with a 422, I get an assertion error that the errors should be in the JSON API format

ember.debug.js:19157 Error: Assertion Failed: `AdapterError` expects json-api formatted errors array.
at new Error (native)
at Error.EmberError (http://60.60.60.60:4200/assets/vendor.js:29613:21)
at assert (http://60.60.60.60:4200/assets/vendor.js:17311:13)
at Object.assert (http://60.60.60.60:4200/assets/vendor.js:29425:34)
at assert (http://60.60.60.60:4200/assets/vendor.js:66105:37)
at ErrorClass (http://60.60.60.60:4200/assets/vendor.js:77100:41)
at Class.handleResponse (http://60.60.60.60:4200/assets/vendor.js:78331:16)
at ajaxError (http://60.60.60.60:4200/assets/vendor.js:78852:25)
at Class.hash.error (http://60.60.60.60:4200/assets/vendor.js:78426:23)
at fire (http://60.60.60.60:4200/assets/vendor.js:3617:31)

My error payload is

{
    "errors": {
        "email": ["can't be blank"],
        "password": ["can't be blank"],
        "first_name": ["can't be blank"],
        "last_name": ["can't be blank"],
        "title": ["can't be blank", "Should be Mr or Ms"]
    }
}

Most helpful comment

So ember-data internally uses JSON-API. All errors which are pushed into ember-data need to be in the JSON-API format.

At the moment you need to convert your errors into the JSON-API format within your handleResponse handler on the adapter. You can re-use the DS.errorsHashToArray utility to convert your errors hash into an array Ember-Data expects. See this ember-twiddle for a way how you can do that.

Also, you are using the rest adapter in combination with the json serializer. Though this might work for your use case, the recommended way is to use the rest serializer when you also use that adapter.


4409 aims for a smoother error normalization handling, but until then you can use the way demonstrated in the twiddle.

All 7 comments

Hey, if you use the JSONAPI serializer the errors must be an array of objects.
Check these examples: http://jsonapi.org/examples/#error-objects-basics

But i'm using the JSONSerializer by customizing the application serializer -

export default DS.JSONSerializer.extend({
});

Am I missing something?

I think your error payload should be something like:

{
    "errors": [
        {
          "source": { "pointer": "/data/attributes/email" },
          "detail": "can't be blank"
        },
        {
          "source": { "pointer": "/data/attributes/password" },
          "detail": "can't be blank"
        },
        {
          "source": { "pointer": "/data/attributes/first-name" },
          "detail": "can't be blank"
        },
        {
          "source": { "pointer": "/data/attributes/last-name" },
          "detail": "can't be blank"
        },
        {
          "source": { "pointer": "/data/attributes/title" },
          "detail": "can't be blank, should be Mr or Ms"
        }
    ]
}

only if I''m using the JSONAPISerializer right?

I'm using the JSONSerializer, not JSONAPISerializer

So ember-data internally uses JSON-API. All errors which are pushed into ember-data need to be in the JSON-API format.

At the moment you need to convert your errors into the JSON-API format within your handleResponse handler on the adapter. You can re-use the DS.errorsHashToArray utility to convert your errors hash into an array Ember-Data expects. See this ember-twiddle for a way how you can do that.

Also, you are using the rest adapter in combination with the json serializer. Though this might work for your use case, the recommended way is to use the rest serializer when you also use that adapter.


4409 aims for a smoother error normalization handling, but until then you can use the way demonstrated in the twiddle.

Sorry! I totally missed that.
Did some digging on the source-code and it looks like json-api style errors are the default error style for Ember Data: http://emberjs.com/api/data/classes/DS.InvalidError.html

Thanks a lot @pangratz, will look into this. No worries @urbany.

I'll also look into ways of making our rails server JSON-API complaint.

Was this page helpful?
0 / 5 - 0 ratings