1

At this moment i'm using a response interceptor instance.interceptors.response.use for global error handling. In that function i'm redirecting in case of a 404, i'm clearing the storage in case of a 401 etc. Which works fine :)

Except now I've got a spot where i want to handle the 404 error differently, but a simple try/catch or a .catch won't work since the axios interceptor intercepts the response and redirects immediately.

Whats the best practice in this situation?

  • Not using the interceptor for global error handling? (but then what? call a handleError kind of function after every request except this one?)
  • Temporary turn it off somehow
  • Pass an ignore404 option or something with the axios request (although I use a function per request so i can't do this really simple)
  • What would be the best, but i'm not sure how is that axios is always doing some global error reporting except when you catch it yourself. And at that point at the bottom of the catch i could let the global error handler do it's job anyway if i want to

My interceptor:

function onResponseRejected(error) {
  const { response } = error;

  if (!response) return Promise.reject(error); // network error, not axios related
  const { status } = response;
  const { errors } = response.data;

  if (status === 403) {
    router.push({ name: '403' });
  } else if (status === 404) {
    router.push({ name: '404' });
  }
....
....

My axios requests

export const fetch = (some) => {
  return get(`some/${someId}/url/`)
}

My application usage of a request

const response = fetch(this.$route.params.connectorId, this.$route.params.mapperId).catch(() => {
  console.log('At this moment the request will be redirected to another route...');
});
  • try this: https://stackoverflow.com/questions/48990632/how-to-manage-axios-errors-globally-or-from-one-point – eamanola Mar 29 '21 at 11:45
  • My interceptor is in an axios instance, my issue is that i'm handling errors inside the interceptor and therefor my app is already redirected by the interceptor so i can't handle anything local anymore. – Ricardo van Laarhoven Mar 29 '21 at 11:56
  • You can provide custom options to Axios per request - and then check in your interceptor whether the particular request requires some special handling (e.g. not redirecting on 401). – IVO GELOV Mar 29 '21 at 14:15

0 Answers0