From ed850f844bbbd62586a08a14528f99e6dac9a74b Mon Sep 17 00:00:00 2001 From: Jean de Klerk Date: Fri, 23 Feb 2018 10:32:40 -0800 Subject: [PATCH] Remove convertCode and replace usage with codes.Unknown - convertCode utilized errors that were not allowed by the library per https://github.com/grpc/grpc/blob/9d0bc30edbe14fef58f32e74009dd513dee2cfd0/doc/statuscodes.md - Relevant issue: #1672 --- go16.go | 29 ----------------------------- go17.go | 29 ----------------------------- interceptor.go | 6 ++++-- server.go | 4 ++-- stream.go | 7 +++++-- 5 files changed, 11 insertions(+), 64 deletions(-) diff --git a/go16.go b/go16.go index 0ae4dbda9e6f..535ee9356f34 100644 --- a/go16.go +++ b/go16.go @@ -25,7 +25,6 @@ import ( "io" "net" "net/http" - "os" "golang.org/x/net/context" "google.golang.org/grpc/codes" @@ -69,31 +68,3 @@ func toRPCErr(err error) error { } return status.Error(codes.Unknown, err.Error()) } - -// 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 { - switch err { - case nil: - return codes.OK - case io.EOF: - return codes.OutOfRange - case io.ErrClosedPipe, io.ErrNoProgress, io.ErrShortBuffer, io.ErrShortWrite, io.ErrUnexpectedEOF: - return codes.FailedPrecondition - case os.ErrInvalid: - return codes.InvalidArgument - case context.Canceled: - return codes.Canceled - case context.DeadlineExceeded: - return codes.DeadlineExceeded - } - switch { - case os.IsExist(err): - return codes.AlreadyExists - case os.IsNotExist(err): - return codes.NotFound - case os.IsPermission(err): - return codes.PermissionDenied - } - return codes.Unknown -} diff --git a/go17.go b/go17.go index 539088280838..ec676a93c396 100644 --- a/go17.go +++ b/go17.go @@ -26,7 +26,6 @@ import ( "io" "net" "net/http" - "os" netctx "golang.org/x/net/context" "google.golang.org/grpc/codes" @@ -70,31 +69,3 @@ func toRPCErr(err error) error { } return status.Error(codes.Unknown, err.Error()) } - -// 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 { - switch err { - case nil: - return codes.OK - case io.EOF: - return codes.OutOfRange - case io.ErrClosedPipe, io.ErrNoProgress, io.ErrShortBuffer, io.ErrShortWrite, io.ErrUnexpectedEOF: - return codes.FailedPrecondition - case os.ErrInvalid: - return codes.InvalidArgument - case context.Canceled, netctx.Canceled: - return codes.Canceled - case context.DeadlineExceeded, netctx.DeadlineExceeded: - return codes.DeadlineExceeded - } - switch { - case os.IsExist(err): - return codes.AlreadyExists - case os.IsNotExist(err): - return codes.NotFound - case os.IsPermission(err): - return codes.PermissionDenied - } - return codes.Unknown -} diff --git a/interceptor.go b/interceptor.go index 06dc825b9fba..8162c5bca677 100644 --- a/interceptor.go +++ b/interceptor.go @@ -45,10 +45,12 @@ type UnaryServerInfo struct { Server interface{} // FullMethod is the full RPC method string, i.e., /package.service/method. FullMethod string -} +}; // UnaryHandler defines the handler invoked by UnaryServerInterceptor to complete the normal -// execution of a unary RPC. +// execution of a unary RPC. If a UnaryHandler returns an error, it should be produced by the +// status package, or else gRPC will use codes.Unknown as the status code and err.Error() as +// the status message of the RPC. type UnaryHandler func(ctx context.Context, req interface{}) (interface{}, error) // UnaryServerInterceptor provides a hook to intercept the execution of a unary RPC on the server. info diff --git a/server.go b/server.go index 0f7ff5d6022a..c085b23c7b2e 100644 --- a/server.go +++ b/server.go @@ -922,7 +922,7 @@ func (s *Server) processUnaryRPC(t transport.ServerTransport, stream *transport. appStatus, ok := status.FromError(appErr) if !ok { // Convert appErr if it is not a grpc status error. - appErr = status.Error(convertCode(appErr), appErr.Error()) + appErr = status.Error(codes.Unknown, appErr.Error()) appStatus, _ = status.FromError(appErr) } if trInfo != nil { @@ -1065,7 +1065,7 @@ func (s *Server) processStreamingRPC(t transport.ServerTransport, stream *transp case transport.StreamError: appStatus = status.New(err.Code, err.Desc) default: - appStatus = status.New(convertCode(appErr), appErr.Error()) + appStatus = status.New(codes.Unknown, appErr.Error()) } appErr = appStatus.Err() } diff --git a/stream.go b/stream.go index deb735927288..2af07a5386da 100644 --- a/stream.go +++ b/stream.go @@ -33,10 +33,13 @@ import ( "google.golang.org/grpc/stats" "google.golang.org/grpc/status" "google.golang.org/grpc/transport" -) +); // StreamHandler defines the handler called by gRPC server to complete the -// execution of a streaming RPC. +// execution of a streaming RPC. If a StreamHandler returns an error, it +// should be produced by the status package, or else gRPC will use +// codes.Unknown as the status code and err.Error() as the status message +// of the RPC. type StreamHandler func(srv interface{}, stream ServerStream) error // StreamDesc represents a streaming RPC service's method specification.