Vue-resource: stop response in interceptors

Created on 4 Apr 2016  Â·  9Comments  Â·  Source: pagekit/vue-resource

We use global response interceptor to do authentication:
when the server returns 401, call vue-router to go to the Login route. After this point, the response should not be processed by any other interceptors or by user code any more.
The problem is that, if I understand correctly according to the code, the response will be sent down though the interceptor chain and to user code after all.
Can we have something like response.stop method to prevent it from further processing?

Feature request

Most helpful comment

@steffans Hello, I'm sorry but I still don't know how to stop response in interceptors.

When response is an error from server, how to handle the error globally and stop the Promise.resolve or Promise.reject?

I don't want to handle the error like 40* specifically in every component.

All 9 comments

I don't think this would be a good idea. In the particular case of authentication, you should handle logged out users in the backend, so when you have that error 401, send or update some cookie/header to inform that you are logged out, and the request that triggered the interceptor should handle that response. I do it like this, and I can't imagine other use case when stopping the response would be necesary, in fact it would be a problem. How would you handle that missing response? it isn't an error response, it's just a missing response

how to use router.go of vue-router in vue.http.interceptors of vue-resource, i want to redirect according to the response status

@listen-lavender In your top level component ( the first argument of router.start call), define:

created() {
  let self = this
  Vue.http.interceptors.push({
      response: function(response) {
         if(response.status === 401) {
           self.$route.router.go(...) 
         }
      }
  }
}

it is the same to global setting, it can't stop response in interceptors. Maybe the issue #5 is a solution. @Centaur

@listen-lavender Of course it can not. If it could, this issue would not exist in the first place.

Have look here i just updated the interceptors in the develop branch, https://github.com/vuejs/vue-resource/issues/224#issuecomment-226107447

@steffans I was struggling for a while because the documentation of the interceptors refers to the v0.8.0 but when you install it through npm install vue-resource the v0.7.4 was installed by default.

Not sure if that's something to check out.

@steffans Hello, I'm sorry but I still don't know how to stop response in interceptors.

When response is an error from server, how to handle the error globally and stop the Promise.resolve or Promise.reject?

I don't want to handle the error like 40* specifically in every component.

At first, my English is poor.
I just encountered this problem, my solution is as follows.
version: vue-resource v1.0.3

Vue.http.interceptors.push((req, next) => {
  next((res) => {
    if (!res.ok) {
      // toast message like error or warning, or do other things you like
    }
    // !this is the focus
    // in source code(.es2015.js line #1337), if return has no attribute of 'ok', it reject,
    // after add ‘ok’:true, it always resolve
    // now $http...then((res) => {}), this res is the response from backend
    return {...res.body, ok: true}
   })
})
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ayyobro picture ayyobro  Â·  3Comments

odranoelBR picture odranoelBR  Â·  6Comments

EmilMoe picture EmilMoe  Â·  5Comments

Dreampie picture Dreampie  Â·  3Comments

ramstein74 picture ramstein74  Â·  3Comments