Skip to content

Instantly share code, notes, and snippets.

@oshiro-kazuma
Last active March 3, 2021 08:08
Show Gist options
  • Save oshiro-kazuma/d5c72b62b8c30d5dee8c324c1b56b907 to your computer and use it in GitHub Desktop.
Save oshiro-kazuma/d5c72b62b8c30d5dee8c324c1b56b907 to your computer and use it in GitHub Desktop.
package main
import (
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func main() {
// grpc標準のコード、1がOK
st := status.New(codes.Unknown, "Name is blank")
// 詳細なエラー情報
// ここはプロバフのMessageで、es-serverでエラー用のprotoを定義してあげればいいかな
// errdetails.RetryInfoなどはgoogleapisが使っているMessage
retry := &errdetails.RetryInfo{
RetryDelay: &duration.Duration{
Seconds: 30,
Nanos: 0,
},
}
details := &errdetails.BadRequest{
FieldViolations: []*errdetails.BadRequest_FieldViolation{
{Field: "Name", Description: "Name is required. Only alphanumeric characters are allowed"},
},
}
err := &errdetails.ErrorInfo{
Reason: "",
Domain: "",
Metadata: map[string]string{
"error_code": "4000401",
},
}
// WithDetailsでヘッダーにbinではいる?ましまさんが言っていたやつ
// ここは gRPC 標準仕様
st, _ = st.WithDetails(err, details, retry)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment