Skip to content

Commit

Permalink
Merge pull request #18510 from redwrasse/redwrasse/errors-is
Browse files Browse the repository at this point in the history
Use errors.Is for error equality checks
  • Loading branch information
serathius authored Sep 20, 2024
2 parents 9fc3b2a + d4df7a9 commit 1820d3b
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 42 deletions.
3 changes: 2 additions & 1 deletion client/pkg/fileutil/lock_flock.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 @@ -28,7 +29,7 @@ func flockTryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, err
}
if err = syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil {
f.Close()
if err == syscall.EWOULDBLOCK {
if errors.Is(err, syscall.EWOULDBLOCK) {
err = ErrLocked
}
return nil, err
Expand Down
5 changes: 3 additions & 2 deletions pkg/proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package proxy

import (
"context"
"errors"
"fmt"
"io"
mrand "math/rand"
Expand Down Expand Up @@ -427,7 +428,7 @@ func (s *server) ioCopy(dst io.Writer, src io.Reader, ptype proxyType) {
for {
nr1, err := src.Read(buf)
if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
return
}
// connection already closed
Expand Down Expand Up @@ -545,7 +546,7 @@ func (s *server) ioCopy(dst io.Writer, src io.Reader, ptype proxyType) {
var nw int
nw, err = dst.Write(data)
if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
return
}
select {
Expand Down
3 changes: 2 additions & 1 deletion server/etcdserver/api/v3compactor/periodic.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package v3compactor

import (
"context"
"errors"
"sync"
"time"

Expand Down Expand Up @@ -139,7 +140,7 @@ func (pc *Periodic) Run() {
)
startTime := pc.clock.Now()
_, err := pc.c.Compact(pc.ctx, &pb.CompactionRequest{Revision: rev})
if err == nil || err == mvcc.ErrCompacted {
if err == nil || errors.Is(err, mvcc.ErrCompacted) {
pc.lg.Info(
"completed auto periodic compaction",
zap.Int64("revision", rev),
Expand Down
4 changes: 2 additions & 2 deletions server/etcdserver/api/v3discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func newDiscovery(lg *zap.Logger, dcfg *DiscoveryConfig, id types.ID) (*discover
func (d *discovery) getCluster() (string, error) {
cls, clusterSize, rev, err := d.checkCluster()
if err != nil {
if err == ErrFullCluster {
if errors.Is(err, ErrFullCluster) {
return cls.getInitClusterStr(clusterSize)
}
return "", err
Expand Down Expand Up @@ -303,7 +303,7 @@ func (d *discovery) checkClusterRetry() (*clusterInfo, int, int64, error) {
func (d *discovery) checkCluster() (*clusterInfo, int, int64, error) {
clusterSize, err := d.getClusterSize()
if err != nil {
if err == ErrSizeNotFound || err == ErrBadSizeKey {
if errors.Is(err, ErrSizeNotFound) || errors.Is(err, ErrBadSizeKey) {
return nil, 0, 0, err
}

Expand Down
13 changes: 7 additions & 6 deletions server/etcdserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package etcdserver
import (
"context"
"encoding/json"
errorspkg "errors"
"expvar"
"fmt"
"math"
Expand Down Expand Up @@ -1445,7 +1446,7 @@ func (s *EtcdServer) PromoteMember(ctx context.Context, id uint64) ([]*membershi
learnerPromoteSucceed.Inc()
return resp, nil
}
if err != errors.ErrNotLeader {
if !errorspkg.Is(err, errors.ErrNotLeader) {
learnerPromoteFailed.WithLabelValues(err.Error()).Inc()
return resp, err
}
Expand All @@ -1464,13 +1465,13 @@ func (s *EtcdServer) PromoteMember(ctx context.Context, id uint64) ([]*membershi
return resp, nil
}
// If member promotion failed, return early. Otherwise keep retry.
if err == errors.ErrLearnerNotReady || err == membership.ErrIDNotFound || err == membership.ErrMemberNotLearner {
if errorspkg.Is(err, errors.ErrLearnerNotReady) || errorspkg.Is(err, membership.ErrIDNotFound) || errorspkg.Is(err, membership.ErrMemberNotLearner) {
return nil, err
}
}
}

if cctx.Err() == context.DeadlineExceeded {
if errorspkg.Is(cctx.Err(), context.DeadlineExceeded) {
return nil, errors.ErrTimeout
}
return nil, errors.ErrCanceled
Expand Down Expand Up @@ -1980,7 +1981,7 @@ func (s *EtcdServer) applyEntryNormal(e *raftpb.Entry, shouldApplyV3 membership.
return
}

if ar.Err != errors.ErrNoSpace || len(s.alarmStore.Get(pb.AlarmType_NOSPACE)) > 0 {
if !errorspkg.Is(ar.Err, errors.ErrNoSpace) || len(s.alarmStore.Get(pb.AlarmType_NOSPACE)) > 0 {
s.w.Trigger(id, ar)
return
}
Expand Down Expand Up @@ -2149,7 +2150,7 @@ func (s *EtcdServer) snapshot(snapi uint64, confState raftpb.ConfState) {
if err != nil {
// the snapshot was done asynchronously with the progress of raft.
// raft might have already got a newer snapshot.
if err == raft.ErrSnapOutOfDate {
if errorspkg.Is(err, raft.ErrSnapOutOfDate) {
return
}
lg.Panic("failed to create snapshot", zap.Error(err))
Expand Down Expand Up @@ -2190,7 +2191,7 @@ func (s *EtcdServer) snapshot(snapi uint64, confState raftpb.ConfState) {
if err != nil {
// the compaction was done asynchronously with the progress of raft.
// raft log might already been compact.
if err == raft.ErrCompacted {
if errorspkg.Is(err, raft.ErrCompacted) {
return
}
lg.Panic("failed to compact", zap.Error(err))
Expand Down
17 changes: 9 additions & 8 deletions server/etcdserver/v3_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"encoding/base64"
"encoding/binary"
errorspkg "errors"
"strconv"
"time"

Expand Down Expand Up @@ -296,7 +297,7 @@ func (s *EtcdServer) LeaseRenew(ctx context.Context, id lease.LeaseID) (int64, e
if err == nil { // already requested to primary lessor(leader)
return ttl, nil
}
if err != lease.ErrNotPrimary {
if !errorspkg.Is(err, lease.ErrNotPrimary) {
return -1, err
}
}
Expand All @@ -313,15 +314,15 @@ func (s *EtcdServer) LeaseRenew(ctx context.Context, id lease.LeaseID) (int64, e
for _, url := range leader.PeerURLs {
lurl := url + leasehttp.LeasePrefix
ttl, err := leasehttp.RenewHTTP(cctx, id, lurl, s.peerRt)
if err == nil || err == lease.ErrLeaseNotFound {
if err == nil || errorspkg.Is(err, lease.ErrLeaseNotFound) {
return ttl, err
}
}
// Throttle in case of e.g. connection problems.
time.Sleep(50 * time.Millisecond)
}

if cctx.Err() == context.DeadlineExceeded {
if errorspkg.Is(cctx.Err(), context.DeadlineExceeded) {
return -1, errors.ErrTimeout
}
return -1, errors.ErrCanceled
Expand Down Expand Up @@ -402,13 +403,13 @@ func (s *EtcdServer) leaseTimeToLive(ctx context.Context, r *pb.LeaseTimeToLiveR
if err == nil {
return resp.LeaseTimeToLiveResponse, nil
}
if err == lease.ErrLeaseNotFound {
if errorspkg.Is(err, lease.ErrLeaseNotFound) {
return nil, err
}
}
}

if cctx.Err() == context.DeadlineExceeded {
if errorspkg.Is(cctx.Err(), context.DeadlineExceeded) {
return nil, errors.ErrTimeout
}
return nil, errors.ErrCanceled
Expand Down Expand Up @@ -527,7 +528,7 @@ func (s *EtcdServer) Authenticate(ctx context.Context, r *pb.AuthenticateRequest
for {
checkedRevision, err := s.AuthStore().CheckPassword(r.Name, r.Password)
if err != nil {
if err != auth.ErrAuthNotEnabled {
if !errorspkg.Is(err, auth.ErrAuthNotEnabled) {
lg.Warn(
"invalid authentication was requested",
zap.String("user", r.Name),
Expand Down Expand Up @@ -854,7 +855,7 @@ func (s *EtcdServer) linearizableReadLoop() {
}

func isStopped(err error) bool {
return err == raft.ErrStopped || err == errors.ErrStopped
return errorspkg.Is(err, raft.ErrStopped) || errorspkg.Is(err, errors.ErrStopped)
}

func (s *EtcdServer) requestCurrentIndex(leaderChangedNotifier <-chan struct{}, requestID uint64) (uint64, error) {
Expand Down Expand Up @@ -942,7 +943,7 @@ func (s *EtcdServer) sendReadIndex(requestIndex uint64) error {
cctx, cancel := context.WithTimeout(context.Background(), s.Cfg.ReqTimeout())
err := s.r.ReadIndex(cctx, ctxToSend)
cancel()
if err == raft.ErrStopped {
if errorspkg.Is(err, raft.ErrStopped) {
return err
}
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion tests/framework/e2e/etcd_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func (ep *EtcdServerProcess) IsRunning() bool {
}

exitCode, err := ep.proc.ExitCode()
if err == expect.ErrProcessRunning {
if errors.Is(err, expect.ErrProcessRunning) {
return true
}

Expand Down
23 changes: 12 additions & 11 deletions tests/integration/clientv3/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package clientv3test
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"reflect"
Expand Down Expand Up @@ -50,12 +51,12 @@ func TestKVPutError(t *testing.T) {
ctx := context.TODO()

_, err := kv.Put(ctx, "", "bar")
if err != rpctypes.ErrEmptyKey {
if !errors.Is(err, rpctypes.ErrEmptyKey) {
t.Fatalf("expected %v, got %v", rpctypes.ErrEmptyKey, err)
}

_, err = kv.Put(ctx, "key", strings.Repeat("a", int(maxReqBytes+100)))
if err != rpctypes.ErrRequestTooLarge {
if !errors.Is(err, rpctypes.ErrRequestTooLarge) {
t.Fatalf("expected %v, got %v", rpctypes.ErrRequestTooLarge, err)
}

Expand All @@ -67,7 +68,7 @@ func TestKVPutError(t *testing.T) {
time.Sleep(1 * time.Second) // give enough time for commit

_, err = kv.Put(ctx, "foo2", strings.Repeat("a", int(maxReqBytes-50)))
if err != rpctypes.ErrNoSpace { // over quota
if !errors.Is(err, rpctypes.ErrNoSpace) { // over quota
t.Fatalf("expected %v, got %v", rpctypes.ErrNoSpace, err)
}
}
Expand Down Expand Up @@ -118,7 +119,7 @@ func TestKVPutWithIgnoreValue(t *testing.T) {
kv := clus.RandClient()

_, err := kv.Put(context.TODO(), "foo", "", clientv3.WithIgnoreValue())
if err != rpctypes.ErrKeyNotFound {
if !errors.Is(err, rpctypes.ErrKeyNotFound) {
t.Fatalf("err expected %v, got %v", rpctypes.ErrKeyNotFound, err)
}

Expand Down Expand Up @@ -157,7 +158,7 @@ func TestKVPutWithIgnoreLease(t *testing.T) {
t.Errorf("failed to create lease %v", err)
}

if _, err := kv.Put(context.TODO(), "zoo", "bar", clientv3.WithIgnoreLease()); err != rpctypes.ErrKeyNotFound {
if _, err := kv.Put(context.TODO(), "zoo", "bar", clientv3.WithIgnoreLease()); !errors.Is(err, rpctypes.ErrKeyNotFound) {
t.Fatalf("err expected %v, got %v", rpctypes.ErrKeyNotFound, err)
}

Expand Down Expand Up @@ -199,7 +200,7 @@ func TestKVPutWithRequireLeader(t *testing.T) {

kv := clus.Client(0)
_, err := kv.Put(clientv3.WithRequireLeader(context.Background()), "foo", "bar")
if err != rpctypes.ErrNoLeader {
if !errors.Is(err, rpctypes.ErrNoLeader) {
t.Fatal(err)
}

Expand Down Expand Up @@ -413,12 +414,12 @@ func TestKVCompactError(t *testing.T) {
}

_, err = kv.Compact(ctx, 6)
if err != rpctypes.ErrCompacted {
if !errors.Is(err, rpctypes.ErrCompacted) {
t.Fatalf("expected %v, got %v", rpctypes.ErrCompacted, err)
}

_, err = kv.Compact(ctx, 100)
if err != rpctypes.ErrFutureRev {
if !errors.Is(err, rpctypes.ErrFutureRev) {
t.Fatalf("expected %v, got %v", rpctypes.ErrFutureRev, err)
}
}
Expand All @@ -443,7 +444,7 @@ func TestKVCompact(t *testing.T) {
t.Fatalf("couldn't compact kv space (%v)", err)
}
_, err = kv.Compact(ctx, 7)
if err == nil || err != rpctypes.ErrCompacted {
if err == nil || !errors.Is(err, rpctypes.ErrCompacted) {
t.Fatalf("error got %v, want %v", err, rpctypes.ErrCompacted)
}

Expand Down Expand Up @@ -472,7 +473,7 @@ func TestKVCompact(t *testing.T) {
}

_, err = kv.Compact(ctx, 1000)
if err == nil || err != rpctypes.ErrFutureRev {
if err == nil || !errors.Is(err, rpctypes.ErrFutureRev) {
t.Fatalf("error got %v, want %v", err, rpctypes.ErrFutureRev)
}
}
Expand Down Expand Up @@ -750,7 +751,7 @@ func TestKVLargeRequests(t *testing.T) {
_, err := cli.Put(context.TODO(), "foo", strings.Repeat("a", test.valueSize))

if _, ok := err.(rpctypes.EtcdError); ok {
if err != test.expectError {
if !errors.Is(err, test.expectError) {
t.Errorf("#%d: expected %v, got %v", i, test.expectError, err)
}
} else if err != nil && !strings.HasPrefix(err.Error(), test.expectError.Error()) {
Expand Down
11 changes: 6 additions & 5 deletions tests/integration/clientv3/lease/lease_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package lease_test

import (
"context"
"errors"
"fmt"
"reflect"
"sort"
Expand All @@ -38,7 +39,7 @@ func TestLeaseNotFoundError(t *testing.T) {
kv := clus.RandClient()

_, err := kv.Put(context.TODO(), "foo", "bar", clientv3.WithLease(clientv3.LeaseID(500)))
if err != rpctypes.ErrLeaseNotFound {
if !errors.Is(err, rpctypes.ErrLeaseNotFound) {
t.Fatalf("expected %v, got %v", rpctypes.ErrLeaseNotFound, err)
}
}
Expand All @@ -54,7 +55,7 @@ func TestLeaseGrant(t *testing.T) {
kv := clus.RandClient()

_, merr := lapi.Grant(context.Background(), clientv3.MaxLeaseTTL+1)
if merr != rpctypes.ErrLeaseTTLTooLarge {
if !errors.Is(merr, rpctypes.ErrLeaseTTLTooLarge) {
t.Fatalf("err = %v, want %v", merr, rpctypes.ErrLeaseTTLTooLarge)
}

Expand Down Expand Up @@ -90,7 +91,7 @@ func TestLeaseRevoke(t *testing.T) {
}

_, err = kv.Put(context.TODO(), "foo", "bar", clientv3.WithLease(resp.ID))
if err != rpctypes.ErrLeaseNotFound {
if !errors.Is(err, rpctypes.ErrLeaseNotFound) {
t.Fatalf("err = %v, want %v", err, rpctypes.ErrLeaseNotFound)
}
}
Expand All @@ -114,7 +115,7 @@ func TestLeaseKeepAliveOnce(t *testing.T) {
}

_, err = lapi.KeepAliveOnce(context.Background(), clientv3.LeaseID(0))
if err != rpctypes.ErrLeaseNotFound {
if !errors.Is(err, rpctypes.ErrLeaseNotFound) {
t.Errorf("expected %v, got %v", rpctypes.ErrLeaseNotFound, err)
}
}
Expand Down Expand Up @@ -761,7 +762,7 @@ func TestV3LeaseFailureOverlap(t *testing.T) {
go func() {
defer wg.Done()
err := updown(n)
if err == nil || err == rpctypes.ErrTimeoutDueToConnectionLost {
if err == nil || errors.Is(err, rpctypes.ErrTimeoutDueToConnectionLost) {
return
}
t.Error(err)
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/clientv3/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package clientv3test
import (
"bufio"
"context"
"errors"
"io"
"net"
"net/http"
Expand Down Expand Up @@ -165,7 +166,7 @@ func getHTTPBodyAsLines(t *testing.T, url string) []string {
for {
line, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
} else {
t.Fatalf("error reading: %v", err)
Expand Down
Loading

0 comments on commit 1820d3b

Please sign in to comment.