question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: Interceptor not triggered

See original GitHub issue

Here is my interceptor

axios.interceptors.response.use((response) => {
  return response
}, function (error) {
  // Do something with response error
  if (error.response.status === 401) {
    auth.logout()
    router.replace('/login')
  }

  return Promise.reject(error)
})

During this call which returns a 401, the interceptor is not triggered. I can catch the reject() where I called method.

axios.get('/api/employees/')
      .then(response => {
        commit('SET_EMPLOYEE_LIST', response.data)
        resolve(response)
      })
      .catch(err => reject(err))
  • axios version: v0.16.2
  • Environment: chrome 59, windows 10

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
Alexandre-Georgescommented, Jul 13, 2017

I have had a similar issue, it seems like the axios instance is different when the interceptor is added and when a call is performed.

You also probably want to have a better handling of the 401 in the interceptor.

I found a workaround for the not triggered interceptor although it is probably a bug, I add the interceptor to the instance itself:

const instance = axios.create();
instance.interceptors.response.use((response) => {
  console.log(`response ${response}`);
  return response;
}, (error) => {
  console.log(`error ${error}`);
  return Promise.reject(error);
});
0reactions
paddelbootcommented, May 14, 2020

This is weird. In all the tutorials and SE answers, I see the interceptor being added to the axios instance. But this didn’t work for me at all. Instead, I had to add it to the imported axios module before calling the create() method:

import axios from 'axios';

// Redirect if API return unauthorized
axios.interceptors.response.use(function (response) {

console.log( 'interceptor' );

return response;
}, function (error) {

console.log( error );

if (401 === error.response.status) {

    window.location = '/login';

} else {
    return Promise.reject(error);
}
});

const axiosInstance = axios.create({
baseURL: `http://example.org/`,
headers: {
    'Content-Type': 'application/json'
},
withCredentials: true
});

export default axiosInstance;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Interceptor not intercepting http requests (Angular 6)
In my case interceptor wasn't getting involved for service calls because I had imported HttpClientModule multiple times, for different ...
Read more >
Interceptor not triggering error function on 401 response #5120
I'm trying to do this by checking if I receive a 401 response, if that was caused by an expired token, and if...
Read more >
That's why your Angular Interceptor may NOT WORK! [5 ...
EDIT: This problem does not occur when you import HttpClientModule only ONCE in the AppModule or CoreModule (and import the CoreModule into ...
Read more >
Angular - handle HTTP errors using interceptors
An error interceptor is a special type of interceptor which is used for handling errors that arise while making an HTTP request. The...
Read more >
HTTP Requests and Error Handling with Angular Interceptors
Create a folder called error-dialog in the app folder. · Create a service for errors called errorDialogService in a file called errordialog.service.ts. ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found