From edda9f9e2d9d35b93749880c2b9404ebc100526d Mon Sep 17 00:00:00 2001 From: Kosuke Morimoto Date: Wed, 2 Oct 2024 23:37:30 +0900 Subject: [PATCH] use FromError Signed-off-by: Kosuke Morimoto --- pkg/gateway/filter/handler/grpc/handler.go | 36 +++++++++++++++------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/pkg/gateway/filter/handler/grpc/handler.go b/pkg/gateway/filter/handler/grpc/handler.go index dd42febe0b7..04470281bb5 100644 --- a/pkg/gateway/filter/handler/grpc/handler.go +++ b/pkg/gateway/filter/handler/grpc/handler.go @@ -1448,10 +1448,13 @@ func (s *server) Search( } res, err = s.gateway.Search(ctx, req, s.copts...) if err != nil { - st, msg, err := status.ParseError(err, codes.Internal, "failed to parse "+vald.SearchRPCName+" gRPC error response") + st, ok := status.FromError(err) + if !ok || st == nil { + st = status.New(codes.Internal, "failed to convert "+vald.SearchRPCName+" gRPC error response") + } if span != nil { span.RecordError(err) - span.SetAttributes(trace.FromGRPCStatus(st.Code(), msg)...) + span.SetAttributes(trace.FromGRPCStatus(st.Code(), st.Message())...) span.SetStatus(trace.StatusError, err.Error()) } return nil, err @@ -1531,10 +1534,13 @@ func (s *server) SearchByID( }() res, err = s.gateway.SearchByID(ctx, req, s.copts...) if err != nil { - st, msg, err := status.ParseError(err, codes.Internal, "failed to parse "+vald.SearchByIDRPCName+" gRPC error response") + st, ok := status.FromError(err) + if !ok || st == nil { + st = status.New(codes.Internal, "failed to convert "+vald.SearchByIDRPCName+" gRPC error response") + } if span != nil { span.RecordError(err) - span.SetAttributes(trace.FromGRPCStatus(st.Code(), msg)...) + span.SetAttributes(trace.FromGRPCStatus(st.Code(), st.Message())...) span.SetStatus(trace.StatusError, err.Error()) } return nil, err @@ -2196,11 +2202,13 @@ func (s *server) StreamLinearSearchByID( }, nil }) if err != nil { - st, msg, err := status.ParseError(err, codes.Internal, - "failed to parse "+vald.StreamLinearSearchRPCName+" gRPC error response") + st, ok := status.FromError(err) + if !ok || st == nil { + st = status.New(codes.Internal, "failed to convert "+vald.StreamLinearSearchRPCName+" gRPC error response") + } if span != nil { span.RecordError(err) - span.SetAttributes(trace.FromGRPCStatus(st.Code(), msg)...) + span.SetAttributes(trace.FromGRPCStatus(st.Code(), st.Message())...) span.SetStatus(trace.StatusError, err.Error()) } log.Error(err) @@ -3056,10 +3064,13 @@ func (s *server) StreamUpsert(stream vald.Upsert_StreamUpsertServer) (err error) }, nil }) if err != nil { - st, msg, err := status.ParseError(err, codes.Internal, "failed to parse "+vald.StreamUpsertRPCName+" gRPC error response") + st, ok := status.FromError(err) + if !ok || st == nil { + st = status.New(codes.Internal, "failed to parse "+vald.StreamUpsertRPCName+" gRPC error response") + } if span != nil { span.RecordError(err) - span.SetAttributes(trace.FromGRPCStatus(st.Code(), msg)...) + span.SetAttributes(trace.FromGRPCStatus(st.Code(), st.Message())...) span.SetStatus(trace.StatusError, err.Error()) } log.Error(err) @@ -3223,10 +3234,13 @@ func (s *server) StreamRemove(stream vald.Remove_StreamRemoveServer) (err error) }, nil }) if err != nil { - st, msg, err := status.ParseError(err, codes.Internal, "failed to parse "+vald.StreamRemoveRPCName+" gRPC error response") + st, ok := status.FromError(err) + if !ok || st == nil { + st = status.New(codes.Internal, "failed to parse "+vald.StreamRemoveRPCName+" gRPC error response") + } if span != nil { span.RecordError(err) - span.SetAttributes(trace.FromGRPCStatus(st.Code(), msg)...) + span.SetAttributes(trace.FromGRPCStatus(st.Code(), st.Message())...) span.SetStatus(trace.StatusError, err.Error()) } log.Error(err)