axios: Axios not failing on chrome "canceled request".
Summary
Google Chrome may cancel requests sometimes. Axios does not seem to throw an error when this happens.
Context
- axios version: 0.17.1
- Environment: Electron / Chrome / Chromium
For more details about what can cause this situation, here is a stackoverflow link "What does status=canceled for a resource mean in Chrome Developer Tools? ". This is also valid in Chromium (Electron apps).
I was expecting axios to fail and throw an error when this happens. But this code does not work:
refreshData () {
this.isFetching = true
this.fetchingError = false
return this.fetchData()
.then(() => {
this.set('lastUpdated', new Date())
})
.catch((err) => {
this.set('fetchingError', true)
})
.finally(() => {
this.set('isFetching', false)
})
}
Nor catch
, nor finally
run.
Is this an expected behaviour?
(In case you don’t know about: finally)
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 24 (1 by maintainers)
it helped me -> event.preventDefault();
Found the solution guys! @AoDev
Axios documentation has a timeout of 1000 like:
axios.create({ ..., timeout: 1000, ... })
I increased the timeout to 5000. I think Axios documentation needs to be updated to change the timeout to a higher number to prevent newcomers to go crazy trying to figure out what’s going on.
I hope this solutions helps!
A workaround is to use an interceptor with the cancel token.
The example below will add a 30 second timeout on the call. I’ve tested this on Chrome (and FF and Edge) and it recovers correctly, by rejecting the promise, after the PC wakes up.
I was seeing the same thing with requests chrome cancelled when the machine went into sleep - https://github.com/axios/axios/pull/1399 (which was merged to master) ensures that an error is thrown when the request is aborted so you should start catching these once it is out (not sure when the next release is planned)