6

I want to take a specific action when an API request results in a 404 error. I've read that the appropriated way to do this would be handling the error in the application adapter like below:

  handleResponse: function(status, headers, payload){
    if(status === 404 && payload.errors){
      //handle error
    }
    return this._super(...arguments);
  }

The problem is, as soon as I set up the adapter, it won't finish loading the page so I can handle the error on the page itself. Instead, it automatically takes me to some error route that just says "Adapter error". How can I stop/override this behaviour?

2 Answers 2

7

Turns out I needed to adjust the handleResponse hook in the application adapter to pass the new requestData parameter to the super method.

handleResponse (status, headers, payload, requestData) {
    return this._super(status, headers, payload, requestData);
},

Then I just changed the first line of the code in my question from

handleResponse: function(status, headers, payload) {

to

handleResponse: function(status, headers, payload, requestData) {

I was then able to catch and handle the error

Sign up to request clarification or add additional context in comments.

Comments

2

Perhaps you could handle it on a request basis like below:

return this.get('store').find('user', userId).then((user) => {
    this.set('user', user);
}).catch((reason) => {
    var possible404 = reason.errors.filterBy('status','404');
    if(possible404.length !== 0) {
      // Handle 404 error
    }
});

Although this is not the solution that you wanted, this alternative will allow you to customize more. By the way, the automatic behavior seems to be the application-error substates kicking in on 404, so give it a read if you want to override the behavior.

1 Comment

Thanks, I came up with another answer awhile ago, but forgot that I had posted this. I'll post the answer I came up with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.