Modelli avanzati degli errori con google.rpc.Status
Vada oltre i semplici codici di stato allegando dettagli strutturati e leggibili dalle macchine con il modello google.rpc.Status e i tipi standard per i dettagli degli errori.
Modelli avanzati degli errori con google.rpc.Status è una lezione gRPC & High Performance APIs gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento gRPC & High Performance APIs, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso gRPC & High Performance APIs include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Limits of Plain Status Codes
A bare status code plus a message tells the client that something failed, but not the structured why. Clients often need field-level validation errors, retry hints, or quota info.
The rich error model attaches structured details to a status.
The google.rpc.Status Message
The core type is google.rpc.Status with three fields:
- code: a numeric status code
- message: developer-facing text
- details: a repeated list of
Anypayloads
Standard Detail Types
Google defines reusable detail messages in google/rpc/error_details.proto:
BadRequest— field violationsRetryInfo— when to retryQuotaFailure— limit exceededErrorInfo— machine-readable reason
BadRequest for Validation
BadRequest carries a list of FieldViolation entries, each naming a bad field and describing the problem. Perfect for form validation responses.
Building a Rich Error in Go
The status package lets you create a status and append typed details with WithDetails.
st := status.New(codes.InvalidArgument, 'invalid request')
v := &errdetails.BadRequest_FieldViolation{
Field: 'email', Description: 'must be a valid address',
}
br := &errdetails.BadRequest{FieldViolations: []*errdetails.BadRequest_FieldViolation{v}}
st, _ = st.WithDetails(br)
return st.Err()RetryInfo for Backoff Hints
For temporary failures, attach RetryInfo with a retry_delay. A well-behaved client reads this and waits before retrying.
ri := &errdetails.RetryInfo{RetryDelay: durationpb.New(2 * time.Second)}
st, _ = status.New(codes.Unavailable, 'busy').WithDetails(ri)ErrorInfo for Stable Reasons
ErrorInfo gives a stable reason string and a domain plus metadata. Unlike free-text messages, clients can branch on these reliably.
ei := &errdetails.ErrorInfo{
Reason: 'EMAIL_TAKEN', Domain: 'auth.example.com',
}Reading Details on the Client
The client converts the returned error back to a status and inspects each detail with a type switch.
st := status.Convert(err)
for _, d := range st.Details() {
switch t := d.(type) {
case *errdetails.BadRequest:
handleFieldErrors(t)
case *errdetails.RetryInfo:
waitThenRetry(t.RetryDelay)
}
}How Details Travel
Details are serialized into the grpc-status-details-bin trailer as a binary Status proto. Languages with the rich-error libraries decode it automatically.
Best Practices
Use the rich model wisely:
- Prefer standard detail types for interoperability
- Never leak secrets in messages or details
- Keep
ErrorInfo.reasonvalues stable and documented - Pair
RetryInfowith truly retryable codes
Cross-Language Interop
Because the model is defined in protobuf, a Go server can emit a BadRequest that a Java or Python client decodes identically. This consistency is the whole point of the standard types.
Quick Check
Test your rich error knowledge.
Recap
You learned the rich error model:
google.rpc.Statuscarries code, message, and repeated detailAnypayloads- Standard types:
BadRequest,RetryInfo,QuotaFailure,ErrorInfo - Build with
WithDetails, read with a type switch overDetails() - Details travel in the
grpc-status-details-bintrailer - Standard types give cross-language consistency
Domande Frequenti
La lezione «Modelli avanzati degli errori con google.rpc.Status» è gratuita?
Sì — il testo completo di «Modelli avanzati degli errori con google.rpc.Status» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso gRPC & High Performance APIs, passa a CoddyKit PRO. Il corso gRPC & High Performance APIs include 4 lezioni in totale.
Cosa imparerò in «Modelli avanzati degli errori con google.rpc.Status»?
Vada oltre i semplici codici di stato allegando dettagli strutturati e leggibili dalle macchine con il modello google.rpc.Status e i tipi standard per i dettagli degli errori. Eserciti gRPC & High Performance APIs con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare gRPC & High Performance APIs?
Non è richiesta alcuna esperienza precedente. gRPC & High Performance APIs su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Modelli avanzati degli errori con google.rpc.Status»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione gRPC & High Performance APIs?
Sì. Ogni lezione gRPC & High Performance APIs include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Codici di stato e gestione degli errori
- Trasmissione di metadati personalizzati
- Contesto e scadenze
- Modelli avanzati degli errori con google.rpc.Status