How does error handling and status codes work in gRPC?
Understand how gRPC error handling works: canonical status codes, error messages, structured details, and how clients handle and retry failed RPC calls.
Expected Interview Answer
gRPC reports the outcome of every call through a numeric status code (0 = OK, non-zero = error) drawn from a fixed set of canonical codes, an optional human-readable message, and optional structured error details carried in trailing metadata.
Instead of HTTP status codes, gRPC defines its own enum of roughly 17 codes such as NOT_FOUND, INVALID_ARGUMENT, DEADLINE_EXCEEDED, UNAVAILABLE, and PERMISSION_DENIED. A server signals failure by returning a status rather than a normal response; the client receives it as an exception or error object with the code and message. Rich, machine-readable details (like which field failed validation) are attached using the google.rpc.Status message and error-detail protos, transmitted in the grpc-status-details-bin trailer.
- Consistent, language-independent error semantics across services
- Callers can branch on a small, well-defined code set
- Structured details enable precise, machine-readable failures
- Retry logic can key off codes like UNAVAILABLE
- Cleaner separation of transport errors from application errors
AI Mentor Explanation
Think of the umpire's signals in cricket: a raised finger, a wide, a no-ball, a boundary wave — each is a fixed, well-known gesture every player instantly understands. A gRPC status code is that agreed signal set: OK is the run given, NOT_FOUND is the not-out call, and the spoken explanation to the captain is the error message that adds detail.
Step-by-Step Explanation
Step 1
Return a status, not a response
On failure the server aborts the RPC and returns a status code and message instead of a normal reply message.
Step 2
Pick the right canonical code
Map the failure to one of gRPC's fixed codes — INVALID_ARGUMENT for bad input, NOT_FOUND for missing entities, UNAVAILABLE for transient outages.
Step 3
Attach structured details
Use google.rpc.Status with detail protos (e.g. BadRequest, QuotaFailure) serialized into the grpc-status-details-bin trailer for machine-readable context.
Step 4
Propagate to the client
The client library surfaces the status as an exception or error object exposing the code, message, and any details.
Step 5
Handle by code
The caller branches on the code — retry UNAVAILABLE, surface INVALID_ARGUMENT to the user, treat DEADLINE_EXCEEDED as a timeout.
What Interviewer Expects
- Knowledge that gRPC uses its own status-code enum, not HTTP codes
- Ability to name common codes and when to use them
- Understanding of status message vs structured error details
- Awareness of the grpc-status-details-bin trailer and google.rpc.Status
- How codes inform retry and client-side handling
Common Mistakes
- Assuming gRPC returns HTTP status codes directly
- Returning OK on a logical failure and hiding the error in the payload
- Overusing UNKNOWN instead of a precise canonical code
- Putting sensitive internal details in the client-facing message
- Forgetting that error details ride in trailing metadata
Best Answer (HR Friendly)
“In gRPC, every request comes back with a clear result code — like a labelled stamp saying 'OK', 'not found', or 'invalid input' — instead of a vague failure. This lets the calling program understand exactly what went wrong and respond appropriately, such as retrying or showing a helpful message to the user.”
Code Example
import grpc
def GetUser(self, request, context):
user = db.find(request.id)
if user is None:
context.set_code(grpc.StatusCode.NOT_FOUND)
context.set_details(f"user {request.id} does not exist")
return user_pb2.User()
return usertry:
resp = stub.GetUser(user_pb2.GetUserRequest(id=42))
except grpc.RpcError as e:
if e.code() == grpc.StatusCode.NOT_FOUND:
print("no such user:", e.details())
elif e.code() == grpc.StatusCode.UNAVAILABLE:
retry()Follow-up Questions
- Which gRPC status codes are safe to retry automatically?
- How do you attach field-level validation errors to a gRPC error?
- How do gRPC status codes map to HTTP/2 and gRPC-Web?
- What is the difference between UNKNOWN and INTERNAL?
- How would you convert application exceptions into gRPC statuses centrally?
MCQ Practice
1. What does a gRPC status code of 0 mean?
Status code 0 (OK) indicates the RPC completed successfully; all non-zero codes indicate an error.
2. Where are structured gRPC error details transmitted?
Rich details use google.rpc.Status serialized into the grpc-status-details-bin trailer, separate from the status code and message.
3. Which code best fits a request with a malformed field value?
INVALID_ARGUMENT signals the client sent a bad argument that is invalid regardless of system state.
Flash Cards
What signals success in gRPC? — Status code 0 (OK). Any non-zero code from the canonical enum indicates an error.
How are rich error details carried? — As google.rpc.Status detail protos in the grpc-status-details-bin trailing metadata.
INVALID_ARGUMENT vs FAILED_PRECONDITION? — INVALID_ARGUMENT is bad input regardless of state; FAILED_PRECONDITION means state isn't ready for the operation.
Which code implies a safe retry? — UNAVAILABLE typically indicates a transient condition that is safe to retry with backoff.