HTTP Status Code Classifier

Classify HTTP status codes by class and expose retryable, cacheable, success and error flags for client logic.

JavascriptResilience

Code

javascriptsnippet
30 lines
const RETRYABLE = new Set([408, 425, 429, 500, 502, 503, 504]);
const CACHEABLE = new Set([200, 203, 204, 206, 300, 301, 308, 404, 410]);

export function classifyStatus(status) {
  if (!Number.isInteger(status) || status < 100 || status > 599) {
    throw new RangeError("status must be a three-digit HTTP status code");
  }
  const group = Math.floor(status / 100);
  const labels = {
    1: "informational",
    2: "success",
    3: "redirect",
    4: "client-error",
    5: "server-error",
  };
  return {
    status,
    group,
    label: labels[group],
    success: group === 2,
    redirect: group === 3,
    error: group >= 4,
    retryable: RETRYABLE.has(status),
    cacheable: CACHEABLE.has(status),
  };
}

export function isRetryableStatus(status) {
  return classifyStatus(status).retryable;
}

What the code does

Count the functions classifyStatus and isRetryableStatus as the entire surface area of HTTP Status Code Classifier.

The failure path is explicit — RangeError is raised when the guard condition holds.

The result comes back as an object carrying status, group.

Applying it

Entry signature: export function classifyStatus(status)

classifyStatus is what another module would consume; nothing else here is load-bearing for callers.

The call returns status, group — destructure what you need at the call site.

Statements that matter

const CACHEABLE = new Set([200, 203, 204, 206, 300, 301, 308, 404, 410]);
throw new RangeError("status must be a three-digit HTTP status code");
const RETRYABLE = new Set([408, 425, 429, 500, 502, 503, 504]);

Interactive Tools for This Topic

FAQ

Does `classifyStatus` modify its input?
No. Nothing passed in is written to, which makes the call safe on shared data.
How are precondition violations signaled?
It throws `RangeError` before any real work runs — handle it at the call site or let it propagate deliberately.
Is anything beyond the standard library required?
Nothing beyond the standard library — the snippet is dependency-free. The file defines `classifyStatus`, `isRetryableStatus` at load time.