Skip to content

Commit

Permalink
Replaces rest of error equality checks with errors.Is
Browse files Browse the repository at this point in the history
Signed-off-by: redwrasse <[email protected]>
  • Loading branch information
redwrasse committed Sep 20, 2024
1 parent 6ea81c1 commit d59fa33
Show file tree
Hide file tree
Showing 69 changed files with 209 additions and 158 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 err == ctx.Err() {
if errors.Is(err, ctx.Err()) {
return nil, nil, ctx.Err()
}
if err == context.Canceled || err == context.DeadlineExceeded {
if errors.Is(err, context.Canceled) || errors.Is(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 err != ErrNoRequest {
if !errors.Is(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 err != context.Canceled {
if !errors.Is(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 err != context.DeadlineExceeded {
if !errors.Is(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 err != errFakeCancelContext {
if !errors.Is(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 err != context.Canceled {
if !errors.Is(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 err == ErrEmptyBody {
if errors.Is(err, ErrEmptyBody) {
continue
}
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion client/pkg/fileutil/lock_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package fileutil

import (
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -64,7 +65,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 err == syscall.EWOULDBLOCK {
if errors.Is(err, syscall.EWOULDBLOCK) {
err = ErrLocked
}
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion client/pkg/fileutil/lock_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package fileutil

import (
"errors"
"os"
"syscall"
)
Expand All @@ -35,7 +36,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 err == syscall.EAGAIN {
if errors.Is(err, syscall.EAGAIN) {
err = ErrLocked
}
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion client/pkg/fileutil/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package fileutil

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

// try lock a locked file
if _, err = TryLockFile(f.Name(), os.O_WRONLY, PrivateFileMode); err != ErrLocked {
if _, err = TryLockFile(f.Name(), os.O_WRONLY, PrivateFileMode); !errors.Is(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 err.Error() == errLocked.Error() {
} else if errors.Is(err, errLocked) {
return ErrLocked
} else if err != windows.ERROR_LOCK_VIOLATION {
} else if !errors.Is(err, windows.ERROR_LOCK_VIOLATION) {
return err
}
return nil
Expand Down
3 changes: 2 additions & 1 deletion client/pkg/fileutil/preallocate_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package fileutil

import (
"errors"
"os"
"syscall"

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

// wrong argument to fallocate syscall
if err == unix.EINVAL {
if errors.Is(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 err == rpctypes.ErrAuthNotEnabled {
if errors.Is(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 err == context.Canceled || err == context.DeadlineExceeded
return errors.Is(err, context.Canceled) || errors.Is(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 err == context.Canceled {
if errors.Is(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(); err != tt.expectedError {
if err := c.checkVersion(); !errors.Is(err, tt.expectedError) {
t.Errorf("heckVersion err:%v", err)
}
})
Expand Down
5 changes: 3 additions & 2 deletions client/v3/experimental/recipes/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package recipe

import (
"context"
"errors"
"fmt"
"strings"
"time"
Expand Down Expand Up @@ -51,7 +52,7 @@ func newUniqueKV(kv v3.KV, prefix string, val string) (*RemoteKV, error) {
if err == nil {
return &RemoteKV{kv, newKey, rev, val}, nil
}
if err != ErrKeyExists {
if !errors.Is(err, ErrKeyExists) {
return nil, err
}
}
Expand Down Expand Up @@ -155,7 +156,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 || err != ErrKeyExists {
if err == nil || !errors.Is(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 err == context.Canceled {
if errors.Is(err, context.Canceled) {
err = nil
}
return err
Expand Down
3 changes: 2 additions & 1 deletion contrib/raftexample/kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bytes"
"encoding/gob"
"encoding/json"
"errors"
"log"
"strings"
"sync"
Expand Down Expand Up @@ -113,7 +114,7 @@ func (s *kvstore) getSnapshot() ([]byte, error) {

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

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

import (
"errors"
"fmt"

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

import (
"errors"
"reflect"
"testing"
)
Expand Down Expand Up @@ -534,7 +535,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 err != ts.err {
if !errors.Is(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: 3 additions & 2 deletions pkg/ioutil/readcloser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package ioutil

import (
"bytes"
"errors"
"io"
"testing"
)
Expand All @@ -28,7 +29,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)); err != ErrExpectEOF {
if _, err := rc.Read(make([]byte, 10)); !errors.Is(err, ErrExpectEOF) {
t.Fatalf("expected %v, got %v", ErrExpectEOF, err)
}
}
Expand All @@ -40,7 +41,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(); err != ErrShortRead {
if err := rc.Close(); !errors.Is(err, ErrShortRead) {
t.Fatalf("Close expected %v, got %v", ErrShortRead, err)
}
}
3 changes: 2 additions & 1 deletion server/auth/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package auth

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

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

Expand Down
Loading

0 comments on commit d59fa33

Please sign in to comment.