Skip to content

Commit 9aba044

Browse files
jeanbzadfawley
authored andcommitted
server: Convert all non-status errors to codes.Unknown (#1881)
- convertCode utilized errors that were not allowed by the library per https://github.com/grpc/grpc/blob/9d0bc30edbe14fef58f32e74009dd513dee2cfd0/doc/statuscodes.md - Relevant issue: #1672
1 parent efcc755 commit 9aba044

File tree

5 files changed

+9
-62
lines changed

5 files changed

+9
-62
lines changed

go16.go

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"io"
2626
"net"
2727
"net/http"
28-
"os"
2928

3029
"golang.org/x/net/context"
3130
"google.golang.org/grpc/codes"
@@ -69,31 +68,3 @@ func toRPCErr(err error) error {
6968
}
7069
return status.Error(codes.Unknown, err.Error())
7170
}
72-
73-
// convertCode converts a standard Go error into its canonical code. Note that
74-
// this is only used to translate the error returned by the server applications.
75-
func convertCode(err error) codes.Code {
76-
switch err {
77-
case nil:
78-
return codes.OK
79-
case io.EOF:
80-
return codes.OutOfRange
81-
case io.ErrClosedPipe, io.ErrNoProgress, io.ErrShortBuffer, io.ErrShortWrite, io.ErrUnexpectedEOF:
82-
return codes.FailedPrecondition
83-
case os.ErrInvalid:
84-
return codes.InvalidArgument
85-
case context.Canceled:
86-
return codes.Canceled
87-
case context.DeadlineExceeded:
88-
return codes.DeadlineExceeded
89-
}
90-
switch {
91-
case os.IsExist(err):
92-
return codes.AlreadyExists
93-
case os.IsNotExist(err):
94-
return codes.NotFound
95-
case os.IsPermission(err):
96-
return codes.PermissionDenied
97-
}
98-
return codes.Unknown
99-
}

go17.go

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"io"
2727
"net"
2828
"net/http"
29-
"os"
3029

3130
netctx "golang.org/x/net/context"
3231
"google.golang.org/grpc/codes"
@@ -70,31 +69,3 @@ func toRPCErr(err error) error {
7069
}
7170
return status.Error(codes.Unknown, err.Error())
7271
}
73-
74-
// convertCode converts a standard Go error into its canonical code. Note that
75-
// this is only used to translate the error returned by the server applications.
76-
func convertCode(err error) codes.Code {
77-
switch err {
78-
case nil:
79-
return codes.OK
80-
case io.EOF:
81-
return codes.OutOfRange
82-
case io.ErrClosedPipe, io.ErrNoProgress, io.ErrShortBuffer, io.ErrShortWrite, io.ErrUnexpectedEOF:
83-
return codes.FailedPrecondition
84-
case os.ErrInvalid:
85-
return codes.InvalidArgument
86-
case context.Canceled, netctx.Canceled:
87-
return codes.Canceled
88-
case context.DeadlineExceeded, netctx.DeadlineExceeded:
89-
return codes.DeadlineExceeded
90-
}
91-
switch {
92-
case os.IsExist(err):
93-
return codes.AlreadyExists
94-
case os.IsNotExist(err):
95-
return codes.NotFound
96-
case os.IsPermission(err):
97-
return codes.PermissionDenied
98-
}
99-
return codes.Unknown
100-
}

interceptor.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ type UnaryServerInfo struct {
4848
}
4949

5050
// UnaryHandler defines the handler invoked by UnaryServerInterceptor to complete the normal
51-
// execution of a unary RPC.
51+
// execution of a unary RPC. If a UnaryHandler returns an error, it should be produced by the
52+
// status package, or else gRPC will use codes.Unknown as the status code and err.Error() as
53+
// the status message of the RPC.
5254
type UnaryHandler func(ctx context.Context, req interface{}) (interface{}, error)
5355

5456
// UnaryServerInterceptor provides a hook to intercept the execution of a unary RPC on the server. info

server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ func (s *Server) processUnaryRPC(t transport.ServerTransport, stream *transport.
922922
appStatus, ok := status.FromError(appErr)
923923
if !ok {
924924
// Convert appErr if it is not a grpc status error.
925-
appErr = status.Error(convertCode(appErr), appErr.Error())
925+
appErr = status.Error(codes.Unknown, appErr.Error())
926926
appStatus, _ = status.FromError(appErr)
927927
}
928928
if trInfo != nil {
@@ -1065,7 +1065,7 @@ func (s *Server) processStreamingRPC(t transport.ServerTransport, stream *transp
10651065
case transport.StreamError:
10661066
appStatus = status.New(err.Code, err.Desc)
10671067
default:
1068-
appStatus = status.New(convertCode(appErr), appErr.Error())
1068+
appStatus = status.New(codes.Unknown, appErr.Error())
10691069
}
10701070
appErr = appStatus.Err()
10711071
}

stream.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ import (
3636
)
3737

3838
// StreamHandler defines the handler called by gRPC server to complete the
39-
// execution of a streaming RPC.
39+
// execution of a streaming RPC. If a StreamHandler returns an error, it
40+
// should be produced by the status package, or else gRPC will use
41+
// codes.Unknown as the status code and err.Error() as the status message
42+
// of the RPC.
4043
type StreamHandler func(srv interface{}, stream ServerStream) error
4144

4245
// StreamDesc represents a streaming RPC service's method specification.

0 commit comments

Comments
 (0)