Data: handleResponse should use serializer to extract errors from payload

Created on 19 Apr 2017  路  5Comments  路  Source: emberjs/data

Our backend returns errors in payload._errors key, but REST and JSONAPI adapter expects errors in error key. To override the behaviour RESTAdapter.handleResponse must be overriden. But extraction of errors from payload should be serializer responsibility

Bug api-errors

Most helpful comment

@pangratz seems odd to have that hook on the adapter. Everywhere else the serializer is what understands the payload format, and the adapter is concerned primarily with how to interact with the server, treating the payload as opaque.

All 5 comments

@janmisek does it not work for you to implement extractErrors?

No it does not, because error response from our backend looks like this:

{
    _errors: [...]
}

But adapter expects to have errors in errors key and hardly depends on it: https://github.com/emberjs/data/blob/master/addon/adapters/rest.js#L966
Even documentation mentions hardcoded errors key: https://github.com/emberjs/data/blob/master/addon/adapters/rest.js#L48

As ember-data should be backend agnostic I would expect to have errors key customizable. Only solution I found is to override handleResponse. But I consider it dirty as parsing of payload should be by my opinion serializer responsibility.

My workaround using handleResponse of adapter:

export default DS.JSONAPIAdapter.extend({
  handleResponse(status, headers, payload, requestData) {
    if (payload && payload._errors) {
      payload.errors = payload._errors;
      delete payload._errors;
    }
    return this._super(...arguments);
  }
});

I can try to prepare pr ?

If I recall correctly #4409 aimed to add a normalizeInvalidErrorResponse hook for this purpose. Unfortunately it was closed and is pending a proper RFC... // cc @tchak

@pangratz seems odd to have that hook on the adapter. Everywhere else the serializer is what understands the payload format, and the adapter is concerned primarily with how to interact with the server, treating the payload as opaque.

Was this page helpful?
0 / 5 - 0 ratings