Skip to the page

Networking & HTTP · Write code

fetch pitfalls and API error handling

The interviewer asks

This helper looks right but the caller always gets undefined. Find the bug. Then: how should a UI handle HTTP errors from fetch?

function getDogs() {
  fetch('/api/dogs').then((r) => r.json());
}
getDogs().then(render); // TypeError

Then

Follow-ups the interviewer asks next

  1. fetch resolves on 404/500, so check res.ok.
  2. Timeouts and abort.
  3. JSON parse errors and empty 204 bodies.
  4. Centralised API client with interceptors (or RTK Query baseQuery).

The answer

What a strong answer covers

  • Missing return, so the function returns undefined
  • fetch rejects only on network failure or abort
  • Typed error objects; user-facing vs logged errors

The red pen

Common traps

Avoid:assuming fetch throws on 4xx.

Practise this out loud

Say your answer out loud to an AI interviewer that follows up, and get a score for it.

Practise by voice

Practise questions like this out loud

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview