Skip to content

fix(dispatchRequest): attach response to AxiosError on JSON parse failure#10724

Merged
jasonsaayman merged 3 commits intoaxios:v1.xfrom
AKIB473:fix/json-parse-error-response
Apr 25, 2026
Merged

fix(dispatchRequest): attach response to AxiosError on JSON parse failure#10724
jasonsaayman merged 3 commits intoaxios:v1.xfrom
AKIB473:fix/json-parse-error-response

Conversation

@AKIB473
Copy link
Copy Markdown
Contributor

@AKIB473 AKIB473 commented Apr 15, 2026

Summary

Fixes #7224.

When JSON.parse fails in strict mode (responseType: 'json' or silentJSONParsing: false), the thrown AxiosError had error.response and error.status both as undefined, making it impossible for callers to inspect the HTTP response status without re-examining the raw response.

Root cause

transformResponse runs inside transformData.call(config, ...) where this is the config object. AxiosError.from uses this.response to populate the error — but config.response is never set in dispatchRequest, so it is always undefined.

Fix

In dispatchRequest, temporarily assign config.response = response before calling transformData, then clean it up in a finally block. This makes this.response available inside transformResponse when building the AxiosError.

Changes

  • lib/core/dispatchRequest.js: set/clean config.response around transformData calls for both resolution and rejection paths
  • tests/unit/transformResponse.test.js: 3 new tests — ERR_BAD_RESPONSE code, response/status attached, config.response cleaned up after

Summary by cubic

Ensures AxiosError includes error.response and error.status on strict JSON parse failures by temporarily exposing the response during transform. Restores consistent error behavior for responseType: 'json'.

Description

  • Summary of changes
    • In lib/core/dispatchRequest.js, set config.response = response before transformData.call(config, config.transformResponse, ...) on both resolve and reject paths, then delete it in finally.
  • Reasoning
    • AxiosError.from reads this.response inside transformResponse; without it, parse failures lacked response details.
  • Additional context
    • Covers adapter resolve and reject paths; expands malformed JSON tests.

Docs

  • No docs changes needed. If desired, add a short note in /docs/ about consistent AxiosError fields on JSON parse errors.

Testing

  • Added tests/unit/core/dispatchRequest.test.js:
    • Asserts error.response and error.status are set on JSON parse failure (both paths).
    • Verifies config.response is cleaned up after throws and after success.
  • Updated tests/unit/transformResponse.test.js:
    • Asserts ERR_BAD_RESPONSE and attaches the original response.

Semantic version impact

  • Patch: bug fix with no API changes.

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

…lure

When JSON.parse fails in strict mode (responseType: 'json' or
silentJSONParsing: false), the thrown AxiosError was missing both
error.response and error.status because transformResponse has no
direct access to the response object — only the config is available
as `this` inside transformData.

Root cause: transformData is called as transformData.call(config, ...)
and AxiosError.from uses `this.response` to populate the error's
response field, but config.response was never set.

Fix: in dispatchRequest, temporarily assign the response object to
config.response before invoking transformData, then clean it up in a
finally block to avoid permanently polluting the config object.

This ensures error.response and error.status are available when a
request with responseType='json' receives a malformed JSON body,
making the error consistent with all other AxiosError instances.

Fixes axios#7224
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 2 files

Confidence score: 5/5

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

@jasonsaayman jasonsaayman merged commit bd3816b into axios:v1.x Apr 25, 2026
23 checks passed
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.

Bug: Axios throws unclear error when JSON.parse fails on malformed responses

2 participants