google.rpc.Statusによる詳細なエラーモデル
単純なステータスコードにとどまらず、google.rpc.Statusモデルと標準のエラー詳細型を使って、構造化された機械可読なエラー詳細を付加する方法を学びます。
「google.rpc.Statusによる詳細なエラーモデル」はCoddyKit上の無料gRPC & High Performance APIsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはgRPC & High Performance APIs学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 gRPC & High Performance APIsコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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
AI チューターと学ぶ gRPC & High Performance APIs — 無料
ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。
- コース
- 12
- レッスン
- 48
よくある質問
「google.rpc.Statusによる詳細なエラーモデル」レッスンは無料ですか?
はい。「google.rpc.Statusによる詳細なエラーモデル」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、gRPC & High Performance APIsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 gRPC & High Performance APIsコースには全4レッスンが含まれています。
「google.rpc.Statusによる詳細なエラーモデル」で何を学びますか?
単純なステータスコードにとどまらず、google.rpc.Statusモデルと標準のエラー詳細型を使って、構造化された機械可読なエラー詳細を付加する方法を学びます。 ブラウザで直接実行するハンズオンコードでgRPC & High Performance APIsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
gRPC & High Performance APIsを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのgRPC & High Performance APIsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「google.rpc.Statusによる詳細なエラーモデル」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このgRPC & High Performance APIsレッスンでコードを書いて実行できますか?
はい。すべてのgRPC & High Performance APIsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- ステータスコードとエラーハンドリング
- カスタムメタデータの送受信
- コンテキストとデッドライン
- google.rpc.Statusによる詳細なエラーモデル