gRPC errorsmedium3-5 years

A gRPC service needs to signal 'the requested order doesn't exist' and 'you don't have permission to view this order' to its caller. What mechanism carries that, and how would you decide which status code to use for each?

gRPC errors are a status code from a fixed, small set — not an HTTP status code, though several map closely onto ones you already know — with a message and, optionally, typed error details attached as trailers. "Order doesn't exist" is NOT_FOUND (the gRPC analogue of HTTP 404); "you don't have permission" is PERMISSION_DENIED (the analogue of 403) — both thrown on the server as Status.NOT_FOUND.withDescription(...).asRuntimeException() or similar, and both caught on the client by switching on getStatus().getCode() from the StatusRuntimeException. The one rule that matters regardless of which code: the description is a message that crosses the wire to the caller, so it can carry "order 42" but never a stack trace or internal detail.

The lesson behind it →