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?