Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored AxiosError to be a constructor; Refactored Cancel [error] to be an AxiosError subclass; #3645

merged 1 commit into from Mar 10, 2022


Copy link

@DigitalBrainJS DigitalBrainJS commented Feb 13, 2021

This PR refactors error subsystem of the Axios to the class (constructors) paradigm. It has some breaking changes, since some error codes were changed. Details:

  • ECONNABORTED and ETIMEDOUT kept theirs old values.
  • now you can use instanceof operator to check whether object is an AxiosError subclass:
    • err instanceof AxiosError;
  • added static error codes to the AxiosError class to ensure they are the same for client&server builds;
    • AxiosError.ERR_NETWORK ; // 'ERR_NETWORK'
    • err.code === AxiosError.ERR_NETWORK ; // true
  • refactored old error codes and introduced some new codes instead of generic null code:
  • Refactored Cancel (error) to be CanceledError constructor, subclassed from AxiosError:
    • err instanceof CanceledError;
  • isAxiosError is still supported
  • axios.Cancel is just an alias for axios.CanceledError
  • isCancel(err) is still there

@DigitalBrainJS DigitalBrainJS marked this pull request as draft Feb 13, 2021
@DigitalBrainJS DigitalBrainJS marked this pull request as ready for review Feb 13, 2021
@@ -859,7 +859,7 @@ describe('supports http with nodejs', function () {
axios.get('http://localhost:4444/', {
cancelToken: source.token
}).catch(function (thrown) {
assert.ok(thrown instanceof axios.Cancel, 'Promise must be rejected with a Cancel obejct');
assert.ok(thrown instanceof axios.Cancel, 'Promise must be rejected with a CanceledError obejct');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: object

Copy link

LifeIsStrange commented Oct 13, 2021

I have not looked in depth about this PR but I'm curious about it.
My application has network error sources coming from multiple places, not only axios.
We wanted all our errors to be normalized to a standard shape AKA axiosError, even if they do not come from Axios.
As such we wanted to instantiate new AxiosErrors to wrap our other errors.
But I realized that AxiosError is just an interface and is therefore not instanciable.
I had to create my own class InstanciableAxiosError implementing the AxiosError interface to workaround this limitation.
If AxiosError become instanciable with this PR that would make the workaround irrelevant.

@DigitalBrainJS DigitalBrainJS marked this pull request as draft Oct 14, 2021
Refactored `Cancel` to a constructor, a subclass of the `AxiosError`;
Expose CanceledError class;
Refactored axios error codes;
Added `toFlatObject` util;
@DigitalBrainJS DigitalBrainJS marked this pull request as ready for review Oct 14, 2021
@DigitalBrainJS DigitalBrainJS mentioned this pull request Oct 22, 2021
Copy link

Argeento commented Nov 4, 2021

Woah! Good job! 💪 Finally nice error handling in TS (#2013) 🚀

It would be great if this PR was merged before axios-next release.

Copy link

alko89 commented Jan 11, 2022

This is great news! I noticed a similar thing was done in beta-1.0.0 branch. Was hoping I wouldn't have to wait that long to get this 😄

Copy link

jasonsaayman commented Mar 7, 2022

@DigitalBrainJS can you fix the merge conflict please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

Successfully merging this pull request may close these issues.

None yet

5 participants