Skip to content

fix(core): serialize custom properties in AxiosError.toJSON (#6511)#7540

Open
sudip-kumar-prasad wants to merge 2 commits intoaxios:v1.xfrom
sudip-kumar-prasad:fix/axios-error-custom-props-serialization
Open

fix(core): serialize custom properties in AxiosError.toJSON (#6511)#7540
sudip-kumar-prasad wants to merge 2 commits intoaxios:v1.xfrom
sudip-kumar-prasad:fix/axios-error-custom-props-serialization

Conversation

@sudip-kumar-prasad
Copy link

@sudip-kumar-prasad sudip-kumar-prasad commented Mar 21, 2026

Summary

Fixes #6511

When using AxiosError.from(...), it's possible to pass customProps which are merged onto the error instance. However, if the error was serialized via AxiosError.toJSON(), those custom properties were completely omitted from the output. This affected users attempting to log AxiosError objects using JSON-based loggers like Pino.

This PR updates AxiosError.toJSON() to spread any enumerable own properties of the error instance into the resulting JSON object. request, response, and cause are intentionally excluded to prevent circular reference errors or serializing raw Error instances.

Important Changes

  • Modified AxiosError.prototype.toJSON to dynamically capture custom properties.
  • Added a regression test in AxiosError.spec.js to ensure custom properties successfully serialize.

Testing

  • Verified unit tests (npm run test:node) pass successfully.
  • Verified package typing and build tests (npm run test:package) pass successfully.

Summary by cubic

Serialize custom properties in AxiosError.toJSON() so fields from AxiosError.from() are kept in JSON and show up in loggers. Spread custom props before standard fields so canonical error fields always win. Fixes #6511.

Description

  • Summary of changes
    • Updated toJSON() to include own enumerable properties and exclude request, response, and cause.
    • Spread custom props first, then standard fields (message, name, etc.) to prevent overrides.
  • Reasoning
    • Custom props from AxiosError.from(...) were dropped, hiding context like request/trace IDs in logs.
  • Additional context
    • Exclusions avoid circular references and raw Error serialization.

Docs

No docs changes.

Testing

  • Added unit test in test/specs/core/AxiosError.spec.js to assert custom props appear in toJSON() output.
  • Existing tests pass.

Written for commit fceca01. Summary will update on new commits.

Copilot AI review requested due to automatic review settings March 21, 2026 15:17
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses #6511 by ensuring custom properties attached via AxiosError.from(..., customProps) are included when serializing an AxiosError through toJSON(), improving compatibility with JSON-based loggers.

Changes:

  • Updated AxiosError.toJSON() to include enumerable own properties from the error instance (excluding request, response, and cause).
  • Added a regression test verifying custom properties are present in the JSON output.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
lib/core/AxiosError.js Extends toJSON() to include instance-owned enumerable properties as “custom props” while excluding certain non-serializable fields.
test/specs/core/AxiosError.spec.js Adds a unit test to assert customProps passed to AxiosError.from appear in toJSON() output.

Copy link
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.

1 issue found across 2 files

Confidence score: 3/5

  • There is a concrete regression risk in lib/core/AxiosError.js: spreading customProps last can overwrite the sanitized config, which may bring back circular JSON serialization failures.
  • Given the high severity/confidence (7/10, 9/10) and direct runtime impact, this is more than a minor cleanup issue and raises moderate merge risk.
  • Pay close attention to lib/core/AxiosError.js - property merge order may undo sanitization and trigger serialization errors.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="lib/core/AxiosError.js">

<violation number="1" location="lib/core/AxiosError.js:67">
P1: Spreading `customProps` last overrides sanitized `config` and can reintroduce circular JSON serialization failures.</violation>
</file>

Since this is your first cubic review, here's how it works:

  • cubic automatically reviews your code and comments on bugs and improvements
  • Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
  • Add one-off context when rerunning by tagging @cubic-dev-ai with guidance or docs links (including llms.txt)
  • Ask questions if you need clarification on any suggestion

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@sudip-kumar-prasad sudip-kumar-prasad force-pushed the fix/axios-error-custom-props-serialization branch from 27ee1cf to 9d7dd10 Compare March 21, 2026 15:20
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.

Serializing AxiosError customProps

2 participants