API Error Handling 00:00:01 What can the caller change? A customer submits a form with a missing field. Her colleague submits a perfectly valid order just as the server stops responding. If both phones merely say something went wrong, neither person knows what to do next. The request is broken: a required field is missing. We know, and the answer is no. It clashes with the current state: the address is already registered. Readable, but the price must be positive. Something unexpected in our code. We cannot serve now, and we say until when. A validation error can name the field and accepted shape. Exposing a private stack trace does not help the customer correct the form and can reveal internals unnecessarily. 00:00:50 One place to translate failures Business code raises its own errors. One handler at the edge of the service answers. It gets a trace id before anything can fail. The database drops the connection. The code does not catch it. Stack trace, query name and user go to the log under 4bf9. 500, unexpected error, trace id. Nothing else. Not found is ours: 404 with a clear sentence, the same shape. Map known failures to a consistent public contract. Keep surprising internal details in protected logs with a correlation identifier so the team can investigate without teaching clients to depend on private exceptions. 00:01:36 Report independent issues together A form with three fields, two of them wrong. Quantity 0, ZIP 12 and a good address. 0 is outside 1 to 100. Note it and go on. 12 is not five digits. Note it too. Fine. 422 with both problems. The form shows both messages. If several fields are invalid, returning a structured collection can save repeated submissions. For a business operation, still explain whether any changes were committed before the error. 00:02:12 Retries need limits Three failures, then success. The limits double; the real wait is random below the limit. 503. The server is overloaded. Second call, still 503. Third call, still 503. Fourth call works. At most 7 seconds have passed. Backoff helps a caller avoid adding work while a dependency is overloaded. Only retry operations with suitable repeat semantics, and stop once the caller's overall deadline expires. 00:02:47 Trace the failed request One id through three services, then one search. 4bf9 for this request. Each call carries the id. Every service writes what it saw. Support gets 4bf9 from the customer and sees all three lines. An error identifier helps the support team find one request across components. Correlate it with traces and deployment changes before interpreting a correlation as proof of a cause. 00:03:16 Ten items, several outcomes One transaction. Item 7 fails, so none is stored and the answer is an error. Nine are stored. The body lists ten results, number 7 with its own error. Keys per item make the second call safe. A batch may partly succeed. Give each item an interpretable result and a stable operation identity so the caller can retry the unresolved items without duplicating completed writes. Design an error response for the next safe caller action. Distinguish repairing the request, supplying credentials, waiting and checking whether a previous write actually succeeded.