Skip to content

Consider adding getErrorCode #471

Description

@aleclarson

We already have getErrorMessage for safely reading a message from an unknown thrown value. A matching getErrorCode could be useful for errors from Node, HTTP clients, SDKs, and custom domain code that expose a code field.

Proposed shape:

getErrorCode(error: unknown): string | undefined

Proposed behavior:

  • Return error.code when it is a non-empty string.
  • Return undefined when there is no usable code.
  • Do not fall back to name, status, or a generic value.

Example:

try {
  await sendRequest()
} catch (error) {
  const code = getErrorCode(error)

  if (code === "ETIMEDOUT") {
    // handle timeout
  }
}

Open question: should numeric codes be stringified, returned as-is, or ignored?

Any objections to adding this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions