Add grpc methods to errdefs

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby
2017-11-10 12:09:48 -05:00
parent c81788b129
commit 8376b50b19
3 changed files with 24 additions and 7 deletions

View File

@@ -4,7 +4,6 @@ import (
"strings"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
@@ -61,7 +60,7 @@ func FromGRPC(err error) error {
var cls error // divide these into error classes, becomes the cause
switch grpc.Code(err) {
switch code(err) {
case codes.InvalidArgument:
cls = ErrInvalidArgument
case codes.AlreadyExists:
@@ -94,7 +93,7 @@ func FromGRPC(err error) error {
// Effectively, we just remove the string of cls from the end of err if it
// appears there.
func rebaseMessage(cls error, err error) string {
desc := grpc.ErrorDesc(err)
desc := errDesc(err)
clss := cls.Error()
if desc == clss {
return ""
@@ -107,3 +106,17 @@ func isGRPCError(err error) bool {
_, ok := status.FromError(err)
return ok
}
func code(err error) codes.Code {
if s, ok := status.FromError(err); ok {
return s.Code()
}
return codes.Unknown
}
func errDesc(err error) string {
if s, ok := status.FromError(err); ok {
return s.Message()
}
return err.Error()
}