Slide 46
Slide 46 text
Ⅰ マイクロサービス間でのエラーの伝播②
4. Server InterceptorでError Detailsにカスタムエラーを突っ込む
5.
func ApplicationErrorUnaryServerInterceptor() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler)
(interface{}, error) {
res, err := handler(ctx, req)
if err == nil {
return res, nil
}
ae, ok := err.(*ApplicationError)
if !ok {
ae = UnknownError.Wrap(err).(*ApplicationError)
}
st := status.New(ae.gRPCStatusCode, ae.Message())
if st, _ := st.WithDetails(&commonpb.ApplicationError{
Message: ae.message,
Code: ae.code,
}); st != nil {
return nil, st.Err()
}
return nil, st.Err()
}
}
エラーが起きたらエラーをProtoで
シリアライズして詰めている
エラーに対応するgRPCステータス
コードを設定している