Skip to content

Commit

Permalink
etcdctl: remaining errors.Is conversions for error equality and inequ…
Browse files Browse the repository at this point in the history
…ality checks

Signed-off-by: redwrasse <[email protected]>
  • Loading branch information
redwrasse committed Sep 20, 2024
1 parent d59fa33 commit 0a355d3
Show file tree
Hide file tree
Showing 67 changed files with 156 additions and 205 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
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)
}
}
3 changes: 1 addition & 2 deletions server/auth/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package auth

import (
"context"
"errors"
"fmt"
"testing"
"time"
Expand Down Expand Up @@ -140,7 +139,7 @@ func testJWTInfo(t *testing.T, opts map[string]string) {
}

_, aerr := verify.assign(ctx, "abc", 123)
if !errors.Is(aerr, ErrVerifyOnly) {
if aerr != ErrVerifyOnly {
t.Fatalf("unexpected error when attempting to sign with public key: %v", aerr)
}

Expand Down
Loading

0 comments on commit 0a355d3

Please sign in to comment.