Skip to content

Commit

Permalink
*: replace deprecated grpc.Errorf calls with status.Errorf (grpc#1651)
Browse files Browse the repository at this point in the history
  • Loading branch information
gyuho authored and dfawley committed Nov 6, 2017
1 parent 4318e64 commit 865013b
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 64 deletions.
15 changes: 8 additions & 7 deletions benchmark/worker/benchmark_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/status"
"google.golang.org/grpc/testdata"
)

Expand Down Expand Up @@ -117,7 +118,7 @@ func createConns(config *testpb.ClientConfig) ([]*grpc.ClientConn, func(), error
case testpb.ClientType_SYNC_CLIENT:
case testpb.ClientType_ASYNC_CLIENT:
default:
return nil, nil, grpc.Errorf(codes.InvalidArgument, "unknow client type: %v", config.ClientType)
return nil, nil, status.Errorf(codes.InvalidArgument, "unknow client type: %v", config.ClientType)
}

// Check and set security options.
Expand All @@ -127,7 +128,7 @@ func createConns(config *testpb.ClientConfig) ([]*grpc.ClientConn, func(), error
}
creds, err := credentials.NewClientTLSFromFile(*caFile, config.SecurityParams.ServerHostOverride)
if err != nil {
return nil, nil, grpc.Errorf(codes.InvalidArgument, "failed to create TLS credentials %v", err)
return nil, nil, status.Errorf(codes.InvalidArgument, "failed to create TLS credentials %v", err)
}
opts = append(opts, grpc.WithTransportCredentials(creds))
} else {
Expand All @@ -141,7 +142,7 @@ func createConns(config *testpb.ClientConfig) ([]*grpc.ClientConn, func(), error
opts = append(opts, grpc.WithCodec(byteBufCodec{}))
case *testpb.PayloadConfig_SimpleParams:
default:
return nil, nil, grpc.Errorf(codes.InvalidArgument, "unknow payload config: %v", config.PayloadConfig)
return nil, nil, status.Errorf(codes.InvalidArgument, "unknow payload config: %v", config.PayloadConfig)
}
}

Expand Down Expand Up @@ -176,17 +177,17 @@ func performRPCs(config *testpb.ClientConfig, conns []*grpc.ClientConn, bc *benc
payloadRespSize = int(c.SimpleParams.RespSize)
payloadType = "protobuf"
default:
return grpc.Errorf(codes.InvalidArgument, "unknow payload config: %v", config.PayloadConfig)
return status.Errorf(codes.InvalidArgument, "unknow payload config: %v", config.PayloadConfig)
}
}

// TODO add open loop distribution.
switch config.LoadParams.Load.(type) {
case *testpb.LoadParams_ClosedLoop:
case *testpb.LoadParams_Poisson:
return grpc.Errorf(codes.Unimplemented, "unsupported load params: %v", config.LoadParams)
return status.Errorf(codes.Unimplemented, "unsupported load params: %v", config.LoadParams)
default:
return grpc.Errorf(codes.InvalidArgument, "unknown load params: %v", config.LoadParams)
return status.Errorf(codes.InvalidArgument, "unknown load params: %v", config.LoadParams)
}

rpcCountPerConn := int(config.OutstandingRpcsPerChannel)
Expand All @@ -199,7 +200,7 @@ func performRPCs(config *testpb.ClientConfig, conns []*grpc.ClientConn, bc *benc
bc.doCloseLoopStreaming(conns, rpcCountPerConn, payloadReqSize, payloadRespSize, payloadType)
// TODO open loop.
default:
return grpc.Errorf(codes.InvalidArgument, "unknown rpc type: %v", config.RpcType)
return status.Errorf(codes.InvalidArgument, "unknown rpc type: %v", config.RpcType)
}

return nil
Expand Down
7 changes: 4 additions & 3 deletions benchmark/worker/benchmark_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/status"
"google.golang.org/grpc/testdata"
)

Expand Down Expand Up @@ -86,7 +87,7 @@ func startBenchmarkServer(config *testpb.ServerConfig, serverPort int) (*benchma
case testpb.ServerType_ASYNC_SERVER:
case testpb.ServerType_ASYNC_GENERIC_SERVER:
default:
return nil, grpc.Errorf(codes.InvalidArgument, "unknow server type: %v", config.ServerType)
return nil, status.Errorf(codes.InvalidArgument, "unknow server type: %v", config.ServerType)
}

// Set security options.
Expand Down Expand Up @@ -131,9 +132,9 @@ func startBenchmarkServer(config *testpb.ServerConfig, serverPort int) (*benchma
Type: "protobuf",
}, opts...)
case *testpb.PayloadConfig_ComplexParams:
return nil, grpc.Errorf(codes.Unimplemented, "unsupported payload config: %v", config.PayloadConfig)
return nil, status.Errorf(codes.Unimplemented, "unsupported payload config: %v", config.PayloadConfig)
default:
return nil, grpc.Errorf(codes.InvalidArgument, "unknow payload config: %v", config.PayloadConfig)
return nil, status.Errorf(codes.InvalidArgument, "unknow payload config: %v", config.PayloadConfig)
}
} else {
// Start protobuf server if payload config is nil.
Expand Down
5 changes: 3 additions & 2 deletions benchmark/worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
testpb "google.golang.org/grpc/benchmark/grpc_testing"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/status"
)

