← Back to Frontend, page 3

frontend-0056 · Easy fresher

Which HTTP status codes must a front-end developer know, and how should fetch code react to 301, 401, 404, 429 and 500?

Status codes come in families: 2xx success, 3xx redirects, 4xx client errors and 5xx server errors. fetch only rejects on a network failure, so code must check response.ok and the status. A 401 should lead to sign in, a 404 to a not found message, a 429 to a wait based on Retry-After, and a 500 to a retry or an error message.

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. 301 and 308 are permanent, 302 and 307 are temporary. 304 Not Modified answers a conditional request and means "use your cached copy".
  • 4xx: the request was wrong or not allowed. 400 bad request, 401 not authenticated, 403 authenticated but not allowed, 404 not found, 409 conflict, 422 validation failed, 429 too many requests.
  • 5xx: the server failed. 500 internal error, 502 and 504 gateway problems, 503 unavailable (often with Retry-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 404 or 500 is a resolved promise. Check response.ok (true for 200 to 299) or the status.
  • Redirects are followed by default. response.redirected tells you that it happened. With redirect: '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.
  • 204 and 304 have 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-After header (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.