fix: turn AxiosError into a native error (#5394)#5558
fix: turn AxiosError into a native error (#5394)#5558jasonsaayman merged 10 commits intoaxios:v1.xfrom
Conversation
|
After getting even deeper into the |
|
I think after the rewrite the code is much more understandable for people used to modern JavaScript. |
|
I needed to update |
|
@hexiaokang is there anything I can do to move forward with this? |
|
By the way, the current version also calculates the stack trace twice, wich is a performance problem. |
|
@brodo any chance you can look at the conflicts please |
|
@jasonsaayman I'll take a look later today. |
|
@jasonsaayman should be done now |
|
@brodo could you rebase pls |
There was a problem hiding this comment.
Pull Request Overview
This PR refactors AxiosError and CanceledError from constructor functions using prototypal inheritance to ES6 classes with native extends syntax. The main changes modernize the codebase to use class syntax while maintaining the same API surface.
Key changes:
- Converted
AxiosErrorfrom a constructor function to an ES6 class that extendsError - Converted
CanceledErrorto an ES6 class extendingAxiosError - Added tests to verify both errors are recognized as native errors by Node.js
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| lib/core/AxiosError.js | Refactored from constructor function to ES6 class, moved static error code properties outside class definition with comment explaining parser limitation |
| lib/cancel/CanceledError.js | Converted to ES6 class extending AxiosError, removed utils import as inherits helper is no longer needed |
| test/specs/core/AxiosError.spec.js | Added tests for native error detection and static error code property usage |
| test/specs/cancel/CanceledError.spec.js | Added test for native error detection |
Being an object returned by the 'Error' constructor turns something into a 'native error'.
This turns CanceledError into a native error.
If no error code is provided, use the code from the underlying error.
If a response is passed to the constructor, set the response status as a property.
13201e5 to
576e71a
Compare
|
@jasonsaayman done! |
* fix: turn AxiosError into a native error (axios#5394) Being an object returned by the 'Error' constructor turns something into a 'native error'. * fix: simplify code in AxiosError * fix: simplify code in AxiosError * refactor: implement AxiosError as a class * refactor: implement CanceledError as a class This turns CanceledError into a native error. * refactor: simplify AxiosError.toJSON * fix: improve error code handling in `AxiosError.from` If no error code is provided, use the code from the underlying error. * fix: set error status in `AxiosError.constructor` If a response is passed to the constructor, set the response status as a property. * fix: remove unnecessary async --------- Co-authored-by: Jay <jasonsaayman@gmail.com>
Overview
This PR turns
AxiosErrorinto an instance ofErrorthat returns true when checked with Node'sutils.types.isNativeError.Related Issue: #5394
Why this PR?
This PR is useful if axios is used in Node.js and the user wants to check if an instance of
AxiosError, that moved across realms, is anError. In practice this might happen if Node.js worker threads or vms are used.A call to
X instanceof Errorcompares X's prototype to theErrorconstructor in the current realm and thus returnsfalseif the object was created in another realm. Node's isNativeError can be used to address this:Node uses
isNativeError(e) || e instanceof Errorinternally to check for errors.What does the PR actually do?
Currently, the
AxiosErrorconstructor callsError.call(this)but it does not return its result. With the change the result is returned. Here is a short illustration of the difference:Testing
I wrote a test which should only run in NodeJS environments. Sadly, I could not get the tests under
test/specsrunning. The contributing guidelines mention grunt but I did not get it working. (Is this still up to date?) When runningnpm run teston a freshly checked-out repo I had several failures.