Skip to content

Commit

Permalink
Remove convertCode and replace usage with codes.Unknown
Browse files Browse the repository at this point in the history
- convertCode utilized errors that were not allowed by the library per https://github.com/grpc/grpc/blob/9d0bc30edbe14fef58f32e74009dd513dee2cfd0/doc/statuscodes.md
- Relevant issue: grpc#1672
  • Loading branch information
jeanbza committed Mar 6, 2018
1 parent 583a630 commit ed850f8
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 64 deletions.
29 changes: 0 additions & 29 deletions go16.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"io"
"net"
"net/http"
"os"

"golang.org/x/net/context"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -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
}
29 changes: 0 additions & 29 deletions go17.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"io"
"net"
"net/http"
"os"

netctx "golang.org/x/net/context"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -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
}
6 changes: 4 additions & 2 deletions interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
Expand Down
7 changes: 5 additions & 2 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit ed850f8

Please sign in to comment.