Gregg
Gregg

Reputation: 35864

Handling Errors with RESTSerializer

Using ember and ember-data 2.6 versions, I'm trying to get error handling working but running into issues. I'm using the RESTSerializer and I'm sending the following payload to my server:

{
    "brand": {
        "name": null,
        "description": null,
    }
}]

Since name cannot be null, this is what I'm sending back as my response:

{
    "errors": [{
        "code": null,
        "detail": "may not be null",
        "status": null,
        "title": null,
        "source": {
            "pointer": "brand/name",
            "parameter": null
        }
    }]
}

In my route, I am doing the following in the save promise on failure:

console.log(savedBrand.get('isValid')); // logs false
console.log(savedBrand.get('errors').toArray()); // logs [ ]

There doesn't seem to be a lot of detailed documentation on how this all works so I'm using this article as a starting point. I'm unsure as to why toArray() returns an empty array instead of my error that I'm returning in the JSON.

Upvotes: 0

Views: 70

Answers (1)

Gregg
Gregg

Reputation: 35864

even though I'm not passing in the data per the json-api, the pointer still had to be set as if I were:

{
    "errors": [{
        "code": null,
        "detail": "may not be null",
        "status": null,
        "title": null,
        "source": {
            "pointer": "/data/attributes/name",
            "parameter": null
        }
    }]
}

Upvotes: 1

Related Questions