How to return the default object after a 404 response?

Hello All!

There is a model company and an model employee. The company contains the relation employee: belongsTo('employee')

If there is no employee and REST gives me a 404 response, everything crashes. I had an idea to return default employee object with status 200 to make it show up correctly via handleResponse.

But it doesn’t work that way. As soon as the adapter receives a response with the status 404, an error is issued. Regardless of what I will do in handleResponse, change status and payload.

Maybe there is a better solution for such a case?

Is the relationship being fetched based on id? The best fix seems like a change to the server-side behavior. E.g. if it’s an id based fetched the company record shouldn’t have an employee id that doesn’t exist. If it’s using links it probably shouldn’t return a 404.

That said you should be able to find a solution with handleResponse. What did your handleResponse look like? And did you verify it was actually being executed?

I tryed to write handleResponse like this:

import ApplicationAdapter from 'app/adapters/application';

export default ApplicationAdapter.extend({
  handleResponse(status, headers, payload, requestData) {
    if (status === 404) {
      const unknownEmployee = {data:{client:{name:"unknown"}}};
      return this._super(200,headers,unknownEmployee,requestData);
    }
  }
});

At a glance that looks ok… have you put a breakpoint in that method to see what’s going wrong? I’d step into the if and make sure it’s being hit properly, and then if necessary step into the _super call to make sure it’s being handled how you expected there as well.