Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to Check if Axios Call Fails due to No Internet Connection? #2986

Closed
KaizenTamashi opened this issue May 28, 2020 · 4 comments
Closed

How to Check if Axios Call Fails due to No Internet Connection? #2986

KaizenTamashi opened this issue May 28, 2020 · 4 comments

Comments

@KaizenTamashi
Copy link

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});
    }
}
@freakymittal
Copy link

You shouldn't rely on axios or any http library to determine the internet connectivity, cause it can't distinguish between No Internet and Server not reachable.

If you don't care about the difference between the 2 states, refer

https://github.com/axios/axios#handling-errors

If you want to check the network connectivity on browser, refer

https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/onLine

@KaizenTamashi
Copy link
Author

@mayank-mittal Thanks

@AidanHak
Copy link

If you're using Expo with React Native, there's a Network API.

@dozsolti
Copy link

Well, you kinda can't, at least not with axios, you have to use another library because:

axios timeout is response timeout, not connection timeout.
arthankamal - https://stackoverflow.com/a/54573024

But there is a workaround to wait some time and if there isn't a response it means that it will be an internet problem.
Check out here for an example: https://stackoverflow.com/a/70390385

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants