fix: make AxiosError.cause non-enumerable to prevent circular reference in JSON.stringify#7526
Open
mango766 wants to merge 1 commit intoaxios:v1.xfrom
Open
fix: make AxiosError.cause non-enumerable to prevent circular reference in JSON.stringify#7526mango766 wants to merge 1 commit intoaxios:v1.xfrom
mango766 wants to merge 1 commit intoaxios:v1.xfrom
Conversation
…ce in JSON.stringify
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #7205
Since v1.12.0,
AxiosError.from()setscauseas a regular (enumerable) property via direct assignment. When the original error contains circular references (common with network errors that include socket objects),JSON.stringify(axiosError)fails with "Converting circular structure to JSON".The existing
toJSON()method already handles serialization correctly by explicitly listing safe properties — butJSON.stringifystill walks all enumerable properties includingcausebeforetoJSON()gets a chance to intervene when used in contexts likeconsole.logwith certain formatters or logging libraries that don't calltoJSON().Fix: Use
Object.definePropertyto setcauseas non-enumerable (matching how nativeError.causebehaves in V8), while keeping it configurable and writable for compatibility.This is a one-line change that restores the v1.11.0 behavior where
JSON.stringify(error)works without throwing.Summary by cubic
Make
AxiosError.causenon-enumerable to avoidJSON.stringifycircular reference errors introduced in v1.12.0. Restores pre-1.12 behavior and aligns with nativeError.cause.causeviaObject.definePropertyas non-enumerable (still configurable, writable).JSON.stringify(axiosError)from throwing when the original error has circular refs.toJSON()unchanged. No tests added—consider a regression test forJSON.stringifywith a circularcause.Written for commit 05d1538. Summary will update on new commits.