fetch-intercept icon indicating copy to clipboard operation
fetch-intercept copied to clipboard

Intercepting 401 or responses not working?

Open nexeh opened this issue 4 years ago • 1 comments

I didnt want to comment on issue #16 but i see the same issue. i really just care about intercepting a 401 but the response console.log is never hit. even if its the only thing in there. responseError does tho.


fetchIntercept.register({
  responseError: function(error) {
    console.log('responseError', error)
  }
});

fetchIntercept.register({
  response: function(response) {
    console.log('fetchIntercept response', response)
    if (response.status >= 401) throw new Error('Request error');
    return response;
  }
});

// Call fetch to see your interceptors in action.
fetch('http://google.com');

nexeh avatar Apr 07 '20 19:04 nexeh

Hi! I had the same problem. I've used the response interceptor with an async function that will check the response.ok and if its false, I call the function I created for the responseError with the response parameter as the error itself. In essence, it's a bypass to the method that is already called for all other response status besides the 401. hope it helps!

Cheers

ruifcnunes avatar May 10 '22 09:05 ruifcnunes