diff options
author | Ævar Arnfjörð Bjarmason <avarab@gmail.com> | 2020-12-15 18:34:02 +0300 |
---|---|---|
committer | Ævar Arnfjörð Bjarmason <avarab@gmail.com> | 2020-12-15 18:38:12 +0300 |
commit | 69aa55079fd2e1d2cfc348009682185fab441804 (patch) | |
tree | 5d4793a9376680646d458e6e5375e66af7ace89c | |
parent | 3604932a6ac3cf95c2548899e27b96b3dec96291 (diff) |
DEMO Error helper tests: show an actual behavior change in fmt v.s. non-fmt
Test with:
go test -v ./internal/gitaly/service/operations/ -run 'FailedUserDeleteTagRequestDueToValidation' -count=1
-rw-r--r-- | internal/gitaly/service/operations/tags.go | 22 | ||||
-rw-r--r-- | internal/helper/error.go | 29 |
2 files changed, 28 insertions, 23 deletions
diff --git a/internal/gitaly/service/operations/tags.go b/internal/gitaly/service/operations/tags.go index b951cb035..23433322c 100644 --- a/internal/gitaly/service/operations/tags.go +++ b/internal/gitaly/service/operations/tags.go @@ -8,6 +8,7 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/git" "gitlab.com/gitlab-org/gitaly/internal/gitaly/rubyserver" "gitlab.com/gitlab-org/gitaly/internal/metadata/featureflag" + "gitlab.com/gitlab-org/gitaly/internal/helper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -36,11 +37,28 @@ func (s *Server) UserDeleteTagRuby(ctx context.Context, req *gitalypb.UserDelete func (s *Server) UserDeleteTagGo(ctx context.Context, req *gitalypb.UserDeleteTagRequest) (*gitalypb.UserDeleteTagResponse, error) { if len(req.TagName) == 0 { - return nil, status.Errorf(codes.InvalidArgument, "empty tag name") + // This one gets a new Internal error code. Then try + // it with: + // + // git checkout 495a384d4 -- internal/helper/error.go + // + // And it does not, but the message is mangled to: + // + // rpc error: code = Internal desc = empty user + return nil, helper.ErrInvalidArgument(status.Error(codes.Internal, "empty tag name")) } if req.User == nil { - return nil, status.Errorf(codes.InvalidArgument, "empty user") + // This does not get a new error code, but the message + // is mangled to: + // + // rpc error: code = Internal desc = empty user + // + // git checkout 495a384d4 -- internal/helper/error.go + // + // And the message is the same, but we mangle the + // error code to InvalidArgument. + return nil, helper.ErrInvalidArgumentf("%w", status.Error(codes.Internal, "empty user")) } referenceName := fmt.Sprintf("refs/tags/%s", req.TagName) diff --git a/internal/helper/error.go b/internal/helper/error.go index d8580a534..db0f9703c 100644 --- a/internal/helper/error.go +++ b/internal/helper/error.go @@ -8,26 +8,13 @@ import ( ) // Unimplemented is a Go error with gRPC error code 'Unimplemented' -var Unimplemented = status.Error(codes.Unimplemented, "this rpc is not implemented") - -type statusWrapper struct { - error - status *status.Status -} - -func (sw statusWrapper) GRPCStatus() *status.Status { - return sw.status -} - -func (sw statusWrapper) Unwrap() error { - return sw.error -} +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 && GrpcCode(err) == codes.Unknown { - return statusWrapper{err, status.New(code, err.Error())} + return status.Errorf(code, "%v", err) } return err } @@ -35,25 +22,25 @@ func DecorateError(code codes.Code, err error) error { // ErrInternal wraps err with codes.Internal, unless err is already a grpc error func ErrInternal(err error) error { return DecorateError(codes.Internal, err) } -// ErrInternalf wrapps a formatted error with codes.Internal, clobbering any existing grpc error +// ErrInternalf wrapps a formatted error with codes.Internal, unless err is already a grpc error func ErrInternalf(format string, a ...interface{}) error { - return ErrInternal(fmt.Errorf(format, a...)) + return DecorateError(codes.Internal, fmt.Errorf(format, a...)) } // ErrInvalidArgument wraps err with codes.InvalidArgument, unless err is already a grpc error func ErrInvalidArgument(err error) error { return DecorateError(codes.InvalidArgument, err) } -// ErrInvalidArgumentf wraps a formatted error with codes.InvalidArgument, clobbering any existing grpc error +// ErrInvalidArgumentf wraps a formatted error with codes.InvalidArgument, unless err is already a grpc error func ErrInvalidArgumentf(format string, a ...interface{}) error { - return ErrInvalidArgument(fmt.Errorf(format, a...)) + return DecorateError(codes.InvalidArgument, fmt.Errorf(format, a...)) } // ErrPreconditionFailed wraps err with codes.FailedPrecondition, unless err is already a grpc error func ErrPreconditionFailed(err error) error { return DecorateError(codes.FailedPrecondition, err) } -// ErrPreconditionFailedf wraps a formatted error with codes.FailedPrecondition, clobbering any existing grpc error +// ErrPreconditionFailedf wraps a formatted error with codes.FailedPrecondition, unless err is already a grpc error func ErrPreconditionFailedf(format string, a ...interface{}) error { - return ErrPreconditionFailed(fmt.Errorf(format, a...)) + return DecorateError(codes.FailedPrecondition, fmt.Errorf(format, a...)) } // ErrNotFound wraps error with codes.NotFound, unless err is already a grpc error |