Skip to content

fix: prevent undefined error codes in settle#7276

Merged
jasonsaayman merged 5 commits intoaxios:v1.xfrom
VeerShah41:v1.x
May 1, 2026
Merged

fix: prevent undefined error codes in settle#7276
jasonsaayman merged 5 commits intoaxios:v1.xfrom
VeerShah41:v1.x

Conversation

@VeerShah41
Copy link
Copy Markdown
Contributor

…atus codes

Replace array indexing logic with conditional check to ensure proper error code assignment for all HTTP status code ranges.

Previously, the error code selection used:
[ERR_BAD_REQUEST, ERR_BAD_RESPONSE][Math.floor(status / 100) - 4]

This produced undefined for status codes outside 4xx/5xx ranges (1xx, 2xx, 3xx, 6xx+), which could break error handling when users customize validateStatus to reject non-standard status codes.

Now uses:
status >= 400 && status < 500 ? ERR_BAD_REQUEST : ERR_BAD_RESPONSE

This ensures:

  • 4xx codes → ERR_BAD_REQUEST
  • All other error codes → ERR_BAD_RESPONSE

Instructions

Please read and follow the instructions before creating and submitting a pull request:

  • Create an issue explaining the feature. It could save you some effort in case we don't consider it should be included in axios.
  • If you're fixing a bug, try to commit the failing test/s and the code fixing it in different commits.
  • Ensure you're following our contributing guide.

⚠️👆 Delete the instructions before submitting the pull request 👆⚠️

Describe your pull request here.

…atus codes

Replace array indexing logic with conditional check to ensure proper error
code assignment for all HTTP status code ranges.

Previously, the error code selection used:
[ERR_BAD_REQUEST, ERR_BAD_RESPONSE][Math.floor(status / 100) - 4]

This produced undefined for status codes outside 4xx/5xx ranges (1xx, 2xx,
3xx, 6xx+), which could break error handling when users customize
validateStatus to reject non-standard status codes.

Now uses:
status >= 400 && status < 500 ? ERR_BAD_REQUEST : ERR_BAD_RESPONSE

This ensures:
- 4xx codes → ERR_BAD_REQUEST
- All other error codes → ERR_BAD_RESPONSE
@VeerShah41
Copy link
Copy Markdown
Contributor Author

@tomaash

@VeerShah41
Copy link
Copy Markdown
Contributor Author

@adimit

@nidhishgajjar

This comment was marked as spam.

1 similar comment
@nidhishgajjar

This comment was marked as spam.

@jasonsaayman
Copy link
Copy Markdown
Member

jasonsaayman commented Apr 29, 2026

Thanks @VeerShah41, the bug is real, and the diagnosis is right. The current [ERR_BAD_REQUEST, ERR_BAD_RESPONSE][Math.floor(status/100) - 4] produces undefined for any status outside 400–599, which means every 1xx and 6xx+ rejection (and any 2xx/3xx the user rejects via custom validateStatus) gets an AxiosError with no code. Doesn't crash, but error.code === 'ERR_BAD_REQUEST' checks silently miss those cases.

One semantic thing worth flagging on the fix itself. Today ERR_BAD_RESPONSE reads pretty consistently as "server returned a 5xx or the response couldn't be parsed" see http.js:468/860/890/905, fetch.js:204/307/345/390, defaults/index.js:133. After this PR, a 200 the user rejects via a custom validateStatus becomes ERR_BAD_RESPONSE alongside an actual 5xx. Defensible (the user did say it was a bad response), but the meaning broadens.

The bigger gap is in tests. There's no settled spec today grep -r ERR_BAD_REQUEST test/ comes up empty, so the bug existed precisely because nothing exercised these boundaries. A test/specs/core/settle.spec.js covering 4xx → ERR_BAD_REQUEST, 5xx → ERR_BAD_RESPONSE, and a non-4xx/5xx case via custom validateStatus asserting error.code is defined would lock this in.

One last thing, please don't ping unrelated GitHub users on PRs here. They're not maintainers of this repo, and ad-hoc pings tend to get muted rather than acted on. The maintainers do see open PRs.

@jasonsaayman jasonsaayman added priority::medium A medium priority commit::fix The PR is related to a bugfix status::changes-requested A reviewer requested changes to the PR status::needs-rebase The PR requires a rebase labels Apr 29, 2026
@jasonsaayman jasonsaayman changed the title prevent undefined error codes in settle for non-4xx/5xx st... fix: prevent undefined error codes in settle Apr 29, 2026
Cover the boundary conditions that were previously untested:
- 4xx status codes → ERR_BAD_REQUEST
- 5xx status codes → ERR_BAD_RESPONSE
- Non-4xx/5xx codes rejected via custom validateStatus → ERR_BAD_RESPONSE (defined, not undefined)

These tests would have caught the original array-indexing bug where
Math.floor(status/100) - 4 returned negative/out-of-range indices
for 1xx, 2xx, 3xx, and 6xx+ status codes, resulting in undefined error.code.
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.

@VeerShah41
Copy link
Copy Markdown
Contributor Author

Hey @jasonsaayman sorry for the unrelated pings and the spam, that was genuinely careless on my part. I didn't check if those people were actually maintainers before tagging them .Won't do that again.

I've added the tests you mentioned . I'll be upfront ,I used an LLM to help me generate them since I'm still learning how to write good test cases though on my part, I did go through each one and understand what it's checking , just wanted to be transparent about that .

The tests cover 4xx ➡️ ERR_BAD_REQUEST, 5xx ➡️ ERR_BAD_RESPONSE ,and the custom validateStatus edge cases where error.code was coming back as undefined before the fix .

Thanks for the detailed feedback it actually helped me understand the codebase a lot better .

@jasonsaayman jasonsaayman merged commit 5107ee6 into axios:v1.x May 1, 2026
23 checks passed
ModyQyW added a commit to uni-helper/uni-network that referenced this pull request May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commit::fix The PR is related to a bugfix priority::medium A medium priority status::changes-requested A reviewer requested changes to the PR status::needs-rebase The PR requires a rebase

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants