Not able to handle ECONNREFUSED error · Issue #1862 · node-fetch/node-fetch · GitHub
Skip to content

Not able to handle ECONNREFUSED error #1862

Description

@Amark19

I am handling fetch call in catch block using following code

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions