Skip to content

AxiosError circular reference when stringifying since v1.12.0 #7205

@psurma

Description

@psurma

Since Axios v1.12.0, AxiosError objects contain circular references that cause JSON.stringify() to fail with "Converting circular structure to JSON" errors.

Root Cause

PR #6982 introduced the cause property that chains the original error:

javascript
if (error && axiosError.cause == null) {
Object.defineProperty(axiosError, 'cause', { value: error, configurable: true });
}

The original error objects often contain circular references (e.g., network errors with socket references), which are now preserved in the AxiosError, making the entire error object unstringifiable.

To Reproduce

javascript
const axios = require('axios');

// Trigger a network error
axios.get('http://nonexistent-domain.invalid')
.catch(error => {
console.log(JSON.stringify(error)); // Throws: Converting circular structure to JSON
});

Expected behavior

AxiosError objects should be serializable with JSON.stringify() for logging purposes, as they were in v1.11.0 and earlier.

Environment

  • Axios version: 1.12.0, 1.12.1, 1.12.2
  • Node.js version: Any
  • Last working version: 1.11.0

Suggested Fix

The cause property should either:

  1. Be made non-enumerable to exclude it from JSON serialization
  2. Have circular references cleaned before assignment
  3. Implement a custom toJSON() method that handles circular references

Impact

This breaks logging systems that stringify error objects, causing application crashes in production environments.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions