API error handling
easy 24 minGood error handling tells the caller what went wrong, why, and what to do next, in one format for the whole API.
See it run
Follow failures from the place they happen to the answer, and from the answer to the client's next step.
Operations
Status explorer
Ten status codes with one example each. 4xx: the request must change. 5xx: we failed.
Step 1 of 10
400 Bad Request
| status | name | example |
|---|---|---|
| 400 | Bad Request | missing field email |
| 401 | Unauthorized | expired token |
| 403 | Forbidden | cannot delete admins |
| 404 | Not Found | product id unknown |
| 409 | Conflict | email registered |
| 422 | Unprocessable | price not positive |
| 429 | Too Many Requests | retry after 60 s |
| 500 | Server Error | unexpected error |
| 502 | Bad Gateway | payment answered badly |
| 503 | Unavailable | under maintenance |
Caller: correct the request
The request is broken: a required field is missing.
Step 1: 400 Bad Request
STATUS = { "unreadable": 400, "who_are_you": 401, "not_allowed": 403, "unknown_id": 404, "conflict": 409, "bad_values": 422, "too_many": 429, "bug": 500, "bad_upstream": 502, "down": 503,}- Caller does
- Reads status
- Team does
- Picks one rule
If you hesitate between two codes, ask what the caller should do next. That decides it.
In the real world
The same api error handling doing real jobs. Pick one and step through it.
The same wrong input, a bad and a good answer.
Step 1 of 2
Bad API
Nothing to act on
500 and something went wrong. The person tries the same thing again.
Step 1: Bad API
Common mistakes
The bugs interviewers watch for, with the fix beside each.
200 OK with an error inside
criticalCaches store it, monitors count it as success and clients forget to look.
Wrongreturn 200, {"success": False, "msg": "no"}Rightreturn 409, problem("DUPLICATE_ENTRY", ...)Showing the exception
criticalHost names, queries and file paths help an attacker and nobody else.
Wrongexcept Exception as e: return 500, {"error": str(e)}Rightexcept Exception as e: log(trace, e, stack=True) return 500, problem(UNEXPECTED, trace)Retrying everything
majorA 422 never gets better, and a repeated POST may charge twice.
Wrongwhile not ok: call()Rightif status in RETRYABLE and repeatable and n < 4: sleep(backoff(n))Stopping at the first field
minorThree mistakes become three slow trips for the user.
Wrongif bad(quantity): raise Error("quantity")if bad(zip): raise Error("zip")Righterrors = [e for e in checks(order) if e]if errors: raise ValidationError(errors)
Interview tips
How to explain it
- Status for the class, code for programs, sentence for people.
- One handler, one shape, unknown becomes 500.
- Details in the logs, linked by a trace id.
Memory trick: ERROR
- Explicit codes, Readable messages.
- Recovery hints, Observability.
- Retry guidance.
Say yes to a retry when
- 429 after its wait.
- 502, 503, 504, with care 500.
- The call is repeatable and attempts are few.
Never
- Reveal internals.
- Say which half of a login was wrong.
- Treat no answer as failure.