From 422f95101412f0ac60259005fbf0eb2ed1b60141 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Tue, 17 Mar 2020 14:27:20 +0300 Subject: [PATCH] grpc client/server fixes (#1355) * grpc client/server fixes Signed-off-by: Vasiliy Tolstov --- grpc.go | 19 +++++-------------- util.go | 11 ----------- 2 files changed, 5 insertions(+), 25 deletions(-) diff --git a/grpc.go b/grpc.go index 4015780..af71aa5 100644 --- a/grpc.go +++ b/grpc.go @@ -279,6 +279,9 @@ func (g *grpcServer) handler(srv interface{}, stream grpc.ServerStream) error { // serve the actual request using the request router if err := r.ServeRequest(ctx, request, response); err != nil { + if _, ok := status.FromError(err); ok { + return err + } return status.Errorf(codes.Internal, err.Error()) } @@ -379,7 +382,6 @@ func (g *grpcServer) processRequest(stream grpc.ServerStream, service *service, for i := len(g.opts.HdlrWrappers); i > 0; i-- { fn = g.opts.HdlrWrappers[i-1](fn) } - statusCode := codes.OK statusDesc := "" // execute the handler @@ -402,24 +404,19 @@ func (g *grpcServer) processRequest(stream grpc.ServerStream, service *service, if err != nil { return err } - case *rpcError: - // rpcError handling may be we have ability to attach it to details? - statusCode = verr.code - statusDesc = verr.desc - errStatus = status.New(statusCode, statusDesc) default: // default case user pass own error type that not proto based statusCode = convertCode(verr) statusDesc = verr.Error() errStatus = status.New(statusCode, statusDesc) } + return errStatus.Err() } if err := stream.SendMsg(replyv.Interface()); err != nil { return err } - return status.New(statusCode, statusDesc).Err() } } @@ -459,8 +456,7 @@ func (g *grpcServer) processStream(stream grpc.ServerStream, service *service, m statusCode := codes.OK statusDesc := "" - appErr := fn(ctx, r, ss) - if appErr != nil { + if appErr := fn(ctx, r, ss); appErr != nil { var err error var errStatus *status.Status switch verr := appErr.(type) { @@ -480,11 +476,6 @@ func (g *grpcServer) processStream(stream grpc.ServerStream, service *service, m if err != nil { return err } - case *rpcError: - // rpcError handling may be we have ability to attach it to details? - statusCode = verr.code - statusDesc = verr.desc - errStatus = status.New(statusCode, statusDesc) default: // default case user pass own error type that not proto based statusCode = convertCode(verr) diff --git a/util.go b/util.go index 0583548..dfb467a 100644 --- a/util.go +++ b/util.go @@ -2,7 +2,6 @@ package grpc import ( "context" - "fmt" "io" "os" "sync" @@ -10,16 +9,6 @@ import ( "google.golang.org/grpc/codes" ) -// rpcError defines the status from an RPC. -type rpcError struct { - code codes.Code - desc string -} - -func (e *rpcError) Error() string { - return fmt.Sprintf("rpc error: code = %d desc = %s", e.code, e.desc) -} - // convertCode converts a standard Go error into its canonical code. Note that // this is only used to translate the error returned by the server applications. func convertCode(err error) codes.Code {