Full answer
The first digit tells the family.
- 2xx: success.
200 OK,201 Created,204 No Content(success with no body, so do not call.json()). - 3xx: redirects.
301and308are permanent,302and307are temporary.304 Not Modifiedanswers a conditional request and means "use your cached copy". - 4xx: the request was wrong or not allowed.
400bad request,401not authenticated,403authenticated but not allowed,404not found,409conflict,422validation failed,429too many requests. - 5xx: the server failed.
500internal error,502and504gateway problems,503unavailable (often withRetry-After).
Facts about fetch that cause bugs:
- The promise rejects only when the request cannot complete, such as a network failure, a CORS block or an abort. A
404or500is a resolved promise. Checkresponse.ok(true for 200 to 299) or the status. - Redirects are followed by default.
response.redirectedtells you that it happened. Withredirect: 'manual', a browser gives an opaque redirect response and not the real status, so Node and the browser differ here. - An error response can still have a body. Read it (
response.json()) to show a specific message, but guard against a non-JSON error page. 204and304have no body.
What to do:
- 401: the session ended. Try to refresh the token once, or send the user to sign in. Do not retry in a loop.
- 403: tell the user that they lack permission. Signing in again will not help.
- 404: show a not found state. For an API lookup, it may simply mean "no record".
- 429: wait. Use the
Retry-Afterheader (seconds or a date) and back off. - 5xx: retry idempotent requests a few times with exponential backoff and jitter. Do not retry unsafe requests without an idempotency key.
The script starts a server with six routes and calls them with a helper that turns each status into a user-level decision.
javascript
import http from 'node:http';
const routes = {
'/ok': [200, {}, '{"ok":true}'],
'/moved': [301, { Location: '/ok' }, ''],
'/login': [401, {}, '{"error":"no session"}'],
'/missing': [404, {}, '{"error":"no such course"}'],
'/busy': [429, { 'Retry-After': '2' }, '{"error":"slow down"}'],
'/broken': [500, {}, '{"error":"database down"}'],
};
const server = http.createServer((req, res) => {
const [status, headers, body] = routes[req.url];
res.writeHead(status, { 'Content-Type': 'application/json', ...headers });
res.end(body);
});
await new Promise((resolve) => server.listen(0, resolve));
const base = `http://localhost:${server.address().port}`;
async function decide(path) {
const res = await fetch(base + path); // resolves for every status
if (res.redirected) return 'followed a redirect, then ' + res.status;
if (res.ok) return 'use the data';
if (res.status === 401) return 'ask the user to sign in';
if (res.status === 429) return `wait ${res.headers.get('retry-after')}s, then retry`;
if (res.status >= 500) return 'show an error and offer a retry';
return 'show: ' + (await res.json()).error;
}
for (const path of Object.keys(routes)) console.log(path.padEnd(9), '->', await decide(path));
server.close();Run with Node 24.22.1. The output matched the text in the answer in two runs.
Key points
- 2xx success, 3xx redirect, 4xx client error, 5xx server error.
- fetch rejects only on network-level failure. Check response.ok.
- 401 is not signed in. 403 is not allowed.
- 429 and 503 may carry Retry-After. Back off.
- 204 and 304 have no body.
Common mistakes
- Assuming fetch throws on 404 or 500.
- Calling response.json() on a 204 or an HTML error page.
- Retrying 401 or 403 requests in a loop.
- Retrying unsafe requests such as payments without an idempotency key.
Follow-ups
- What is the difference between 401 and 403?
- How would you retry 503 responses safely?
- What does 304 mean for the browser?
Tips
- Give the families first, then the five codes in the question.
- Point out the fetch trap early.