Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

106
Vistas
catch fetch ERR_CONNECTION_REFUSED

I must be getting old google searches because I can't find any threads on how to do this if you are using javascript's fetch not the old XMLHttpRequest

I am trying to test whether or not my VM is online. The rest of the VM is locked down but I left an open endpoint for testing.

I tried using the status to check if the server is up but gave me a different error than a response error:

fetch("https://rockosmodernserver.westus2.cloudapp.azure.com/ ", {
            method: "GET",
            }).then(response => response)
            .then(data => {
                if (data.status == 200){
                    console.log("server is up")
                }
                else{
                 console.log("server is down!!")   
                }
            })

It works if the server is up but if the server is down I get:

VM739:1 GET https://rockosmodernserver.westus2.cloudapp.azure.com/ net::ERR_CONNECTION_REFUSED

when I tried googling this I got solutions for XMLHttpRequest but not for the fetch module.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

If the server doesn't respond, fetch takes it as connection failure and handles it in the catch() block. The fetch only executes then() blocks when the connection is successful. If the connection was not successful, the catch() block will be executed.

fetch('something')
 .then( response => {})
 .catch(error => {
   // handle error here
 })

Checking that fetch was successful

about 4 years ago · Juan Pablo Isaza Denunciar

0

You looking for catch block. In the catch block you can fetch the errors. Imn the example below you have access to the error object.

fetch("https://rockosmodernserver.westus2.cloudapp.azure.com/ ", {
    method: "GET",
    }).then(response => response)
    .then(data => {
        console.log("server is up")
    })
    .catch((error) => {
      console.error('Error:', error);
      console.log("server is down!!")   
    });

about 4 years ago · Juan Pablo Isaza Denunciar

0

If a server is not answering, he can not give you a response. Which means there is no status code.

Update: A fetch() promise will reject with a TypeError when a network error is encountered or CORS is misconfigured on the server-side, although this usually means permission issues or similar — a 404 does not constitute a network error, for example. An accurate check for a successful fetch() would include checking that the promise resolved, then checking that the Response.ok property has a value of true. The code would look something like this:

  fetch('flowers.jpg')
  .then(response => {
    if (!response.ok) {
      throw new Error('Network response was not OK');
    }
    return response.blob();
  })
  .then(myBlob => {
    myImage.src = URL.createObjectURL(myBlob);
  })
  .catch(error => {
    console.error('There has been a problem with your fetch operation:', error);
  });

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#checking_that_the_fetch_was_successful

First edit: This solution could be helpful, by using a timeout and track that timeout has happened. https://stackoverflow.com/a/50101022/13111978

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda