PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 14, 2022

[FIXED] How to Check if Axios Call Fails due to No Internet Connection?

 October 14, 2022     axios, networking, react-native, reactjs, server     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing