fix(dispatchRequest): attach response to AxiosError on JSON parse failure#10724
Merged
jasonsaayman merged 3 commits intoaxios:v1.xfrom Apr 25, 2026
Merged
fix(dispatchRequest): attach response to AxiosError on JSON parse failure#10724jasonsaayman merged 3 commits intoaxios:v1.xfrom
jasonsaayman merged 3 commits intoaxios:v1.xfrom
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #7224.
When
JSON.parsefails in strict mode (responseType: 'json'orsilentJSONParsing: false), the thrownAxiosErrorhaderror.responseanderror.statusboth asundefined, making it impossible for callers to inspect the HTTP response status without re-examining the raw response.Root cause
transformResponseruns insidetransformData.call(config, ...)wherethisis the config object.AxiosError.fromusesthis.responseto populate the error — butconfig.responseis never set indispatchRequest, so it is alwaysundefined.Fix
In
dispatchRequest, temporarily assignconfig.response = responsebefore callingtransformData, then clean it up in afinallyblock. This makesthis.responseavailable insidetransformResponsewhen building theAxiosError.Changes
lib/core/dispatchRequest.js: set/cleanconfig.responsearoundtransformDatacalls for both resolution and rejection pathstests/unit/transformResponse.test.js: 3 new tests — ERR_BAD_RESPONSE code, response/status attached, config.response cleaned up afterSummary by cubic
Ensures
AxiosErrorincludeserror.responseanderror.statuson strict JSON parse failures by temporarily exposing the response during transform. Restores consistent error behavior forresponseType: 'json'.Description
lib/core/dispatchRequest.js, setconfig.response = responsebeforetransformData.call(config, config.transformResponse, ...)on both resolve and reject paths, then delete it infinally.AxiosError.fromreadsthis.responseinsidetransformResponse; without it, parse failures lacked response details.Docs
/docs/about consistentAxiosErrorfields on JSON parse errors.Testing
tests/unit/core/dispatchRequest.test.js:error.responseanderror.statusare set on JSON parse failure (both paths).config.responseis cleaned up after throws and after success.tests/unit/transformResponse.test.js:ERR_BAD_RESPONSEand attaches the originalresponse.Semantic version impact
Written for commit cc0d8fc. Summary will update on new commits.