Skip to content

Commit

Permalink
server: remaining errors.Is conversions for error equality and inequa…
Browse files Browse the repository at this point in the history
…lity checks

Signed-off-by: redwrasse <[email protected]>
  • Loading branch information
redwrasse committed Sep 24, 2024
1 parent d59fa33 commit f597415
Show file tree
Hide file tree
Showing 40 changed files with 78 additions and 97 deletions.
4 changes: 2 additions & 2 deletions client/internal/v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,10 @@ func (c *httpClusterClient) Do(ctx context.Context, act httpAction) (*http.Respo
resp, body, err = hc.Do(ctx, action)
if err != nil {
cerr.Errors = append(cerr.Errors, err)
if errors.Is(err, ctx.Err()) {
if err == ctx.Err() {
return nil, nil, ctx.Err()
}
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
if err == context.Canceled || err == context.DeadlineExceeded {
return nil, nil, err
}
} else if resp.StatusCode/100 == 5 {
Expand Down
10 changes: 5 additions & 5 deletions client/internal/v2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func TestSimpleHTTPClientDoNilRequest(t *testing.T) {
tr.errchan <- errors.New("fixture")

_, _, err := c.Do(context.Background(), &nilAction{})
if !errors.Is(err, ErrNoRequest) {
if err != ErrNoRequest {
t.Fatalf("expected non-nil error, got nil")
}
}
Expand Down Expand Up @@ -256,7 +256,7 @@ func TestSimpleHTTPClientDoCancelContextResponseBodyClosedWithBlockingBody(t *te
}()

_, _, err := c.Do(ctx, &fakeAction{})
if !errors.Is(err, context.Canceled) {
if err != context.Canceled {
t.Fatalf("expected %+v, got %+v", context.Canceled, err)
}

Expand Down Expand Up @@ -478,7 +478,7 @@ func TestHTTPClusterClientDoDeadlineExceedContext(t *testing.T) {

select {
case err := <-errc:
if !errors.Is(err, context.DeadlineExceeded) {
if err != context.DeadlineExceeded {
t.Errorf("err = %+v, want %+v", err, context.DeadlineExceeded)
}
case <-time.After(time.Second):
Expand Down Expand Up @@ -528,7 +528,7 @@ func TestHTTPClusterClientDoCanceledContext(t *testing.T) {

select {
case err := <-errc:
if !errors.Is(err, errFakeCancelContext) {
if err != errFakeCancelContext {
t.Errorf("err = %+v, want %+v", err, errFakeCancelContext)
}
case <-time.After(time.Second):
Expand Down Expand Up @@ -881,7 +881,7 @@ func TestHTTPClusterClientAutoSyncCancelContext(t *testing.T) {
cancel()

err = hc.AutoSync(ctx, time.Hour)
if !errors.Is(err, context.Canceled) {
if err != context.Canceled {
t.Fatalf("incorrect error value: want=%v got=%v", context.Canceled, err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion client/internal/v2/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ func (hw *httpWatcher) Next(ctx context.Context) (*Response, error) {

resp, err := unmarshalHTTPResponse(httpresp.StatusCode, httpresp.Header, body)
if err != nil {
if errors.Is(err, ErrEmptyBody) {
if err == ErrEmptyBody {
continue
}
return nil, err
Expand Down
3 changes: 1 addition & 2 deletions client/pkg/fileutil/lock_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package fileutil

import (
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -65,7 +64,7 @@ func ofdTryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error
flock := wrlck
if err = syscall.FcntlFlock(f.Fd(), unix.F_OFD_SETLK, &flock); err != nil {
f.Close()
if errors.Is(err, syscall.EWOULDBLOCK) {
if err == syscall.EWOULDBLOCK {
err = ErrLocked
}
return nil, err
Expand Down
3 changes: 1 addition & 2 deletions client/pkg/fileutil/lock_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package fileutil

import (
"errors"
"os"
"syscall"
)
Expand All @@ -36,7 +35,7 @@ func TryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
}
if err := syscall.FcntlFlock(f.Fd(), syscall.F_SETLK, &lock); err != nil {
f.Close()
if errors.Is(err, syscall.EAGAIN) {
if err == syscall.EAGAIN {
err = ErrLocked
}
return nil, err
Expand Down
3 changes: 1 addition & 2 deletions client/pkg/fileutil/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package fileutil

import (
"errors"
"os"
"testing"
"time"
Expand All @@ -41,7 +40,7 @@ func TestLockAndUnlock(t *testing.T) {
}

// try lock a locked file
if _, err = TryLockFile(f.Name(), os.O_WRONLY, PrivateFileMode); !errors.Is(err, ErrLocked) {
if _, err = TryLockFile(f.Name(), os.O_WRONLY, PrivateFileMode); err != ErrLocked {
t.Fatal(err)
}

Expand Down
4 changes: 2 additions & 2 deletions client/pkg/fileutil/lock_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ func lockFile(fd windows.Handle, flags uint32) error {
err := windows.LockFileEx(fd, flags|windows.LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &windows.Overlapped{})
if err == nil {
return nil
} else if errors.Is(err, errLocked) {
} else if err.Error() == errLocked.Error() {
return ErrLocked
} else if !errors.Is(err, windows.ERROR_LOCK_VIOLATION) {
} else if err != windows.ERROR_LOCK_VIOLATION {
return err
}
return nil
Expand Down
3 changes: 1 addition & 2 deletions client/pkg/fileutil/preallocate_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package fileutil

import (
"errors"
"os"
"syscall"

Expand Down Expand Up @@ -45,7 +44,7 @@ func preallocFixed(f *os.File, sizeInBytes int64) error {
}

// wrong argument to fallocate syscall
if errors.Is(err, unix.EINVAL) {
if err == unix.EINVAL {
// filesystem "st_blocks" are allocated in the units of
// "Allocation Block Size" (run "diskutil info /" command)
var stat syscall.Stat_t
Expand Down
6 changes: 3 additions & 3 deletions client/v3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func (c *Client) getToken(ctx context.Context) error {

resp, err := c.Auth.Authenticate(ctx, c.Username, c.Password)
if err != nil {
if errors.Is(err, rpctypes.ErrAuthNotEnabled) {
if err == rpctypes.ErrAuthNotEnabled {
c.authTokenBundle.UpdateAuthToken("")
return nil
}
Expand Down Expand Up @@ -627,7 +627,7 @@ func canceledByCaller(stopCtx context.Context, err error) bool {
return false
}

return errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded)
return err == context.Canceled || err == context.DeadlineExceeded
}

// IsConnCanceled returns true, if error is from a closed gRPC connection.
Expand All @@ -645,7 +645,7 @@ func IsConnCanceled(err error) bool {
}

// >= gRPC v1.10.x
if errors.Is(err, context.Canceled) {
if err == context.Canceled {
return true
}

Expand Down
2 changes: 1 addition & 1 deletion client/v3/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ func TestClientRejectOldCluster(t *testing.T) {
},
}

if err := c.checkVersion(); !errors.Is(err, tt.expectedError) {
if err := c.checkVersion(); err != tt.expectedError {
t.Errorf("heckVersion err:%v", err)
}
})
Expand Down
5 changes: 2 additions & 3 deletions client/v3/experimental/recipes/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package recipe

import (
"context"
"errors"
"fmt"
"strings"
"time"
Expand Down Expand Up @@ -52,7 +51,7 @@ func newUniqueKV(kv v3.KV, prefix string, val string) (*RemoteKV, error) {
if err == nil {
return &RemoteKV{kv, newKey, rev, val}, nil
}
if !errors.Is(err, ErrKeyExists) {
if err != ErrKeyExists {
return nil, err
}
}
Expand Down Expand Up @@ -156,7 +155,7 @@ func newUniqueEphemeralKV(s *concurrency.Session, prefix, val string) (ek *Ephem
for {
newKey := fmt.Sprintf("%s/%v", prefix, time.Now().UnixNano())
ek, err = newEphemeralKV(s, newKey, val)
if err == nil || !errors.Is(err, ErrKeyExists) {
if err == nil || err != ErrKeyExists {
break
}
}
Expand Down
2 changes: 1 addition & 1 deletion client/v3/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ func (w *watcher) Close() (err error) {
}
}
// Consider context.Canceled as a successful close
if errors.Is(err, context.Canceled) {
if err == context.Canceled {
err = nil
}
return err
Expand Down
3 changes: 1 addition & 2 deletions contrib/raftexample/kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"bytes"
"encoding/gob"
"encoding/json"
"errors"
"log"
"strings"
"sync"
Expand Down Expand Up @@ -114,7 +113,7 @@ func (s *kvstore) getSnapshot() ([]byte, error) {

func (s *kvstore) loadSnapshot() (*raftpb.Snapshot, error) {
snapshot, err := s.snapshotter.Load()
if errors.Is(err, snap.ErrNoSnapshot) {
if err == snap.ErrNoSnapshot {
return nil, nil
}
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions contrib/raftexample/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package main

import (
"context"
"errors"
"fmt"
"log"
"net/http"
Expand Down Expand Up @@ -393,7 +392,7 @@ func (rc *raftNode) maybeTriggerSnapshot(applyDoneC <-chan struct{}) {
compactIndex = rc.appliedIndex - snapshotCatchUpEntriesN
}
if err := rc.raftStorage.Compact(compactIndex); err != nil {
if !errors.Is(err, raft.ErrCompacted) {
if err != raft.ErrCompacted {
panic(err)
}
} else {
Expand Down
3 changes: 1 addition & 2 deletions etcdctl/ctlv3/command/auth_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package command

import (
"errors"
"fmt"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -83,7 +82,7 @@ func authEnableCommandFunc(cmd *cobra.Command, args []string) {
if _, err = cli.AuthEnable(ctx); err == nil {
break
}
if errors.Is(err, rpctypes.ErrRootRoleNotExist) {
if err == rpctypes.ErrRootRoleNotExist {
if _, err = cli.RoleAdd(ctx, "root"); err != nil {
break
}
Expand Down
3 changes: 1 addition & 2 deletions etcdctl/ctlv3/command/watch_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package command

import (
"errors"
"reflect"
"testing"
)
Expand Down Expand Up @@ -535,7 +534,7 @@ func Test_parseWatchArgs(t *testing.T) {
}
for i, ts := range tt {
watchArgs, execArgs, err := parseWatchArgs(ts.osArgs, ts.commandArgs, ts.envKey, ts.envRange, ts.interactive)
if !errors.Is(err, ts.err) {
if err != ts.err {
t.Fatalf("#%d: error expected %v, got %v", i, ts.err, err)
}
if !reflect.DeepEqual(watchArgs, ts.watchArgs) {
Expand Down
5 changes: 2 additions & 3 deletions pkg/ioutil/readcloser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package ioutil

import (
"bytes"
"errors"
"io"
"testing"
)
Expand All @@ -29,7 +28,7 @@ func (rc *readerNilCloser) Close() error { return nil }
func TestExactReadCloserExpectEOF(t *testing.T) {
buf := bytes.NewBuffer(make([]byte, 10))
rc := NewExactReadCloser(&readerNilCloser{buf}, 1)
if _, err := rc.Read(make([]byte, 10)); !errors.Is(err, ErrExpectEOF) {
if _, err := rc.Read(make([]byte, 10)); err != ErrExpectEOF {
t.Fatalf("expected %v, got %v", ErrExpectEOF, err)
}
}
Expand All @@ -41,7 +40,7 @@ func TestExactReadCloserShort(t *testing.T) {
if _, err := rc.Read(make([]byte, 10)); err != nil {
t.Fatalf("Read expected nil err, got %v", err)
}
if err := rc.Close(); !errors.Is(err, ErrShortRead) {
if err := rc.Close(); err != ErrShortRead {
t.Fatalf("Close expected %v, got %v", ErrShortRead, err)
}
}
2 changes: 1 addition & 1 deletion server/etcdserver/api/v3rpc/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (ls *LeaseServer) LeaseTimeToLive(ctx context.Context, rr *pb.LeaseTimeToLi

func (ls *LeaseServer) LeaseLeases(ctx context.Context, rr *pb.LeaseLeasesRequest) (*pb.LeaseLeasesResponse, error) {
resp, err := ls.le.LeaseLeases(ctx, rr)
if err != nil && err != lease.ErrLeaseNotFound {
if err != nil && !errors.Is(err, lease.ErrLeaseNotFound) {
return nil, togRPCError(err)
}
if errors.Is(err, lease.ErrLeaseNotFound) {
Expand Down
3 changes: 2 additions & 1 deletion server/etcdserver/api/v3rpc/maintenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package v3rpc
import (
"context"
"crypto/sha256"
errorspkg "errors"
"io"
"time"

Expand Down Expand Up @@ -163,7 +164,7 @@ func (ms *maintenanceServer) Snapshot(sr *pb.SnapshotRequest, srv pb.Maintenance
buf := make([]byte, snapshotSendBufferSize)

n, err := io.ReadFull(pr, buf)
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
if err != nil && err != io.EOF && !errorspkg.Is(err, io.ErrUnexpectedEOF) {
return togRPCError(err)
}
sent += int64(n)
Expand Down
3 changes: 2 additions & 1 deletion server/etcdserver/apply/uber_applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package apply

import (
"context"
"errors"
"time"

"go.uber.org/zap"
Expand Down Expand Up @@ -123,7 +124,7 @@ func (a *uberApplier) dispatch(ctx context.Context, r *pb.InternalRaftRequest) *
op := "unknown"
ar := &Result{}
defer func(start time.Time) {
success := ar.Err == nil || ar.Err == mvcc.ErrCompacted
success := ar.Err == nil || errors.Is(ar.Err, mvcc.ErrCompacted)
txn.ApplySecObserve(v3Version, op, success, time.Since(start))
txn.WarnOfExpensiveRequest(a.lg, a.warningApplyDuration, start, &pb.InternalRaftStringer{Request: r}, ar.Resp, ar.Err)
if !success {
Expand Down
3 changes: 2 additions & 1 deletion server/proxy/grpcproxy/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package grpcproxy

import (
"context"
"errors"
"fmt"
"net/http"
"time"
Expand Down Expand Up @@ -52,7 +53,7 @@ func checkHealth(c *clientv3.Client) etcdhttp.Health {
ctx, cancel := context.WithTimeout(c.Ctx(), time.Second)
_, err := c.Get(ctx, "a")
cancel()
if err == nil || err == rpctypes.ErrPermissionDenied {
if err == nil || errors.Is(err, rpctypes.ErrPermissionDenied) {
h.Health = "true"
} else {
h.Reason = fmt.Sprintf("GET ERROR:%s", err)
Expand Down
3 changes: 2 additions & 1 deletion server/storage/backend/batch_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package backend

import (
"bytes"
"errors"
"math"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -125,7 +126,7 @@ func (t *batchTx) UnsafeCreateBucket(bucket Bucket) {

func (t *batchTx) UnsafeDeleteBucket(bucket Bucket) {
err := t.tx.DeleteBucket(bucket.Name())
if err != nil && err != bolterrors.ErrBucketNotFound {
if err != nil && !errors.Is(err, bolterrors.ErrBucketNotFound) {
t.backend.lg.Fatal(
"failed to delete a bucket",
zap.Stringer("bucket-name", bucket),
Expand Down
Loading

0 comments on commit f597415

Please sign in to comment.