4

When using the JavaScript fetch API, the browser's console logs an error of ERR_CONNECTION_REFUSED, but the catch block of fetch returns a message of TypeError Failed to fetch when a server is offline.

Example

The following minimal fetch highlights the two error messages:

fetch("https://localhost/get-something", {
    method: "POST"
}).then(resp => {
    console.log(resp)
}).catch(err => {
    console.log("ERR:", err, err.name, err.message)
})

When running the fetch, the browser's console will have two errors:

POST https://localhost/get-something net::ERR_CONNECTION_REFUSED

And

ERR: TypeError: Failed to fetch
// err.message will be "failed to fetch"

The generic failed to fetch message from the catch block is returned in instances other than a server being down. I'm more interested in the browser's console error of ERR_CONNECTION_REFUSED.

Question

Is there a way to capture the ERR_CONNECTION_REFUSED value in a failed fetch call?

Metro Smurf
  • 37,266
  • 20
  • 108
  • 140
  • Duplicate: https://stackoverflow.com/questions/28556398/how-to-catch-neterr-connection-refused – Quentin Aug 19 '22 at 13:51
  • @Quentin not sure why that question is a dup. 1) It is using `XMLHttpRequest`; I'm using `fetch`. 2) No answer provided captures the `ERR_CONNECTION_REFUSED`. 3) I'm already catching the error, but is is the "failed to fetch" error. Do you have an ideas on how to captures the connection refused error? – Metro Smurf Aug 19 '22 at 14:28
  • From the Mozilla Developer Network: The Promise returned from fetch() won’t reject on HTTP error status even if the response is an HTTP 404 or 500. Instead, it will resolve normally (with ok status set to false), and it will only reject on network failure or if anything prevented the request from completing. – Henry Xiloj Herrera Aug 19 '22 at 15:13
  • do not call https in localhost! is obvious you don't get results! is insecure to call then with exceptions https (if you resolve the issue by force passing https on localhost) –  Aug 24 '22 at 15:50
  • @Constantin - doesn't matter if you use `https` or `http`; same result. – Metro Smurf Aug 24 '22 at 17:13
  • use this experiment: https://stackoverflow.com/a/73475945/5781320 –  Aug 24 '22 at 17:19
  • There is no separate error response for ERR_CONNECTION_REFUSED, you have to handle in generic error response. Thanks! – Vivek Aug 25 '22 at 07:44
  • does this answer your question: https://stackoverflow.com/a/43061415/14738189 "if you host the websocket together with html, you can start a websocket at the beginning of the page. This ws must connect successfully since you can get the html. And listen to its close event to know the time when the server is not available. Or just build a server to check if ws server available on html server" – Ethicist Aug 26 '22 at 12:47

1 Answers1

0

Unfortunately this is not possible as this kind of message is printed in the console from Chrome itself. Suppressing these types of messages has been debated for years, but the consensus seems to be that these kinds of messages are desirable.

you can only hide them from your client using the filter check this: https://umaar.com/dev-tips/79-hide-network-console/