diff options
author | Jacob Vosmaer (GitLab) <jacob@gitlab.com> | 2018-01-18 22:58:46 +0300 |
---|---|---|
committer | Kim Carlbäcker <kim.carlbacker@gmail.com> | 2018-01-18 22:58:46 +0300 |
commit | 76c862ba55eab6047367122665a69562b064a816 (patch) | |
tree | ee6aed5d8b2b244b17801f38fc48718cdce1fbc6 /internal/helper | |
parent | a613c884192059321f28d17aec5aa811ecad2ffc (diff) |
Use grpc-go 1.9.1
Diffstat (limited to 'internal/helper')
-rw-r--r-- | internal/helper/error.go | 22 | ||||
-rw-r--r-- | internal/helper/repo.go | 14 |
2 files changed, 25 insertions, 11 deletions
diff --git a/internal/helper/error.go b/internal/helper/error.go index 604be7764..4966b788d 100644 --- a/internal/helper/error.go +++ b/internal/helper/error.go @@ -1,18 +1,32 @@ package helper import ( - "google.golang.org/grpc" "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" ) // Unimplemented is a Go error with gRPC error code 'Unimplemented' -var Unimplemented = grpc.Errorf(codes.Unimplemented, "this rpc is not implemented") +var Unimplemented = status.Errorf(codes.Unimplemented, "this rpc is not implemented") // DecorateError unless it's already a grpc error. // If given nil it will return nil. func DecorateError(code codes.Code, err error) error { - if err != nil && grpc.Code(err) == codes.Unknown { - return grpc.Errorf(code, "%v", err) + if err != nil && GrpcCode(err) == codes.Unknown { + return status.Errorf(code, "%v", err) } return err } + +// GrpcCode emulates the old grpc.Code function: it translates errors into codes.Code values. +func GrpcCode(err error) codes.Code { + if err == nil { + return codes.OK + } + + st, ok := status.FromError(err) + if !ok { + return codes.Unknown + } + + return st.Code() +} diff --git a/internal/helper/repo.go b/internal/helper/repo.go index 326088814..a7348075a 100644 --- a/internal/helper/repo.go +++ b/internal/helper/repo.go @@ -9,8 +9,8 @@ import ( pb "gitlab.com/gitlab-org/gitaly-proto/go" - "google.golang.org/grpc" "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" ) // GetRepoPath returns the full path of the repository referenced by an @@ -24,14 +24,14 @@ func GetRepoPath(repo *pb.Repository) (string, error) { } if repoPath == "" { - return "", grpc.Errorf(codes.InvalidArgument, "GetRepoPath: empty repo") + return "", status.Errorf(codes.InvalidArgument, "GetRepoPath: empty repo") } if IsGitDirectory(repoPath) { return repoPath, nil } - return "", grpc.Errorf(codes.NotFound, "GetRepoPath: not a git repository '%s'", repoPath) + return "", status.Errorf(codes.NotFound, "GetRepoPath: not a git repository '%s'", repoPath) } // GetPath returns the path of the repo passed as first argument. An error is @@ -44,12 +44,12 @@ func GetPath(repo *pb.Repository) (string, error) { } if _, err := os.Stat(storagePath); err != nil { - return "", grpc.Errorf(codes.Internal, "GetPath: storage path: %v", err) + return "", status.Errorf(codes.Internal, "GetPath: storage path: %v", err) } relativePath := repo.GetRelativePath() if len(relativePath) == 0 { - err := grpc.Errorf(codes.InvalidArgument, "GetPath: relative path missing from %+v", repo) + err := status.Errorf(codes.InvalidArgument, "GetPath: relative path missing from %+v", repo) return "", err } @@ -58,7 +58,7 @@ func GetPath(repo *pb.Repository) (string, error) { if strings.HasPrefix(relativePath, ".."+separator) || strings.Contains(relativePath, separator+".."+separator) || strings.HasSuffix(relativePath, separator+"..") { - return "", grpc.Errorf(codes.InvalidArgument, "GetRepoPath: relative path can't contain directory traversal") + return "", status.Errorf(codes.InvalidArgument, "GetRepoPath: relative path can't contain directory traversal") } return path.Join(storagePath, relativePath), nil @@ -69,7 +69,7 @@ func GetPath(repo *pb.Repository) (string, error) { func GetStorageByName(storageName string) (string, error) { storagePath, ok := config.StoragePath(storageName) if !ok { - return "", grpc.Errorf(codes.InvalidArgument, "Storage can not be found by name '%s'", storageName) + return "", status.Errorf(codes.InvalidArgument, "Storage can not be found by name '%s'", storageName) } return storagePath, nil |