Skip to content

fix: make AxiosError.cause non-enumerable to prevent circular reference in JSON.stringify#7526

Open
mango766 wants to merge 1 commit intoaxios:v1.xfrom
mango766:fix/axios-error-cause-circular-ref
Open

fix: make AxiosError.cause non-enumerable to prevent circular reference in JSON.stringify#7526
mango766 wants to merge 1 commit intoaxios:v1.xfrom
mango766:fix/axios-error-cause-circular-ref

Conversation

@mango766
Copy link
Copy Markdown

@mango766 mango766 commented Mar 17, 2026

Fixes #7205

Since v1.12.0, AxiosError.from() sets cause as 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 — but JSON.stringify still walks all enumerable properties including cause before toJSON() gets a chance to intervene when used in contexts like console.log with certain formatters or logging libraries that don't call toJSON().

Fix: Use Object.defineProperty to set cause as non-enumerable (matching how native Error.cause behaves 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.cause non-enumerable to avoid JSON.stringify circular reference errors introduced in v1.12.0. Restores pre-1.12 behavior and aligns with native Error.cause.

  • Bug Fixes
    • Set cause via Object.defineProperty as non-enumerable (still configurable, writable).
    • Prevents JSON.stringify(axiosError) from throwing when the original error has circular refs.
    • No API changes; toJSON() unchanged. No tests added—consider a regression test for JSON.stringify with a circular cause.

Written for commit 05d1538. Summary will update on new commits.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

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

No issues found across 1 file

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AxiosError circular reference when stringifying since v1.12.0

1 participant