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

handleResponse should use serializer to extract errors from payload #4934

Closed
janmisek opened this issue Apr 19, 2017 · 6 comments
Closed

handleResponse should use serializer to extract errors from payload #4934

janmisek opened this issue Apr 19, 2017 · 6 comments
Labels
🏷️ bug This PR primarily fixes a reported issue

Comments

@janmisek
Copy link

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

@hjdivad
Copy link
Member

hjdivad commented Apr 19, 2017

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

@janmisek
Copy link
Author

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);
  }
});

@hjdivad hjdivad added the Bug label Apr 19, 2017
@janmisek
Copy link
Author

I can try to prepare pr ?

@pangratz
Copy link
Member

pangratz commented Apr 20, 2017

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

@hjdivad
Copy link
Member

hjdivad commented Apr 24, 2017

@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.

@runspired
Copy link
Contributor

Closing in favor of emberjs/rfcs#860

@runspired runspired added 🏷️ bug This PR primarily fixes a reported issue and removed Bug labels Sep 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bug This PR primarily fixes a reported issue
Projects
Archived in project
Development

No branches or pull requests

4 participants