axios icon indicating copy to clipboard operation
axios copied to clipboard

Need a way to handle rejection/resolve after request is completed

Open Hulkmaster opened this issue 4 years ago • 8 comments

Is your feature request related to a problem? Please describe.

A problem: Currently axios contains interceptors, which are executed before standard handlers. What we want to do: we want to have standard error handler, which will execute only if there is no local handler

Describe the solution you'd like

import Toast from 'toast';

const axiosInstance = axios.create();

axiosInstance.defaultReject((error) => {
  if (['401', '403', '404'].includes(error.status.code)) {
    Toast(error);
  }
  // in case we don't expect that issue at all we fail-fast
  return Promise.reject();
})

// guess also makes sense for some cases
axiosInstance.defaultResolve();

axiosinstance.get('whatever').catch((error) => {
  if (error.status.code === 404) {
    // show user that something was not found
  }
  // axiosInstance.defaultReject will execute
  return Promise.reject(error);
})

// in case of error axiosInstance.defaultReject will execute
axiosinstance.get('whatever-another');

axiosinstance.get('whatever-another-smoething).catch((error) => {
  // only that handler will execute, and not axiosInstance.defaultReject
});

Describe alternatives you've considered

Currently only 1 alternative was found - copypaste "standardhandler" everywhere in catch

Additional context

no

Hulkmaster avatar Aug 27 '21 08:08 Hulkmaster

@Hulkmaster Can response interceptor solve your issue ?

https://axios-http.com/docs/interceptors

with-shrey avatar Sep 09 '21 19:09 with-shrey

No, i need default handle which will run only if there is no try/catch, not always

Hulkmaster avatar Sep 09 '21 19:09 Hulkmaster

I am working on this feature. Can I be an assignee ?

theo-js avatar Sep 09 '21 23:09 theo-js

i guess we need to ask contributors to put you there

Hulkmaster avatar Sep 10 '21 07:09 Hulkmaster

I made a PR but I don't know how to link it to this issue

theo-js avatar Sep 16 '21 01:09 theo-js

you can just leave link to pr here, github will automatically mention it here

Hulkmaster avatar Sep 16 '21 07:09 Hulkmaster

you can also just mention current issue in your PR

https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword

Hulkmaster avatar Sep 16 '21 11:09 Hulkmaster

https://github.com/axios/axios/pull/4055

theo-js avatar Sep 16 '21 21:09 theo-js