Visitar URL original
fix(interceptor): handle the error in the same interceptor by Tackoil · Pull Request #6269 · axios/axios · GitHub
Skip to content

fix(interceptor): handle the error in the same interceptor - #6269

Merged
jasonsaayman merged 5 commits into
axios:v1.xfrom
Tackoil:fix/interceptorChain
Jan 8, 2026
Merged

jasonsaayman merged 5 commits into
axios:v1.xfrom
Tackoil:fix/interceptorChain

Conversation

@Tackoil

@Tackoil Tackoil commented Feb 29, 2024 •

Copy link
Copy Markdown
Contributor

fixes: #4537

When handling error in asynchronous request interceptors, onRejected always handle the error in the next interceptors. So I make a fix about this case. Detail context is in the issue.

During the fix, I found that it will cause a TypeError when an error occurred in the request interceptors, because the config is lost during the promise chain.

TypeError: Cannot read properties of undefined (reading 'cancelToken')

So we can define a new onRejected handler behavior. If users want to continue the request, they should return a default config in the onRejected function.

@Tackoil

Tackoil commented Mar 1, 2024

Copy link
Copy Markdown
Contributor Author

To fix the lost of config in promise chain, I made a change to support it.

        promise = promise
            .then(chain[i++]) // push the onResolved handler. If an error occur, it will jump to the catch function.
            .then(result => {prevResult = result ? result : prevResult}) // update the result
            .catch(chain[i++]) // push the onRejected handler
            .then(() => prevResult); // pass the result to the next interceptor

@bombillazo

Copy link
Copy Markdown

Any followup on this? Just hit this issue where my interceptor error wasn't being handled until I added a second interceptor with an error callback, very misleading behavior.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 attempts to fix issue #4537 by changing how errors are handled in interceptors - making the onRejected handler execute in the same interceptor where the error occurs, rather than in the next interceptor. The PR also addresses a TypeError that occurred when config was lost during the promise chain.

Key changes:

  • Modified asynchronous request interceptor chain execution to use .then().catch() pattern with config preservation logic
  • Modified response interceptor chain to use .then().catch() pattern instead of .then(fulfilled, rejected)
  • Added three test cases to verify error handling behavior in interceptors

Reviewed Changes

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

File Description
lib/core/Axios.js Modified promise chain logic for both request and response interceptors to handle errors in the same interceptor, including addition of config preservation mechanism
test/specs/interceptors.spec.js Added three new test cases covering error handling in request/response interceptors and config modification behavior when errors occur

Comment thread test/specs/interceptors.spec.js
Comment thread lib/core/Axios.js
Comment thread lib/core/Axios.js Outdated
Comment thread lib/core/Axios.js
Comment thread test/specs/interceptors.spec.js
Comment thread test/specs/interceptors.spec.js
Comment thread test/specs/interceptors.spec.js Outdated

@jasonsaayman jasonsaayman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@Tackoil please check the copilot feedback

@jasonsaayman jasonsaayman added the status::changes-requested A reviewer requested changes to the PR label Nov 12, 2025
@Tackoil

Tackoil commented Dec 5, 2025 •

Copy link
Copy Markdown
Contributor Author

@Tackoil please check the copilot feedback

Sorry for the delay. I'll resolve all feedbacks in next three days.

@bombillazo

Copy link
Copy Markdown

@jasonsaayman can we recheck this PR?

@jasonsaayman
jasonsaayman requested a review from Copilot January 8, 2026 18:08
This was referenced Feb 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status::changes-requested A reviewer requested changes to the PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The error handling function in request interceptors may not work as expect.

4 participants