Classify Errors Into Retryable And Fatal
An error classifier that maps thrown errors to retryable or fatal decisions with an explicit reason, so retry policy never guesses from message text.
Code
export type ErrorKind = "retryable" | "fatal" | "unknown";
export interface ErrorDecision {
readonly kind: ErrorKind;
readonly retryable: boolean;
readonly reason: string;
}
const RETRYABLE_CODES = new Set(["ECONNRESET", "ETIMEDOUT", "EAI_AGAIN", "EPIPE"]);
const FATAL_CODES = new Set(["EACCES", "ENOENT", "EINVAL"]);
function statusOf(error: unknown): number | null {
if (!error || typeof error !== "object") return null;
const record = error as Record<string, unknown>;
const status = record.status ?? record.statusCode;
return typeof status === "number" ? status : null;
}
function codeOf(error: unknown): string | null {
if (!error || typeof error !== "object") return null;
const record = error as Record<string, unknown>;
return typeof record.code === "string" ? record.code : null;
}
export function classifyError(error: unknown): ErrorDecision {
const code = codeOf(error);
if (code && RETRYABLE_CODES.has(code)) {
return { kind: "retryable", retryable: true, reason: `transient system error code ${code}` };
}
if (code && FATAL_CODES.has(code)) {
return { kind: "fatal", retryable: false, reason: `local error code ${code} will not fix itself` };
}
const status = statusOf(error);
if (status !== null) {
if (status >= 500) {
return { kind: "retryable", retryable: true, reason: `server status ${status} is transient` };
}
if (status >= 400) {
return { kind: "fatal", retryable: false, reason: `client status ${status} will fail again` };
}
}
return {
kind: "unknown",
retryable: false,
reason: "no recognised code or status; defaulting to non-retryable",
};
}Retrying every error wastes budget and amplifies outages, because a 400 and a 503 look identical to a catch block.
Use case: engineers writing retry logic that must not retry non-transient failures
Inputs / outputs
- Input: an unknown thrown value plus an optional HTTP status extractor
- Output: a decision object with kind (retryable/fatal/unknown), reason and whether it is safe to retry
Error conditions
- thrown value is not an Error
- status missing or not a number
- unknown error type (treated as unknown, not retryable)
Approach
match on stable markers (error code, name, HTTP status class) instead of message substrings; unknown errors are classified unknown and treated as non-retryable by default.
Implementation
export type ErrorKind = "retryable" | "fatal" | "unknown";
export interface ErrorDecision {
readonly kind: ErrorKind;
readonly retryable: boolean;
readonly reason: string;
}
const RETRYABLE_CODES = new Set(["ECONNRESET", "ETIMEDOUT", "EAI_AGAIN", "EPIPE"]);
const FATAL_CODES = new Set(["EACCES", "ENOENT", "EINVAL"]);
function statusOf(error: unknown): number | null {
if (!error || typeof error !== "object") return null;
const record = error as Record<string, unknown>;
const status = record.status ?? record.statusCode;
return typeof status === "number" ? status : null;
}
function codeOf(error: unknown): string | null {
if (!error || typeof error !== "object") return null;
const record = error as Record<string, unknown>;
return typeof record.code === "string" ? record.code : null;
}
export function classifyError(error: unknown): ErrorDecision {
const code = codeOf(error);
if (code && RETRYABLE_CODES.has(code)) {
return { kind: "retryable", retryable: true, reason: `transient system error code ${code}` };
}
if (code && FATAL_CODES.has(code)) {
return { kind: "fatal", retryable: false, reason: `local error code ${code} will not fix itself` };
}
const status = statusOf(error);
if (status !== null) {
if (status >= 500) {
return { kind: "retryable", retryable: true, reason: `server status ${status} is transient` };
}
if (status >= 400) {
return { kind: "fatal", retryable: false, reason: `client status ${status} will fail again` };
}
}
return {
kind: "unknown",
retryable: false,
reason: "no recognised code or status; defaulting to non-retryable",
};
}
Verification tests
import { test } from "node:test";
import assert from "node:assert/strict";
import { classifyError } from "./example.ts";
test("classifies 5xx as retryable", () => {
const decision = classifyError(Object.assign(new Error("unavailable"), { status: 503 }));
assert.equal(decision.kind, "retryable");
assert.equal(decision.retryable, true);
assert.match(decision.reason, /503/);
});
test("classifies 4xx as fatal", () => {
const decision = classifyError(Object.assign(new Error("bad request"), { status: 400 }));
assert.equal(decision.kind, "fatal");
assert.equal(decision.retryable, false);
});
test("classifies transient socket codes as retryable", () => {
const decision = classifyError(Object.assign(new Error("reset"), { code: "ECONNRESET" }));
assert.equal(decision.retryable, true);
});
test("classifies local file errors as fatal", () => {
const decision = classifyError(Object.assign(new Error("denied"), { code: "EACCES" }));
assert.equal(decision.kind, "fatal");
});
test("unknown errors are not retried", () => {
const decision = classifyError(new Error("something odd"));
assert.equal(decision.kind, "unknown");
assert.equal(decision.retryable, false);
});
test("non-error values are handled", () => {
const decision = classifyError("just a string");
assert.equal(decision.retryable, false);
});
Dependencies and running
No external dependencies (standard library only).
Runtime: node >= 18 Entry point: node –experimental-strip-types example.ts
Limitations and error handling
no network classification; only structural markers are inspected.
Verification boundary
execution tests ran offline in an isolated temp dir with a sanitized environment; no network, no writes outside the sandbox
Interactive Tools for This Topic
Report a problem with this exampleOpens a pre-filled issue on GitHub.