Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKarthik Nayak <knayak@gitlab.com>2023-11-29 20:34:31 +0300
committerKarthik Nayak <knayak@gitlab.com>2023-11-30 18:15:59 +0300
commit232812c92f18f03372f73284c079d90e3c532923 (patch)
treef1e629ceee369b9d50fe8c415009add010f28e52
parent966b9a43ac2ea0dc6a03bc3836597b0e4c2bbb75 (diff)
errors: Use `errors.Is()` instead of switch on errors
Currently in many places of our codebase, we have: switch err { case ErrMissingLeaseFile: c.errTotal.WithLabelValues("ErrMissingLeaseFile").Inc() case ErrPendingExists: c.errTotal.WithLabelValues("ErrPendingExists").Inc() } while this works, its not compatible with wrapped errors. So let's fix this by using `errors.Is()` instead.
-rw-r--r--internal/cache/diskcache.go6
-rw-r--r--internal/gitaly/diff/diff.go6
-rw-r--r--internal/gitaly/service/commit/languages.go5
-rw-r--r--internal/gitaly/service/operations/commit_files.go17
-rw-r--r--internal/gitaly/service/smarthttp/cache.go7
-rw-r--r--internal/limiter/concurrency_limiter.go6
-rw-r--r--internal/testhelper/testserver/praefect.go4
7 files changed, 26 insertions, 25 deletions
diff --git a/internal/cache/diskcache.go b/internal/cache/diskcache.go
index 8046ae3c5..b442b75df 100644
--- a/internal/cache/diskcache.go
+++ b/internal/cache/diskcache.go
@@ -209,10 +209,10 @@ func (c *DiskCache) Collect(metrics chan<- prometheus.Metric) {
}
func (c *DiskCache) countErr(err error) error {
- switch err {
- case ErrMissingLeaseFile:
+ switch {
+ case errors.Is(err, ErrMissingLeaseFile):
c.errTotal.WithLabelValues("ErrMissingLeaseFile").Inc()
- case ErrPendingExists:
+ case errors.Is(err, ErrPendingExists):
c.errTotal.WithLabelValues("ErrPendingExists").Inc()
}
return err
diff --git a/internal/gitaly/diff/diff.go b/internal/gitaly/diff/diff.go
index 159f32eed..b5a535a70 100644
--- a/internal/gitaly/diff/diff.go
+++ b/internal/gitaly/diff/diff.go
@@ -453,10 +453,10 @@ func (parser *Parser) consumeChunkLine(updateLineStats bool) {
line, err = parser.patchReader.ReadSlice('\n')
n += len(line)
- switch err {
- case io.EOF, nil:
+ switch {
+ case err == nil || errors.Is(err, io.EOF):
done = true
- case bufio.ErrBufferFull:
+ case errors.Is(err, bufio.ErrBufferFull):
// long line: keep reading
default:
parser.err = fmt.Errorf("read chunk line: %w", err)
diff --git a/internal/gitaly/service/commit/languages.go b/internal/gitaly/service/commit/languages.go
index 4248d44ea..5e0944981 100644
--- a/internal/gitaly/service/commit/languages.go
+++ b/internal/gitaly/service/commit/languages.go
@@ -92,8 +92,7 @@ func (ls languageSorter) Less(i, j int) bool { return ls[i].Share > ls[j].Share
func (s *server) lookupRevision(ctx context.Context, repo git.RepositoryExecutor, revision string) (string, error) {
rev, err := s.checkRevision(ctx, repo, revision)
if err != nil {
- switch err {
- case errAmbigRef:
+ if errors.Is(err, errAmbigRef) {
fullRev, err := s.disambiguateRevision(ctx, repo, revision)
if err != nil {
return "", err
@@ -103,7 +102,7 @@ func (s *server) lookupRevision(ctx context.Context, repo git.RepositoryExecutor
if err != nil {
return "", err
}
- default:
+ } else {
return "", err
}
}
diff --git a/internal/gitaly/service/operations/commit_files.go b/internal/gitaly/service/operations/commit_files.go
index 6ef7fcecf..46bbd1a92 100644
--- a/internal/gitaly/service/operations/commit_files.go
+++ b/internal/gitaly/service/operations/commit_files.go
@@ -372,28 +372,29 @@ func applyAction(
// translateLocalrepoError converts errors returned by the `localrepo` package into nice errors that we can return to the caller.
// Most importantly, these errors will carry metadata that helps to figure out what exactly has gone wrong.
func translateError(err error, path string) error {
- switch err {
- case localrepo.ErrEntryNotFound, localrepo.ErrObjectNotFound:
+ switch {
+ case errors.Is(err, localrepo.ErrEntryNotFound) ||
+ errors.Is(err, localrepo.ErrObjectNotFound):
return indexError{
path: path,
errorType: errFileNotFound,
}
- case localrepo.ErrEmptyPath,
- localrepo.ErrPathTraversal,
- localrepo.ErrAbsolutePath,
- localrepo.ErrDisallowedPath:
+ case errors.Is(err, localrepo.ErrEmptyPath) ||
+ errors.Is(err, localrepo.ErrPathTraversal) ||
+ errors.Is(err, localrepo.ErrAbsolutePath) ||
+ errors.Is(err, localrepo.ErrDisallowedPath):
//The error coming back from git2go has the path in single
//quotes. This is to match the git2go error for now.
//nolint:gitaly-linters
return unknownIndexError(
fmt.Sprintf("invalid path: '%s'", path),
)
- case localrepo.ErrPathTraversal:
+ case errors.Is(err, localrepo.ErrPathTraversal):
return indexError{
path: path,
errorType: errDirectoryTraversal,
}
- case localrepo.ErrEntryExists:
+ case errors.Is(err, localrepo.ErrEntryExists):
return indexError{
path: path,
errorType: errFileExists,
diff --git a/internal/gitaly/service/smarthttp/cache.go b/internal/gitaly/service/smarthttp/cache.go
index df51f9a17..515029c0f 100644
--- a/internal/gitaly/service/smarthttp/cache.go
+++ b/internal/gitaly/service/smarthttp/cache.go
@@ -2,6 +2,7 @@ package smarthttp
import (
"context"
+ "errors"
"io"
"sync"
@@ -68,8 +69,8 @@ func (c infoRefCache) tryCache(ctx context.Context, in *gitalypb.InfoRefsRequest
})
stream, err := c.streamer.GetStream(ctx, in.Repository, in)
- switch err {
- case nil:
+ switch {
+ case err == nil:
defer stream.Close()
countHit()
@@ -81,7 +82,7 @@ func (c infoRefCache) tryCache(ctx context.Context, in *gitalypb.InfoRefsRequest
return nil
- case cache.ErrReqNotFound:
+ case errors.Is(err, cache.ErrReqNotFound):
countMiss()
c.logger.InfoContext(ctx, "cache miss for InfoRefsUploadPack response")
diff --git a/internal/limiter/concurrency_limiter.go b/internal/limiter/concurrency_limiter.go
index 6e0b80007..7f23eb3ad 100644
--- a/internal/limiter/concurrency_limiter.go
+++ b/internal/limiter/concurrency_limiter.go
@@ -258,14 +258,14 @@ func (c *ConcurrencyLimiter) Limit(ctx context.Context, limitingKey string, f Li
if err := sem.acquire(ctx, limitingKey); err != nil {
queueTime := time.Since(start)
- switch err {
- case ErrMaxQueueSize:
+ switch {
+ case errors.Is(err, ErrMaxQueueSize):
c.monitor.Dropped(ctx, limitingKey, sem.queueLength(), sem.inProgress(), queueTime, "max_size")
return nil, structerr.NewResourceExhausted("%w", ErrMaxQueueSize).WithDetail(&gitalypb.LimitError{
ErrorMessage: err.Error(),
RetryAfter: durationpb.New(0),
})
- case ErrMaxQueueTime:
+ case errors.Is(err, ErrMaxQueueTime):
c.monitor.Dropped(ctx, limitingKey, sem.queueLength(), sem.inProgress(), queueTime, "max_time")
return nil, structerr.NewResourceExhausted("%w", ErrMaxQueueTime).WithDetail(&gitalypb.LimitError{
ErrorMessage: err.Error(),
diff --git a/internal/testhelper/testserver/praefect.go b/internal/testhelper/testserver/praefect.go
index 59331264c..6f1be2851 100644
--- a/internal/testhelper/testserver/praefect.go
+++ b/internal/testhelper/testserver/praefect.go
@@ -3,6 +3,7 @@ package testserver
import (
"bytes"
"context"
+ "errors"
"net"
"os"
"os/exec"
@@ -113,8 +114,7 @@ func StartPraefect(tb testing.TB, cfg config.Config) PraefectServer {
select {
case <-ctx.Done():
- switch ctx.Err() {
- case context.DeadlineExceeded:
+ if errors.Is(ctx.Err(), context.DeadlineExceeded) {
// Capture Praefect logs when waitHealthy takes too long.
require.FailNowf(tb, "Connecting to Praefect exceeded deadline", "%s", stderr.String())
}