diff options
author | Toon Claes <toon@gitlab.com> | 2022-12-14 17:25:40 +0300 |
---|---|---|
committer | Toon Claes <toon@gitlab.com> | 2022-12-14 17:25:40 +0300 |
commit | d5f73cd54b1ae3d9bdb21701b3f974e8421839b6 (patch) | |
tree | aa0102e801c03d893b266309a0d7df7221c3d86f | |
parent | be4a52de3497779aa112614dbad096504cfd07d7 (diff) | |
parent | 4d2f0d31e15f3a11d7b3659579b94f1987fd8967 (diff) |
Merge branch 'pks-helper-convert-errorf-pt1' into 'master'
helper: Convert errorf-style functions to use structerr package, pt. 1
See merge request https://gitlab.com/gitlab-org/gitaly/-/merge_requests/5167
Merged-by: Toon Claes <toon@gitlab.com>
Approved-by: Toon Claes <toon@gitlab.com>
Co-authored-by: Patrick Steinhardt <psteinhardt@gitlab.com>
24 files changed, 64 insertions, 134 deletions
diff --git a/internal/gitaly/service/blob/get_blob.go b/internal/gitaly/service/blob/get_blob.go index b43b24023..c25b6b618 100644 --- a/internal/gitaly/service/blob/get_blob.go +++ b/internal/gitaly/service/blob/get_blob.go @@ -8,6 +8,7 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/internal/git/catfile" "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/service" "gitlab.com/gitlab-org/gitaly/v15/internal/helper" + "gitlab.com/gitlab-org/gitaly/v15/internal/structerr" "gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb" "gitlab.com/gitlab-org/gitaly/v15/streamio" ) @@ -31,7 +32,7 @@ func (s *server) GetBlob(in *gitalypb.GetBlobRequest, stream gitalypb.BlobServic if err != nil { if catfile.IsNotFound(err) { if err := stream.Send(&gitalypb.GetBlobResponse{}); err != nil { - return helper.ErrUnavailablef("sending empty response: %w", err) + return structerr.NewUnavailable("sending empty response: %w", err) } return nil } @@ -40,7 +41,7 @@ func (s *server) GetBlob(in *gitalypb.GetBlobRequest, stream gitalypb.BlobServic if blob.Type != "blob" { if err := stream.Send(&gitalypb.GetBlobResponse{}); err != nil { - return helper.ErrUnavailablef("sending empty response: %w", err) + return structerr.NewUnavailable("sending empty response: %w", err) } return nil @@ -57,7 +58,7 @@ func (s *server) GetBlob(in *gitalypb.GetBlobRequest, stream gitalypb.BlobServic if readLimit == 0 { if err := stream.Send(firstMessage); err != nil { - return helper.ErrUnavailablef("sending empty blob: %w", err) + return structerr.NewUnavailable("sending empty blob: %w", err) } return nil @@ -75,7 +76,7 @@ func (s *server) GetBlob(in *gitalypb.GetBlobRequest, stream gitalypb.BlobServic _, err = io.CopyN(sw, blob, readLimit) if err != nil { - return helper.ErrUnavailablef("send: %w", err) + return structerr.NewUnavailable("send: %w", err) } return nil diff --git a/internal/gitaly/service/blob/get_blobs.go b/internal/gitaly/service/blob/get_blobs.go index a38fc90c2..799d89fb5 100644 --- a/internal/gitaly/service/blob/get_blobs.go +++ b/internal/gitaly/service/blob/get_blobs.go @@ -9,6 +9,7 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/internal/git/catfile" "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/service" "gitlab.com/gitlab-org/gitaly/v15/internal/helper" + "gitlab.com/gitlab-org/gitaly/v15/internal/structerr" "gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb" "gitlab.com/gitlab-org/gitaly/v15/streamio" ) @@ -46,7 +47,7 @@ func sendGetBlobsResponse( if treeEntry == nil || len(treeEntry.Oid) == 0 { if err := stream.Send(response); err != nil { - return helper.ErrUnavailablef("send: %w", err) + return structerr.NewUnavailable("send: %w", err) } continue @@ -60,7 +61,7 @@ func sendGetBlobsResponse( response.Type = gitalypb.ObjectType_COMMIT if err := stream.Send(response); err != nil { - return helper.ErrUnavailablef("send: %w", err) + return structerr.NewUnavailable("send: %w", err) } continue @@ -82,7 +83,7 @@ func sendGetBlobsResponse( if response.Type != gitalypb.ObjectType_BLOB { if err := stream.Send(response); err != nil { - return helper.ErrUnavailablef("send: %w", err) + return structerr.NewUnavailable("send: %w", err) } continue } @@ -115,7 +116,7 @@ func sendBlobTreeEntry( // blobObj. if readLimit == 0 { if err := stream.Send(response); err != nil { - return helper.ErrUnavailablef("send: %w", err) + return structerr.NewUnavailable("send: %w", err) } return nil } @@ -147,7 +148,7 @@ func sendBlobTreeEntry( _, err = io.CopyN(sw, blobObj, readLimit) if err != nil { - return helper.ErrUnavailablef("send: %w", err) + return structerr.NewUnavailable("send: %w", err) } return nil diff --git a/internal/gitaly/service/commit/tree_entry.go b/internal/gitaly/service/commit/tree_entry.go index 74827124e..cef7662e6 100644 --- a/internal/gitaly/service/commit/tree_entry.go +++ b/internal/gitaly/service/commit/tree_entry.go @@ -59,7 +59,7 @@ func sendTreeEntry( } if err := stream.Send(response); err != nil { - return helper.ErrUnavailablef("sending response: %w", err) + return structerr.NewUnavailable("sending response: %w", err) } return nil @@ -98,7 +98,7 @@ func sendTreeEntry( } if dataLength == 0 { if err := stream.Send(response); err != nil { - return helper.ErrUnavailablef("sending response: %w", err) + return structerr.NewUnavailable("sending response: %w", err) } return nil diff --git a/internal/gitaly/service/hook/pack_objects.go b/internal/gitaly/service/hook/pack_objects.go index 9730b3337..58eed6970 100644 --- a/internal/gitaly/service/hook/pack_objects.go +++ b/internal/gitaly/service/hook/pack_objects.go @@ -25,6 +25,7 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/internal/helper" "gitlab.com/gitlab-org/gitaly/v15/internal/metadata/featureflag" "gitlab.com/gitlab-org/gitaly/v15/internal/stream" + "gitlab.com/gitlab-org/gitaly/v15/internal/structerr" "gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb" "google.golang.org/protobuf/encoding/protojson" ) @@ -393,7 +394,7 @@ func (s *server) PackObjectsHookWithSidechannel(ctx context.Context, req *gitaly // EPIPE is the error we get if we try to write to c after the client has // closed its side of the connection. By convention, we label server side // errors caused by the client disconnecting with the Canceled gRPC code. - err = helper.ErrCanceledf("%w", err) + err = structerr.NewCanceled("%w", err) } return nil, helper.ErrInternalf("pack objects hook: %w", err) } diff --git a/internal/gitaly/service/hook/reference_transaction.go b/internal/gitaly/service/hook/reference_transaction.go index 68e64982f..16bef56b9 100644 --- a/internal/gitaly/service/hook/reference_transaction.go +++ b/internal/gitaly/service/hook/reference_transaction.go @@ -7,6 +7,7 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/service" "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/transaction" "gitlab.com/gitlab-org/gitaly/v15/internal/helper" + "gitlab.com/gitlab-org/gitaly/v15/internal/structerr" "gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb" "gitlab.com/gitlab-org/gitaly/v15/streamio" ) @@ -50,7 +51,7 @@ func (s *server) ReferenceTransactionHook(stream gitalypb.HookService_ReferenceT ); err != nil { switch { case errors.Is(err, transaction.ErrTransactionAborted): - return helper.ErrAbortedf("reference-transaction hook: %w", err) + return structerr.NewAborted("reference-transaction hook: %w", err) case errors.Is(err, transaction.ErrTransactionStopped): return helper.ErrFailedPreconditionf("reference-transaction hook: %w", err) default: diff --git a/internal/gitaly/service/operations/squash.go b/internal/gitaly/service/operations/squash.go index 5219bc579..9cdb260be 100644 --- a/internal/gitaly/service/operations/squash.go +++ b/internal/gitaly/service/operations/squash.go @@ -188,7 +188,7 @@ func (s *Server) userSquash(ctx context.Context, req *gitalypb.UserSquashRequest voting.VoteFromData([]byte(commitID)), voting.Prepared, ); err != nil { - return "", helper.ErrAbortedf("preparatory vote on squashed commit: %w", err) + return "", structerr.NewAborted("preparatory vote on squashed commit: %w", err) } if err := quarantineDir.Migrate(); err != nil { @@ -201,7 +201,7 @@ func (s *Server) userSquash(ctx context.Context, req *gitalypb.UserSquashRequest voting.VoteFromData([]byte(commitID)), voting.Committed, ); err != nil { - return "", helper.ErrAbortedf("committing vote on squashed commit: %w", err) + return "", structerr.NewAborted("committing vote on squashed commit: %w", err) } return commitID, nil diff --git a/internal/gitaly/service/operations/squash_test.go b/internal/gitaly/service/operations/squash_test.go index f3cd29017..4d24537ab 100644 --- a/internal/gitaly/service/operations/squash_test.go +++ b/internal/gitaly/service/operations/squash_test.go @@ -18,6 +18,7 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/internal/helper" "gitlab.com/gitlab-org/gitaly/v15/internal/helper/text" "gitlab.com/gitlab-org/gitaly/v15/internal/metadata" + "gitlab.com/gitlab-org/gitaly/v15/internal/structerr" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper/testserver" "gitlab.com/gitlab-org/gitaly/v15/internal/transaction/txinfo" @@ -133,7 +134,7 @@ func TestUserSquash_transactional(t *testing.T) { voteFn: func(ctx context.Context, tx txinfo.Transaction, vote voting.Vote, phase voting.Phase) error { return errors.New("vote failed") }, - expectedErr: helper.ErrAbortedf("preparatory vote on squashed commit: vote failed"), + expectedErr: structerr.NewAborted("preparatory vote on squashed commit: vote failed"), expectedVotes: []voting.Vote{ squashedCommitVote, }, @@ -147,7 +148,7 @@ func TestUserSquash_transactional(t *testing.T) { } return nil }, - expectedErr: helper.ErrAbortedf("committing vote on squashed commit: vote failed"), + expectedErr: structerr.NewAborted("committing vote on squashed commit: vote failed"), expectedVotes: []voting.Vote{ squashedCommitVote, squashedCommitVote, diff --git a/internal/gitaly/service/operations/tags.go b/internal/gitaly/service/operations/tags.go index 44b1e22eb..a3290ab3b 100644 --- a/internal/gitaly/service/operations/tags.go +++ b/internal/gitaly/service/operations/tags.go @@ -266,7 +266,7 @@ func (s *Server) createTag( if err != nil { var FormatTagError localrepo.FormatTagError if errors.As(err, &FormatTagError) { - return nil, "", helper.ErrUnknownf("Rugged::InvalidError: failed to parse signature - expected prefix doesn't match actual") + return nil, "", structerr.NewUnknown("Rugged::InvalidError: failed to parse signature - expected prefix doesn't match actual") } var MktagError localrepo.MktagError diff --git a/internal/gitaly/service/repository/apply_gitattributes_test.go b/internal/gitaly/service/repository/apply_gitattributes_test.go index bcbd6b786..3c76e6b26 100644 --- a/internal/gitaly/service/repository/apply_gitattributes_test.go +++ b/internal/gitaly/service/repository/apply_gitattributes_test.go @@ -13,6 +13,7 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/internal/git/gittest" "gitlab.com/gitlab-org/gitaly/v15/internal/helper" "gitlab.com/gitlab-org/gitaly/v15/internal/metadata" + "gitlab.com/gitlab-org/gitaly/v15/internal/structerr" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper/testcfg" "gitlab.com/gitlab-org/gitaly/v15/internal/transaction/txinfo" @@ -163,7 +164,7 @@ func TestApplyGitattributes_transactional(t *testing.T) { }, shouldExist: false, expectedErr: func() error { - return helper.ErrUnknownf("committing gitattributes: voting on locked file: preimage vote: rpc error: code = Unknown desc = foobar") + return structerr.NewUnknown("committing gitattributes: voting on locked file: preimage vote: rpc error: code = Unknown desc = foobar") }(), expectedVotes: 1, }, diff --git a/internal/gitaly/service/repository/calculate_checksum.go b/internal/gitaly/service/repository/calculate_checksum.go index 3e02712ac..83b81a5a9 100644 --- a/internal/gitaly/service/repository/calculate_checksum.go +++ b/internal/gitaly/service/repository/calculate_checksum.go @@ -10,6 +10,7 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/internal/git" "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/service" "gitlab.com/gitlab-org/gitaly/v15/internal/helper" + "gitlab.com/gitlab-org/gitaly/v15/internal/structerr" "gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb" ) @@ -45,7 +46,7 @@ func (s *server) CalculateChecksum(ctx context.Context, in *gitalypb.CalculateCh return &gitalypb.CalculateChecksumResponse{Checksum: git.ZeroChecksum}, nil } - return nil, helper.ErrDataLossf("not a git repository '%s'", repoPath) + return nil, structerr.NewDataLoss("not a git repository '%s'", repoPath) } return &gitalypb.CalculateChecksumResponse{Checksum: hex.EncodeToString(checksum.Bytes())}, nil diff --git a/internal/gitaly/service/repository/create_repository_test.go b/internal/gitaly/service/repository/create_repository_test.go index 93bdf6074..3241f56f7 100644 --- a/internal/gitaly/service/repository/create_repository_test.go +++ b/internal/gitaly/service/repository/create_repository_test.go @@ -18,6 +18,7 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/internal/helper/text" "gitlab.com/gitlab-org/gitaly/v15/internal/metadata" "gitlab.com/gitlab-org/gitaly/v15/internal/praefect/praefectutil" + "gitlab.com/gitlab-org/gitaly/v15/internal/structerr" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper/testcfg" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper/testserver" @@ -41,7 +42,7 @@ func TestCreateRepository_missingAuth(t *testing.T) { RelativePath: gittest.NewRepositoryName(t), }, }) - testhelper.RequireGrpcError(t, helper.ErrUnauthenticatedf("authentication required"), err) + testhelper.RequireGrpcError(t, structerr.NewUnauthenticated("authentication required"), err) } func TestCreateRepository_successful(t *testing.T) { diff --git a/internal/gitaly/service/repository/fetch_remote.go b/internal/gitaly/service/repository/fetch_remote.go index c1d50f796..05ee07307 100644 --- a/internal/gitaly/service/repository/fetch_remote.go +++ b/internal/gitaly/service/repository/fetch_remote.go @@ -12,6 +12,7 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/service" "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/transaction" "gitlab.com/gitlab-org/gitaly/v15/internal/helper" + "gitlab.com/gitlab-org/gitaly/v15/internal/structerr" "gitlab.com/gitlab-org/gitaly/v15/internal/transaction/txinfo" "gitlab.com/gitlab-org/gitaly/v15/internal/transaction/voting" "gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb" @@ -125,7 +126,7 @@ func (s *server) FetchRemote(ctx context.Context, req *gitalypb.FetchRemoteReque return s.txManager.Vote(ctx, tx, vote, voting.UnknownPhase) }); err != nil { - return nil, helper.ErrAbortedf("failed vote on refs: %w", err) + return nil, structerr.NewAborted("failed vote on refs: %w", err) } out := &gitalypb.FetchRemoteResponse{TagsChanged: true} diff --git a/internal/gitaly/service/smarthttp/receive_pack.go b/internal/gitaly/service/smarthttp/receive_pack.go index a44469f5c..1fef1da64 100644 --- a/internal/gitaly/service/smarthttp/receive_pack.go +++ b/internal/gitaly/service/smarthttp/receive_pack.go @@ -9,6 +9,7 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/service" "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/transaction" "gitlab.com/gitlab-org/gitaly/v15/internal/helper" + "gitlab.com/gitlab-org/gitaly/v15/internal/structerr" "gitlab.com/gitlab-org/gitaly/v15/internal/transaction/voting" "gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb" "gitlab.com/gitlab-org/gitaly/v15/streamio" @@ -63,11 +64,11 @@ func (s *server) PostReceivePack(stream gitalypb.SmartHTTPService_PostReceivePac git.WithConfig(config...), ) if err != nil { - return helper.ErrUnavailablef("spawning receive-pack: %w", err) + return structerr.NewUnavailable("spawning receive-pack: %w", err) } if err := cmd.Wait(); err != nil { - return helper.ErrUnavailablef("waiting for receive-pack: %w", err) + return structerr.NewUnavailable("waiting for receive-pack: %w", err) } // In cases where all reference updates are rejected by git-receive-pack(1), we would end up @@ -87,7 +88,7 @@ func (s *server) PostReceivePack(stream gitalypb.SmartHTTPService_PostReceivePac // To avoid this error being presented to the end user, ignore it when the // transaction was stopped. if !errors.Is(err, transaction.ErrTransactionStopped) { - return helper.ErrAbortedf("final transactional vote: %w", err) + return structerr.NewAborted("final transactional vote: %w", err) } } diff --git a/internal/gitaly/service/smarthttp/upload_pack.go b/internal/gitaly/service/smarthttp/upload_pack.go index af64f5b03..385b13264 100644 --- a/internal/gitaly/service/smarthttp/upload_pack.go +++ b/internal/gitaly/service/smarthttp/upload_pack.go @@ -13,6 +13,7 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/service" "gitlab.com/gitlab-org/gitaly/v15/internal/helper" "gitlab.com/gitlab-org/gitaly/v15/internal/sidechannel" + "gitlab.com/gitlab-org/gitaly/v15/internal/structerr" "gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb" "gitlab.com/gitlab-org/gitaly/v15/streamio" ) @@ -152,13 +153,13 @@ func (s *server) runUploadPack(ctx context.Context, req basicPostUploadPackReque Args: []string{repoPath}, }, commandOpts...) if err != nil { - return helper.ErrUnavailablef("spawning upload-pack: %w", err) + return structerr.NewUnavailable("spawning upload-pack: %w", err) } // Use a custom buffer size to minimize the number of system calls. respBytes, err := io.CopyBuffer(stdout, cmd, make([]byte, 64*1024)) if err != nil { - return helper.ErrUnavailablef("copying stdout from upload-pack: %w", err) + return structerr.NewUnavailable("copying stdout from upload-pack: %w", err) } if err := cmd.Wait(); err != nil { @@ -171,7 +172,7 @@ func (s *server) runUploadPack(ctx context.Context, req basicPostUploadPackReque return nil } - return helper.ErrUnavailablef("waiting for upload-pack: %w", err) + return structerr.NewUnavailable("waiting for upload-pack: %w", err) } ctxlogrus.Extract(ctx).WithField("request_sha", fmt.Sprintf("%x", h.Sum(nil))).WithField("response_bytes", respBytes).Info("request details") diff --git a/internal/gitaly/service/smarthttp/upload_pack_test.go b/internal/gitaly/service/smarthttp/upload_pack_test.go index a47938d7f..6b650e57b 100644 --- a/internal/gitaly/service/smarthttp/upload_pack_test.go +++ b/internal/gitaly/service/smarthttp/upload_pack_test.go @@ -23,6 +23,7 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/internal/git/pktline" "gitlab.com/gitlab-org/gitaly/v15/internal/helper" "gitlab.com/gitlab-org/gitaly/v15/internal/sidechannel" + "gitlab.com/gitlab-org/gitaly/v15/internal/structerr" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper/testcfg" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper/testserver" @@ -172,7 +173,7 @@ func testServerPostUploadPackGitConfigOptions(t *testing.T, ctx context.Context, }, } response, err := makeRequest(t, ctx, cfg.SocketPath, cfg.Auth.Token, rpcRequest, bytes.NewReader(requestBody.Bytes())) - testhelper.RequireGrpcError(t, helper.ErrUnavailablef("running upload-pack: waiting for upload-pack: exit status 128"), err) + testhelper.RequireGrpcError(t, structerr.NewUnavailable("running upload-pack: waiting for upload-pack: exit status 128"), err) // The failure message proves that upload-pack failed because of // GitConfigOptions, and that proves that passing GitConfigOptions works. diff --git a/internal/gitaly/service/ssh/receive_pack.go b/internal/gitaly/service/ssh/receive_pack.go index 0dc444564..27825f514 100644 --- a/internal/gitaly/service/ssh/receive_pack.go +++ b/internal/gitaly/service/ssh/receive_pack.go @@ -14,6 +14,7 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/service" "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/transaction" "gitlab.com/gitlab-org/gitaly/v15/internal/helper" + "gitlab.com/gitlab-org/gitaly/v15/internal/structerr" "gitlab.com/gitlab-org/gitaly/v15/internal/transaction/voting" "gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb" "gitlab.com/gitlab-org/gitaly/v15/streamio" @@ -111,7 +112,7 @@ func (s *server) sshReceivePack(stream gitalypb.SSHService_SSHReceivePackServer, // gRPC error code. We can't do anything about this error anyway and it is a totally // valid outcome. if stderrBuilder.String() == "fatal: the remote end hung up unexpectedly\n" { - return helper.ErrCanceledf("user canceled the push") + return structerr.NewCanceled("user canceled the push") } return fmt.Errorf("cmd wait: %w", err) @@ -130,7 +131,7 @@ func (s *server) sshReceivePack(stream gitalypb.SSHService_SSHReceivePackServer, // To avoid this error being presented to the end user, ignore it when the // transaction was stopped. if !errors.Is(err, transaction.ErrTransactionStopped) { - return helper.ErrAbortedf("final transactional vote: %w", err) + return structerr.NewAborted("final transactional vote: %w", err) } } diff --git a/internal/gitaly/service/ssh/receive_pack_test.go b/internal/gitaly/service/ssh/receive_pack_test.go index dda15f2c4..d7a9c176f 100644 --- a/internal/gitaly/service/ssh/receive_pack_test.go +++ b/internal/gitaly/service/ssh/receive_pack_test.go @@ -28,6 +28,7 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/internal/helper/text" "gitlab.com/gitlab-org/gitaly/v15/internal/metadata" "gitlab.com/gitlab-org/gitaly/v15/internal/metadata/featureflag" + "gitlab.com/gitlab-org/gitaly/v15/internal/structerr" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper/testcfg" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper/testserver" @@ -235,7 +236,7 @@ func TestReceivePack_client(t *testing.T) { writeRequest: func(t *testing.T, stdin io.Writer) { gittest.WritePktlinef(t, stdin, "%[1]s %[1]s refs/heads/main", gittest.DefaultObjectHash.ZeroOID) }, - expectedErr: helper.ErrCanceledf("user canceled the push"), + expectedErr: structerr.NewCanceled("user canceled the push"), expectedErrorCode: 128, expectedStderr: "fatal: the remote end hung up unexpectedly\n", }, diff --git a/internal/gitaly/service/ssh/upload_archive.go b/internal/gitaly/service/ssh/upload_archive.go index e85d03b90..44ae08fc5 100644 --- a/internal/gitaly/service/ssh/upload_archive.go +++ b/internal/gitaly/service/ssh/upload_archive.go @@ -11,6 +11,7 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/internal/git/pktline" "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/service" "gitlab.com/gitlab-org/gitaly/v15/internal/helper" + "gitlab.com/gitlab-org/gitaly/v15/internal/structerr" "gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb" "gitlab.com/gitlab-org/gitaly/v15/streamio" ) @@ -80,7 +81,7 @@ func (s *server) sshUploadArchive(stream gitalypb.SSHService_SSHUploadArchiveSer // We thus need to special-case the situation where we cancel our own context in // order to provide that information and return a proper gRPC error code. if ctx.Err() != nil && stream.Context().Err() == nil { - return helper.ErrDeadlineExceededf("waiting for packfile negotiation: %w", ctx.Err()) + return structerr.NewDeadlineExceeded("waiting for packfile negotiation: %w", ctx.Err()) } if status, ok := command.ExitStatus(err); ok { diff --git a/internal/gitaly/service/ssh/upload_archive_test.go b/internal/gitaly/service/ssh/upload_archive_test.go index 9e00db031..def70ee35 100644 --- a/internal/gitaly/service/ssh/upload_archive_test.go +++ b/internal/gitaly/service/ssh/upload_archive_test.go @@ -10,7 +10,7 @@ import ( "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/v15/internal/git/gittest" - "gitlab.com/gitlab-org/gitaly/v15/internal/helper" + "gitlab.com/gitlab-org/gitaly/v15/internal/structerr" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper/testcfg" "gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb" @@ -42,7 +42,7 @@ func TestFailedUploadArchiveRequestDueToTimeout(t *testing.T) { // Because the client says nothing, the server would block. Because of // the timeout, it won't block forever, and return with a non-zero exit // code instead. - requireFailedSSHStream(t, helper.ErrDeadlineExceededf("waiting for packfile negotiation: context canceled"), func() (int32, error) { + requireFailedSSHStream(t, structerr.NewDeadlineExceeded("waiting for packfile negotiation: context canceled"), func() (int32, error) { resp, err := stream.Recv() if err != nil { return 0, err diff --git a/internal/gitaly/service/ssh/upload_pack.go b/internal/gitaly/service/ssh/upload_pack.go index c1f0ca3a4..23e42ad7e 100644 --- a/internal/gitaly/service/ssh/upload_pack.go +++ b/internal/gitaly/service/ssh/upload_pack.go @@ -18,6 +18,7 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/internal/helper" "gitlab.com/gitlab-org/gitaly/v15/internal/sidechannel" "gitlab.com/gitlab-org/gitaly/v15/internal/stream" + "gitlab.com/gitlab-org/gitaly/v15/internal/structerr" "gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb" "gitlab.com/gitlab-org/gitaly/v15/streamio" ) @@ -158,7 +159,7 @@ func (s *server) sshUploadPack(rpcContext context.Context, req sshUploadPackRequ // We thus need to special-case the situation where we cancel our own context in // order to provide that information and return a proper gRPC error code. if ctx.Err() != nil && rpcContext.Err() == nil { - return status, helper.ErrDeadlineExceededf("waiting for packfile negotiation: %w", ctx.Err()) + return status, structerr.NewDeadlineExceeded("waiting for packfile negotiation: %w", ctx.Err()) } // A common error case is that the client is terminating the request prematurely, @@ -171,7 +172,7 @@ func (s *server) sshUploadPack(rpcContext context.Context, req sshUploadPackRequ // Note that we're being quite strict with how we match the error for now. We may // have to make it more lenient in case we see that this doesn't catch all cases. if stderrBuilder.String() == "fatal: the remote end hung up unexpectedly\n" { - return status, helper.ErrCanceledf("user canceled the fetch") + return status, structerr.NewCanceled("user canceled the fetch") } return status, fmt.Errorf("cmd wait: %w, stderr: %q", err, stderrBuilder.String()) @@ -204,7 +205,7 @@ func (rf *largeBufferReaderFrom) ReadFrom(r io.Reader) (int64, error) { func (s *server) SSHUploadPackWithSidechannel(ctx context.Context, req *gitalypb.SSHUploadPackWithSidechannelRequest) (*gitalypb.SSHUploadPackWithSidechannelResponse, error) { conn, err := sidechannel.OpenSidechannel(ctx) if err != nil { - return nil, helper.ErrUnavailablef("opennig sidechannel: %w", err) + return nil, structerr.NewUnavailable("opennig sidechannel: %w", err) } defer conn.Close() diff --git a/internal/gitaly/service/ssh/upload_pack_test.go b/internal/gitaly/service/ssh/upload_pack_test.go index 2fdfb9ae3..b5c42c28d 100644 --- a/internal/gitaly/service/ssh/upload_pack_test.go +++ b/internal/gitaly/service/ssh/upload_pack_test.go @@ -25,6 +25,7 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/internal/helper/text" "gitlab.com/gitlab-org/gitaly/v15/internal/metadata/featureflag" "gitlab.com/gitlab-org/gitaly/v15/internal/sidechannel" + "gitlab.com/gitlab-org/gitaly/v15/internal/structerr" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper/testcfg" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper/testserver" @@ -117,7 +118,7 @@ func testUploadPackTimeout(t *testing.T, opts ...testcfg.Option) { // Because the client says nothing, the server would block. Because of // the timeout, it won't block forever, and return with a non-zero exit // code instead. - requireFailedSSHStream(t, helper.ErrDeadlineExceededf("waiting for packfile negotiation: context canceled"), func() (int32, error) { + requireFailedSSHStream(t, structerr.NewDeadlineExceeded("waiting for packfile negotiation: context canceled"), func() (int32, error) { resp, err := stream.Recv() if err != nil { return 0, err @@ -304,7 +305,7 @@ func TestUploadPackWithSidechannel_client(t *testing.T) { return nil }, - expectedErr: helper.ErrCanceledf("user canceled the fetch"), + expectedErr: structerr.NewCanceled("user canceled the fetch"), }, { desc: "garbage", @@ -334,7 +335,7 @@ func TestUploadPackWithSidechannel_client(t *testing.T) { return nil }, - expectedErr: helper.ErrCanceledf("%w", context.Canceled), + expectedErr: structerr.NewCanceled("%w", context.Canceled), }, { desc: "cancellation and close", @@ -351,7 +352,7 @@ func TestUploadPackWithSidechannel_client(t *testing.T) { return nil }, - expectedErr: helper.ErrCanceledf("%w", context.Canceled), + expectedErr: structerr.NewCanceled("%w", context.Canceled), }, { desc: "cancellation without close", @@ -367,7 +368,7 @@ func TestUploadPackWithSidechannel_client(t *testing.T) { return nil }, - expectedErr: helper.ErrCanceledf("%w", context.Canceled), + expectedErr: structerr.NewCanceled("%w", context.Canceled), }, } { t.Run(tc.desc, func(t *testing.T) { diff --git a/internal/helper/error.go b/internal/helper/error.go index ee159314d..4dcd12de6 100644 --- a/internal/helper/error.go +++ b/internal/helper/error.go @@ -21,18 +21,6 @@ func (sw statusWrapper) Unwrap() error { return sw.error } -// ErrCanceledf wraps a formatted error with codes.Canceled, unless the formatted error is a -// wrapped gRPC error. -func ErrCanceledf(format string, a ...interface{}) error { - return formatError(codes.Canceled, format, a...) -} - -// ErrDeadlineExceededf wraps a formatted error with codes.DeadlineExceeded, unless the formatted -// error is a wrapped gRPC error. -func ErrDeadlineExceededf(format string, a ...interface{}) error { - return formatError(codes.DeadlineExceeded, format, a...) -} - // ErrInternalf wraps a formatted error with codes.Internal, unless the formatted error is a // wrapped gRPC error. func ErrInternalf(format string, a ...interface{}) error { @@ -57,12 +45,6 @@ func ErrFailedPreconditionf(format string, a ...interface{}) error { return formatError(codes.FailedPrecondition, format, a...) } -// ErrUnavailablef wraps a formatted error with codes.Unavailable, unless the -// formatted error is a wrapped gRPC error. -func ErrUnavailablef(format string, a ...interface{}) error { - return formatError(codes.Unavailable, format, a...) -} - // ErrPermissionDeniedf wraps a formatted error with codes.PermissionDenied, unless the formatted // error is a wrapped gRPC error. func ErrPermissionDeniedf(format string, a ...interface{}) error { @@ -75,36 +57,6 @@ func ErrAlreadyExistsf(format string, a ...interface{}) error { return formatError(codes.AlreadyExists, format, a...) } -// ErrAbortedf wraps a formatted error with codes.Aborted, unless the formatted error is a wrapped -// gRPC error. -func ErrAbortedf(format string, a ...interface{}) error { - return formatError(codes.Aborted, format, a...) -} - -// ErrDataLossf wraps a formatted error with codes.DataLoss, unless the formatted error is a wrapped -// gRPC error. -func ErrDataLossf(format string, a ...interface{}) error { - return formatError(codes.DataLoss, format, a...) -} - -// ErrUnknownf wraps a formatted error with codes.Unknown, unless the formatted error is a wrapped -// gRPC error. -func ErrUnknownf(format string, a ...interface{}) error { - return formatError(codes.Unknown, format, a...) -} - -// ErrUnimplementedf wraps a formatted error with codes.Unimplemented, unless the formatted error is a wrapped -// gRPC error. -func ErrUnimplementedf(format string, a ...interface{}) error { - return formatError(codes.Unimplemented, format, a...) -} - -// ErrUnauthenticatedf wraps a formatted error with codes.Unauthenticated, unless the formatted error is a wrapped -// gRPC error. -func ErrUnauthenticatedf(format string, a ...interface{}) error { - return formatError(codes.Unauthenticated, format, a...) -} - // grpcErrorMessageWrapper is used to wrap a gRPC `status.Status`-style error such that it behaves // like a `status.Status`, except that it generates a readable error message. type grpcErrorMessageWrapper struct { diff --git a/internal/helper/error_test.go b/internal/helper/error_test.go index 26dfdebad..7fa6cc59e 100644 --- a/internal/helper/error_test.go +++ b/internal/helper/error_test.go @@ -25,16 +25,6 @@ func TestErrorf(t *testing.T) { expectedCode codes.Code }{ { - desc: "Canceledf", - errorf: ErrCanceledf, - expectedCode: codes.Canceled, - }, - { - desc: "DeadlineExceededf", - errorf: ErrDeadlineExceededf, - expectedCode: codes.DeadlineExceeded, - }, - { desc: "Internalf", errorf: ErrInternalf, expectedCode: codes.Internal, @@ -54,36 +44,6 @@ func TestErrorf(t *testing.T) { errorf: ErrNotFoundf, expectedCode: codes.NotFound, }, - { - desc: "ErrUnavailablef", - errorf: ErrUnavailablef, - expectedCode: codes.Unavailable, - }, - { - desc: "ErrAbortedf", - errorf: ErrAbortedf, - expectedCode: codes.Aborted, - }, - { - desc: "ErrDataLossf", - errorf: ErrDataLossf, - expectedCode: codes.DataLoss, - }, - { - desc: "ErrUnknownf", - errorf: ErrUnknownf, - expectedCode: codes.Unknown, - }, - { - desc: "ErrUnimplementedf", - errorf: ErrUnimplementedf, - expectedCode: codes.Unimplemented, - }, - { - desc: "ErrUnauthenticatedf", - errorf: ErrUnauthenticatedf, - expectedCode: codes.Unauthenticated, - }, } { t.Run(tc.desc, func(t *testing.T) { t.Run("with non-gRPC error", func(t *testing.T) { @@ -241,7 +201,7 @@ func TestGrpcCode(t *testing.T) { exp: codes.NotFound, }, "double helper wrapped status": { - in: ErrAbortedf("outer: %w", fmt.Errorf("context: %w", ErrNotFoundf(""))), + in: ErrFailedPreconditionf("outer: %w", fmt.Errorf("context: %w", ErrNotFoundf(""))), exp: codes.NotFound, }, "nil input": { diff --git a/internal/praefect/service/transaction/server.go b/internal/praefect/service/transaction/server.go index 1e8aaa153..ca099d771 100644 --- a/internal/praefect/service/transaction/server.go +++ b/internal/praefect/service/transaction/server.go @@ -6,6 +6,7 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/internal/helper" "gitlab.com/gitlab-org/gitaly/v15/internal/praefect/transactions" + "gitlab.com/gitlab-org/gitaly/v15/internal/structerr" "gitlab.com/gitlab-org/gitaly/v15/internal/transaction/voting" "gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb" ) @@ -37,7 +38,7 @@ func (s *Server) VoteTransaction(ctx context.Context, in *gitalypb.VoteTransacti case errors.Is(err, transactions.ErrNotFound): return nil, helper.ErrNotFoundf("%w", err) case errors.Is(err, transactions.ErrTransactionCanceled): - return nil, helper.ErrCanceledf("%w", err) + return nil, structerr.NewCanceled("%w", err) case errors.Is(err, transactions.ErrTransactionStopped): return &gitalypb.VoteTransactionResponse{ State: gitalypb.VoteTransactionResponse_STOP, @@ -66,7 +67,7 @@ func (s *Server) StopTransaction(ctx context.Context, in *gitalypb.StopTransacti case errors.Is(err, transactions.ErrNotFound): return nil, helper.ErrNotFoundf("%w", err) case errors.Is(err, transactions.ErrTransactionCanceled): - return nil, helper.ErrCanceledf("%w", err) + return nil, structerr.NewCanceled("%w", err) case errors.Is(err, transactions.ErrTransactionStopped): return &gitalypb.StopTransactionResponse{}, nil default: |