HTTP Status Code Classifier
Classify HTTP status codes by class and expose retryable, cacheable, success and error flags for client logic.
Code
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
HTTP Retry & Backoff CalculatorSimulate exponential backoff retry schedules with Full, Equal, Decorrelated, or No Jitter to inspect per-attempt wait times and cumulative latency.HTTP Status Code ExplorerBrowse and explore HTTP status codes with their meanings, categories, and common use cases. Search by code or description to understand 1xx through 5xx responses.Accept Header ParserParse an Accept header into media ranges with their quality values and specificity, sorted by preference for content negotiation debugging.Basic & Bearer Auth Header ToolEncode and decode HTTP Authorization Basic and Bearer headers locally with UTF-8 Base64 validation and automatic JWT claim and expiry inspection.
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.
Report a problem with this exampleOpens a pre-filled issue on GitHub.