最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ember -- JSON API Adapter Error Handling - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Mar 15, 2016 at 8:17 Deovandski 7902 gold badges8 silver badges22 bronze badges asked Mar 6, 2016 at 18:58 TheCompilerTheCompiler 3082 silver badges11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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

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.

发布评论

评论列表(0)

  1. 暂无评论