How to differentiate the http error with other types of run time error in Axios react?

294 Views Asked by At

I am using the Axios in react app to send the HTTP request. but how to differentiate for the runtime error or the HTTP error in the catch block.

 axios.get(getCertUrl)
                .then(async (response) => {
                   // operation might result in run time error. 
                })
                .catch(async (error) => {
                    // if (only error related to http) {
                        
                    } else { 
                      // other error related not related to http eg, null refrence 
                    }
                });
1

There are 1 best solutions below

0
Ali Raza On

You can easily differentiate between HTTP errors and other run-time errors.HTTP error codes are present in error.response.status and for others, I think you can log them and check their type. Here is a reference to how to get HTTP errors.axios HTTP errors