Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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!!")   
    });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading