pablobm
pablobm

Reputation: 2066

404-like error from Ember-Data serializer

Using Ember and Ember-data, I work against an API that I don't control.

In a very specific case, it returns an apparently valid response (status 200), that turns out to be not that useful after inspecting it. However, this response is complex, and it's not until the serializer that I can realize this.

Therefore, the question: is there a way to return an error to the client (something similar to what a 404 response would do) from the serializer?

If the above is not possible, I have alternative ideas:

  1. Add code in the adapter that checks this complex response before handing it over to the serializer (this would introduce some annoying duplication and mixing of concerns)
  2. Return a null object of some kind, and have the client deal with that. I could have a service in between the client code and store, and have it doing this work to protect the client

Any other alternative ideas? Thank you in advance.

Upvotes: 1

Views: 146

Answers (1)

Alisdair McDiarmid
Alisdair McDiarmid

Reputation: 368

To reject an invalid or useless object at the serializer level, you would need to throw an exception or return undefined from the normalizeResponse method.

If you're loading resources as part of a model hook, this will cause the promise to reject, and you'll be able to handle the error as if it was a 404 from the API.

Upvotes: 1

Related Questions