Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember -- JSON API Adapter Error Handling

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?

like image 328
TheCompiler Avatar asked Mar 06 '16 18:03

TheCompiler


Video Answer


2 Answers

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

like image 153
TheCompiler Avatar answered Oct 22 '22 05:10

TheCompiler


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.

like image 32
Deovandski Avatar answered Oct 22 '22 05:10

Deovandski