Lesson 2 of 9 · 23m
Errors, Status Codes, and Metadata Clients Can Use
Return actionable machine-readable failure, separate transport identity from business context, and stop leaking implementation detail through errors.
Most gRPC APIs have a vague success contract and an accidental error contract. Clients receive INTERNAL for everything, retry validation failures, and give up on permanent ones. The error surface is part of the API. If you do not design it, your exception hierarchy will design it for you.
Status codes are categories, not explanations
Use the narrowest truthful code:
INVALID_ARGUMENT caller sent malformed or inconsistent input
UNAUTHENTICATED credentials absent or invalid
PERMISSION_DENIED authenticated but not allowed
NOT_FOUND object absent or deliberately hidden
FAILED_PRECONDITION state does not permit the operation
ABORTED concurrency conflict; retry may succeed
RESOURCE_EXHAUSTED quota or rate limit
UNAVAILABLE transient dependency or overload
INTERNAL server defect; never a validation catch-allPERMISSION_DENIED and UNAUTHENTICATED are different because the client behavior is different: refresh credentials for one, stop or escalate for the other.
Rich details beat stringly errors
Use structured error details where your ecosystem supports them, commonly google.rpc.Status with BadRequest, RetryInfo, or ErrorInfo:
import "google/rpc/error_details.proto";
message ErrorInfo {
string reason = 1;
string domain = 2;
map<string, string> metadata = 3;
}A client should not regex "user quota exceeded" to decide what happened. Give it reason = "USER_QUOTA_EXCEEDED" and stable metadata such as quota = "active_workspaces".
Metadata is context, not a junk drawer
Metadata carries request-scoped cross-cutting values:
authorization: Bearer ...
x-request-id: 018f-example
x-tenant-id: tenant-7
traceparent: 00-...Keep it small and treat keys as contract surface. Do not place object payloads, large tokens, or user-controlled unbounded data in metadata. Many proxies and libraries impose tight size limits.
Hide implementation failure without hiding operability
A public client does not need:
pq: duplicate key value violates unique constraint "invoice_pkey"It needs a safe explanation and a correlation ID. The detailed exception belongs in server logs tied to that ID. Rewriting errors at the service boundary is not dishonesty; it is containment.
Deadlines interact with error meaning
DEADLINE_EXCEEDED means the caller or an intermediary stopped waiting. It does not prove the operation did not happen. That distinction drives idempotency, which later lessons cover. Do not tell users "failed" when the truth is "unknown after timeout."
Field notes
- Error strings are for humans; error reasons and metadata are for clients.
- Never use
INTERNALfor bad input because it hides validation defects in pager noise. - For hidden objects, choose
NOT_FOUNDorPERMISSION_DENIEDdeliberately; each leaks different existence information. - Canonicalize error mapping in one interceptor; test error contracts because retry logic depends on them.
Was this lesson helpful?