Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON-API AssertionError with JSONSerializer #4608

Closed
maha99tests opened this issue Oct 21, 2016 · 7 comments
Closed

JSON-API AssertionError with JSONSerializer #4608

maha99tests opened this issue Oct 21, 2016 · 7 comments

Comments

@maha99tests
Copy link

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"]
    }
}
@urbany
Copy link

urbany commented Oct 21, 2016

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

@maha99tests
Copy link
Author

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

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

Am I missing something?

@urbany
Copy link

urbany commented Oct 21, 2016

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"
        }
    ]
}

@maha99tests
Copy link
Author

only if I''m using the JSONAPISerializer right?

I'm using the JSONSerializer, not JSONAPISerializer

@pangratz
Copy link
Member

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.

@urbany
Copy link

urbany commented Oct 21, 2016

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

@maha99tests
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants