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?
We already have
getErrorMessagefor safely reading a message from an unknown thrown value. A matchinggetErrorCodecould be useful for errors from Node, HTTP clients, SDKs, and custom domain code that expose acodefield.Proposed shape:
Proposed behavior:
error.codewhen it is a non-empty string.undefinedwhen there is no usable code.name,status, or a generic value.Example:
Open question: should numeric codes be stringified, returned as-is, or ignored?
Any objections to adding this?