Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Axios differentiate between "no network" and "network disconnected"

Is it possible for Axios to differentiate between the following:

  • A request which fails because the client has no network connection at the point when the request was made - (ERR_CONNECTION_REFUSED).
  • A request which fails because network connectivity is lost after the request was made but before the response was received - (ERR_INTERNET_DISCONNECTED).

It seems that in both cases, all we get back is a rather unhelpful "Network Error" message...

The Axios documentation (https://github.com/axios/axios#handling-errors) suggests that in the latter case, error.request should be populated, but I have been unable to replicate any scenario where error.request exists. In both of the above scenarios, I get an error object looking like this:

{
  config: { ... },
  response: undefined,
  message: "Network Error",
  stack: "Error: Network Error at createError (http://localhost:3000/static/js/bundle.js:1636:15) at XMLHttpRequest.handleError (http://localhost:3000/static/js/bundle.js:1170:14)"
}
like image 376
Andy Neale Avatar asked Jan 09 '18 11:01

Andy Neale


1 Answers

I don't know exacly if you can get ERR_CONNECTION_REFUSED but ERR_INTERNET_DISCONNECTED you can get use native web Network Information API,take a look on snippet bellow:

var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
console.log(connection);

When connection.type: "none" mean that you are ERR_INTERNET_DISCONNECTED, maybe you can explore this solution.

you can read complete documention here, cya

like image 113
rflmyk Avatar answered Oct 19 '22 17:10

rflmyk