var (
Expand Down Expand Up @@ -114,7 +115,7 @@ func (s *workerServer) RunServer(stream testpb.WorkerService_RunServerServer) er
grpclog.Printf("server mark received:")
grpclog.Printf(" - %v", argtype)
if bs == nil {
return grpc.Errorf(codes.InvalidArgument, "server does not exist when mark received")
return status.Error(codes.InvalidArgument, "server does not exist when mark received")
}
out = &testpb.ServerStatus{
Stats: bs.getStats(argtype.Mark.Reset_),
Expand Down Expand Up @@ -167,7 +168,7 @@ func (s *workerServer) RunClient(stream testpb.WorkerService_RunClientServer) er
grpclog.Printf("client mark received:")
grpclog.Printf(" - %v", t)
if bc == nil {
return grpc.Errorf(codes.InvalidArgument, "client does not exist when mark received")
return status.Error(codes.InvalidArgument, "client does not exist when mark received")
}
out = &testpb.ClientStatus{
Stats: bc.getStats(t.Mark.Reset_),
Expand Down
7 changes: 4 additions & 3 deletions grpclb/grpclb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
_ "google.golang.org/grpc/grpclog/glogger"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/naming"
"google.golang.org/grpc/status"
testpb "google.golang.org/grpc/test/grpc_testing"
"google.golang.org/grpc/test/leakcheck"
)
Expand Down Expand Up @@ -225,7 +226,7 @@ func (b *remoteBalancer) BalanceLoad(stream lbspb.LoadBalancer_BalanceLoadServer
}
initReq := req.GetInitialRequest()
if initReq.Name != besn {
return grpc.Errorf(codes.InvalidArgument, "invalid service name: %v", initReq.Name)
return status.Errorf(codes.InvalidArgument, "invalid service name: %v", initReq.Name)
}
resp := &lbmpb.LoadBalanceResponse{
LoadBalanceResponseType: &lbmpb.LoadBalanceResponse_InitialResponse{
Expand Down Expand Up @@ -285,10 +286,10 @@ const testmdkey = "testmd"
func (s *testServer) EmptyCall(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, grpc.Errorf(codes.Internal, "failed to receive metadata")
return nil, status.Error(codes.Internal, "failed to receive metadata")
}
if md == nil || md["lb-token"][0] != lbToken {
return nil, grpc.Errorf(codes.Internal, "received unexpected metadata: %v", md)
return nil, status.Errorf(codes.Internal, "received unexpected metadata: %v", md)
}
grpc.SetTrailer(ctx, metadata.Pairs(testmdkey, s.addr))
return &testpb.Empty{}, nil
Expand Down
4 changes: 2 additions & 2 deletions health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import (
"sync"

"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/status"
)

// Server implements `service Health`.
Expand Down Expand Up @@ -60,7 +60,7 @@ func (s *Server) Check(ctx context.Context, in *healthpb.HealthCheckRequest) (*h
Status: status,
}, nil
}
return nil, grpc.Errorf(codes.NotFound, "unknown service")
return nil, status.Error(codes.NotFound, "unknown service")
}

// SetServingStatus is called when need to reset the serving status of a service
Expand Down
15 changes: 8 additions & 7 deletions interop/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"google.golang.org/grpc/grpclog"
testpb "google.golang.org/grpc/interop/grpc_testing"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)

var (
Expand Down Expand Up @@ -533,7 +534,7 @@ func DoCustomMetadata(tc testpb.TestServiceClient, args ...grpc.CallOption) {
func DoStatusCodeAndMessage(tc testpb.TestServiceClient, args ...grpc.CallOption) {
var code int32 = 2
msg := "test status message"
expectedErr := grpc.Errorf(codes.Code(code), msg)
expectedErr := status.Error(codes.Code(code), msg)
respStatus := &testpb.EchoStatus{
Code: code,
Message: msg,
Expand Down Expand Up @@ -611,7 +612,7 @@ func serverNewPayload(t testpb.PayloadType, size int32) (*testpb.Payload, error)
}

func (s *testServer) UnaryCall(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
status := in.GetResponseStatus()
st := in.GetResponseStatus()
if md, ok := metadata.FromIncomingContext(ctx); ok {
if initialMetadata, ok := md[initialMetadataKey]; ok {
header := metadata.Pairs(initialMetadataKey, initialMetadata[0])
Expand All @@ -622,8 +623,8 @@ func (s *testServer) UnaryCall(ctx context.Context, in *testpb.SimpleRequest) (*
grpc.SetTrailer(ctx, trailer)
}
}
if status != nil && status.Code != 0 {
return nil, grpc.Errorf(codes.Code(status.Code), status.Message)
if st != nil && st.Code != 0 {
return nil, status.Error(codes.Code(st.Code), st.Message)
}
pl, err := serverNewPayload(in.GetResponseType(), in.GetResponseSize())
if err != nil {
Expand Down Expand Up @@ -690,9 +691,9 @@ func (s *testServer) FullDuplexCall(stream testpb.TestService_FullDuplexCallServ
if err != nil {
return err
}
status := in.GetResponseStatus()
if status != nil && status.Code != 0 {
return grpc.Errorf(codes.Code(status.Code), status.Message)
st := in.GetResponseStatus()
if st != nil && st.Code != 0 {
return status.Error(codes.Code(st.Code), st.Message)
}
cs := in.GetResponseParameters()
for _, c := range cs {
Expand Down
3 changes: 2 additions & 1 deletion reflection/serverreflection.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
rpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
"google.golang.org/grpc/status"
)

type serverReflectionServer struct {
Expand Down Expand Up @@ -388,7 +389,7 @@ func (s *serverReflectionServer) ServerReflectionInfo(stream rpb.ServerReflectio
},
}
default:
return grpc.Errorf(codes.InvalidArgument, "invalid MessageRequest: %v", in.MessageRequest)
return status.Errorf(codes.InvalidArgument, "invalid MessageRequest: %v", in.MessageRequest)
}

if err := stream.Send(out); err != nil {
Expand Down
11 changes: 6 additions & 5 deletions stats/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/stats"
testpb "google.golang.org/grpc/stats/grpc_testing"
"google.golang.org/grpc/status"
)

func init() {
Expand Down Expand Up @@ -63,10 +64,10 @@ func (s *testServer) UnaryCall(ctx context.Context, in *testpb.SimpleRequest) (*
md, ok := metadata.FromIncomingContext(ctx)
if ok {
if err := grpc.SendHeader(ctx, md); err != nil {
return nil, grpc.Errorf(grpc.Code(err), "grpc.SendHeader(_, %v) = %v, want <nil>", md, err)
return nil, status.Errorf(grpc.Code(err), "grpc.SendHeader(_, %v) = %v, want <nil>", md, err)
}
if err := grpc.SetTrailer(ctx, testTrailerMetadata); err != nil {
return nil, grpc.Errorf(grpc.Code(err), "grpc.SetTrailer(_, %v) = %v, want <nil>", testTrailerMetadata, err)
return nil, status.Errorf(grpc.Code(err), "grpc.SetTrailer(_, %v) = %v, want <nil>", testTrailerMetadata, err)
}
}

Expand All @@ -81,7 +82,7 @@ func (s *testServer) FullDuplexCall(stream testpb.TestService_FullDuplexCallServ
md, ok := metadata.FromIncomingContext(stream.Context())
if ok {
if err := stream.SendHeader(md); err != nil {
return grpc.Errorf(grpc.Code(err), "%v.SendHeader(%v) = %v, want %v", stream, md, err, nil)
return status.Errorf(grpc.Code(err), "%v.SendHeader(%v) = %v, want %v", stream, md, err, nil)
}
stream.SetTrailer(testTrailerMetadata)
}
Expand Down Expand Up @@ -109,7 +110,7 @@ func (s *testServer) ClientStreamCall(stream testpb.TestService_ClientStreamCall
md, ok := metadata.FromIncomingContext(stream.Context())
if ok {
if err := stream.SendHeader(md); err != nil {
return grpc.Errorf(grpc.Code(err), "%v.SendHeader(%v) = %v, want %v", stream, md, err, nil)
return status.Errorf(grpc.Code(err), "%v.SendHeader(%v) = %v, want %v", stream, md, err, nil)
}
stream.SetTrailer(testTrailerMetadata)
}
Expand All @@ -133,7 +134,7 @@ func (s *testServer) ServerStreamCall(in *testpb.SimpleRequest, stream testpb.Te
md, ok := metadata.FromIncomingContext(stream.Context())
if ok {
if err := stream.SendHeader(md); err != nil {
return grpc.Errorf(grpc.Code(err), "%v.SendHeader(%v) = %v, want %v", stream, md, err, nil)
return status.Errorf(grpc.Code(err), "%v.SendHeader(%v) = %v, want %v", stream, md, err, nil)
}
stream.SetTrailer(testTrailerMetadata)
}
Expand Down
3 changes: 2 additions & 1 deletion stress/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/interop"
testpb "google.golang.org/grpc/interop/grpc_testing"
"google.golang.org/grpc/status"
metricspb "google.golang.org/grpc/stress/grpc_testing"
"google.golang.org/grpc/testdata"
)
Expand Down Expand Up @@ -176,7 +177,7 @@ func (s *server) GetGauge(ctx context.Context, in *metricspb.GaugeRequest) (*met
if g, ok := s.gauges[in.Name]; ok {
return &metricspb.GaugeResponse{Name: in.Name, Value: &metricspb.GaugeResponse_LongValue{LongValue: g.get()}}, nil
}
return nil, grpc.Errorf(codes.InvalidArgument, "gauge with name %s not found", in.Name)
return nil, status.Errorf(codes.InvalidArgument, "gauge with name %s not found", in.Name)
}

// createGauge creates a gauge using the given name in metrics server.
Expand Down
Loading

0 comments on commit 865013b

Please sign in to comment.