Video in progress
A service you called returned 404 or 500. What status do you return to your own client?
I don't blindly pass through the downstream status code: its 404 often isn't a 404 for my client. I translate the error into my own domain, "not found", "bad request" or "a dependent service is unavailable" (502/503), and return it with a clear response body.
📖 Answer
For example, order-service was creating an order, asked product-service for a product, and got a 404. If you pass that 404 to the client, the frontend understands "the /orders endpoint wasn't found". In reality the product ID in the request is wrong, which is a 422 or 400, with a clear message.
An approximate table:
| Downstream response | What I return |
|---|---|
| 404 (the requested thing doesn't exist) | 400/422, "product not found" |
| 400 (I sent a bad request) | 500, that's my bug |
| 500, timeout, couldn't connect | 502 or 503 |
| 429 | 503, with Retry-After |
The second row is often forgotten: if downstream says "your request is invalid" and that comes from your code rather than from the client's data, blaming the client is wrong.
In Spring Boot 3 it's convenient to give the response body in the ProblemDetail (RFC 9457) format:
@ExceptionHandler(ProductNotFoundException.class)
ProblemDetail onMissingProduct(ProductNotFoundException e) {
var pd = ProblemDetail.forStatusAndDetail(HttpStatus.UNPROCESSABLE_ENTITY,
"Mahsulot topilmadi: " + e.productId());
pd.setTitle("Noto'g'ri mahsulot");
return pd;
}
Don't expose the downstream's internal error text (stack trace, SQL) to the client; write it to the log and leave only the traceId in the response.
Sign up to mark studied questions and save your progress.