fix(interceptor): handle the error in the same interceptor#6269
fix(interceptor): handle the error in the same interceptor#6269jasonsaayman merged 5 commits intoaxios:v1.xfrom
Conversation
|
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 |
|
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. |
There was a problem hiding this comment.
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 |
jasonsaayman
left a comment
There was a problem hiding this comment.
@Tackoil please check the copilot feedback
Sorry for the delay. I'll resolve all feedbacks in next three days. |
|
@jasonsaayman can we recheck this PR? |
fixes: #4537
When handling error in asynchronous request interceptors,
onRejectedalways 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.
So we can define a new
onRejectedhandler behavior. If users want to continue the request, they should return a default config in theonRejectedfunction.