Issue
I'm trying to figure out an accurate way to detect axios call failure due to no internet connection.
Does axios call failure return a specific response object or specific response status code or throw a specific error details for the no internet connection scenario?
Example of what i will use the "detection of axios failure due to no internet connection" for
try {
const res = await server.get('/auth/user?', {
params: {
refreshToken: refreshToken,
userID: userID
}
}
);
if(res.user.data){
dispatch({type: LOGIN_USER_SUCCESS, payload: res.data.user});
} else {
if(res.axiosFailsDueToNoInternetConnection){
alert('no internet connection');
dispatch({type: RELOAD});
}
}
} catch (error) {
if(error.dueToNoInternetConnection){
alert('no internet connection');
dispatch({type: RELOAD});
}
}
Solution
In your catch clause you can check whether the error is caused by network error or not:
catch (error) {
if(error.toJSON().message === 'Network Error'){
alert('no internet connection');
dispatch({type: RELOAD});
}
}
Answered By - Al Ped Answer Checked By - Clifford M. (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.