Redux docs suggest to have 3 different actions per AJAX request. For example for Login they would be:
LOGIN_REUQEST
LOGIN_FAILURE
LOGIN_SUCCESS
But I have problem with catching all errors that can be thrown by fetch
.
In redux docs I found this example:
return fetch(`https://www.reddit.com/r/${subreddit}.json`)
.then(
response => response.json(),
// Do not use catch, because that will also catch
// any errors in the dispatch and resulting render,
// causing an loop of 'Unexpected batch number' errors.
// https://github.com/facebook/react/issues/6895
error => console.log('An error occured.', error)
)
.then(json =>
// We can dispatch many times!
// Here, we update the app state with the results of the API call.
dispatch(receivePosts(subreddit, json))
)
}
But I see several problems in it:
- It doesn’t use catch, so it wont catch any problems with AJAX request on user side (for example no internet)
- It doesn’t handle responses with status != 200.
I ended up this this code, but still it won’t handle case #1 (because of React issue – see comment in above code):
fetch('/api/auth/signin', {...}),
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json, text/plain, */*',
}
})
.then ( response =>
response.json().then(json => {
if (response.ok) {
dispatch(loginSuccess(json))
} else {
dispatch(loginFailure(json.errMsg))
}
})
)
Can you please give me any example which handles all possible errors with AJAX request using fetch. Many tutorial and open-source projects just ignoring it.
2
Answers
You don’t need
.catch()
to handle offline errors. The second argument to.then()
, the error callback, will catch offline (no internet connection) errors.And, handling responses that are not 200 is straightforward:
This code structure maybe able to solve your problem. I have recorded all the responses and left catch for network request failures. Comment if you don’t understand any of it.
Hope it helps.