#1862·node-fetch

Not able to handle ECONNREFUSED error

Author: Amark19Created Feb 24, 2026Updated Jun 25, 2026
Labelsbug

I am handling fetch call in catch block using following code

javascript
catch (err) {
    if (err.type === "system" && err.message.endsWith("reason: Socket closed")) {
      return {
        success: false,
        status: 400,
        errorMessage: "Not able to reach Hosted service",
      };
    }
    if (err.code === "ENOTFOUND") {     return {
        success: false,
        status: 400,
        errorMessage: "ENOTFOUND error. ",
      };
    }
    if (err.code === "ECONNRESET") {
      return {
        success: false,
        status: 502,
        errorMessage: "ECONNRESET error. Connection reset by server.",
      };
    }
    if (err.code === "ECONNREFUSED") {
      return {
        success: false,
        status: 503,
        errorMessage: "ECONNREFUSED error. Please check if the server is reachable.",
      };
    }

but it never reaches inside err.code === "ECONNREFUSED" block, as per this err.code should have this value but it does not, how we are supposed to handle this ?

I am getting following error from this line ERROR: FetchError: request to <endpoint> failed, reason: connect ECONNREFUSED <ip_address>, stack: FetchError: request to <endpoint> failed, reason: connect ECONNREFUSED <ip_address>

Expected behavior

Should have way to handle ECONNREFUSED error.

Your Environment

software version
node-fetch
node 18.12.1
npm
Operating System mac, linux (its in production env too )

Additional context