fix(core): serialize custom properties in AxiosError.toJSON (#6511)#7540
fix(core): serialize custom properties in AxiosError.toJSON (#6511)#7540sudip-kumar-prasad wants to merge 2 commits intoaxios:v1.xfrom
Conversation
There was a problem hiding this comment.
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 (excludingrequest,response, andcause). - 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. |
There was a problem hiding this comment.
1 issue found across 2 files
Confidence score: 3/5
- There is a concrete regression risk in
lib/core/AxiosError.js: spreadingcustomPropslast can overwrite the sanitizedconfig, 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-aiwith guidance or docs links (includingllms.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.
27ee1cf to
9d7dd10
Compare
|
Thanks for the review! Good catch regarding the merge order and potential serialization risks. I’ve updated the implementation to spread custom enumerable properties before the standard AxiosError fields. This ensures that core fields like Additionally, With this order, we retain custom metadata (e.g., trace IDs) while guaranteeing that no unsafe values override the normalized error shape. Let me know if you'd like me to add tests specifically covering circular reference edge cases as well. |
Summary
Fixes #6511
When using
AxiosError.from(...), it's possible to passcustomPropswhich are merged onto the error instance. However, if the error was serialized viaAxiosError.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, andcauseare intentionally excluded to prevent circular reference errors or serializing raw Error instances.Important Changes
AxiosError.prototype.toJSONto dynamically capture custom properties.Testing
npm run test:node) pass successfully.npm run test:package) pass successfully.Summary by cubic
Serialize custom properties in
AxiosError.toJSON()so fields fromAxiosError.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
toJSON()to include own enumerable properties and excluderequest,response, andcause.message,name, etc.) to prevent overrides.AxiosError.from(...)were dropped, hiding context like request/trace IDs in logs.Errorserialization.Docs
No docs changes.
Testing
test/specs/core/AxiosError.spec.jsto assert custom props appear intoJSON()output.Written for commit fceca01. Summary will update on new commits.