Can Axios distinguishes between “no network” and “network disconnected”,

Is it possible for Axios to distinguish between the following:

  • A request that is not executed because the client does not have a network connection at the time the request was made - (ERR_CONNECTION_REFUSED).
  • A request that is not executed due to loss of network connection after the request is completed, but before it is received, is (ERR_INTERNET_DISCONNECTED).

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

The Axios documentation ( https://github.com/axios/axios#handling-errors ) suggests that in the latter case it should be populated error.request, but I could not reproduce any script in which it exists error.request. In both of the above scenarios, I get an error object similar to 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)"
}
+9
source share
1 answer

I don’t know for sure if you can get ERR_CONNECTION_REFUSED, but you can use ERR_INTERNET_DISCONNECTED’s own web information network API, take a look at the fragment below:

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

When it connection.type: "none"means that you ERR_INTERNET_DISCONNECTED, perhaps you can research this solution.

, cya

+1

Source: https://habr.com/ru/post/1691921/


All Articles