Interceptors - how to prevent intercepted messages from resolving as error
See original GitHub issueI’m trying to make an interceptor for 401 responses that result from expired token. Upon interception I want to login and retry the requests with the new token. My problem is that login is also done asynchronously, so by the time the retry happens, the original promises reject. Is there a way around that? Here’s my code:
axios.interceptors.response.use(undefined, err => {
if (err.status === 401 && err.config && !err.config.__isRetryRequest) {
refreshLogin(getRefreshToken(),
success => {
setTokens(success.access_token, success.refresh_token)
err.config.__isRetryRequest = true
err.config.headers.Authorization = 'Bearer ' + getAccessToken()
axios(err.config)
},
error => { console.log('Refresh login error: ', error) }
)
}
})
Issue Analytics
- State:
- Created 8 years ago
- Reactions:2
- Comments:33 (2 by maintainers)
Top Results From Across the Web
How to use Axios interceptors to handle API error responses
One way to solve this problem, is to handle it when you do the request in your code, so if you have an...
Read more >Chapter 18. Intercepting Messages Red Hat AMQ 7.0
Interceptors must implement the intercept() method, which returns a boolean value. If the value is true , the message packet continues onward. If...
Read more >How to prevent axios.interceptors.response.use to crash the ...
I have several forms "Login, Registration, Create a product..." For these forms, I was handling errors with useState for the front end and...
Read more >4 ways to use Axios interceptors | Khaled Garbaya
Error handling using Axios interceptors ... You can use An Axios interceptor to capture all errors and enhance them before reaching your end...
Read more >Angular: Handle HTTP Errors using Interceptors
Interceptors are a unique type of Angular service that we can implement. Interceptors allow us to intercept incoming or outgoing HTTP ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You could do something like this:
Sorry for the delay, I am just seeing this issue.
You need to make a return statement from your interceptor which will keep the Promise alive. More or less change your
refreshLogin
to return a Promise and then return that from your interceptor.Or if you can’t refactor
refreshLogin
you can wrap it in a Promise.