Skip to content

Commit 55de68d

Browse files
authored
Merge pull request #18722 from mmorel-35/api/errorlint
fix: enable errorlint in api, client and pkg
2 parents 44c3918 + b281a3c commit 55de68d

File tree

22 files changed

+69
-52
lines changed

22 files changed

+69
-52
lines changed

api/v3rpc/rpctypes/error_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package rpctypes
1616

1717
import (
18+
"errors"
1819
"testing"
1920

2021
"google.golang.org/grpc/codes"
@@ -24,19 +25,20 @@ import (
2425
func TestConvert(t *testing.T) {
2526
e1 := status.Error(codes.InvalidArgument, "etcdserver: key is not provided")
2627
e2 := ErrGRPCEmptyKey
27-
e3 := ErrEmptyKey
28+
var e3 EtcdError
29+
errors.As(ErrEmptyKey, &e3)
2830

2931
if e1.Error() != e2.Error() {
3032
t.Fatalf("expected %q == %q", e1.Error(), e2.Error())
3133
}
32-
if ev1, ok := status.FromError(e1); ok && ev1.Code() != e3.(EtcdError).Code() {
33-
t.Fatalf("expected them to be equal, got %v / %v", ev1.Code(), e3.(EtcdError).Code())
34+
if ev1, ok := status.FromError(e1); ok && ev1.Code() != e3.Code() {
35+
t.Fatalf("expected them to be equal, got %v / %v", ev1.Code(), e3.Code())
3436
}
3537

3638
if e1.Error() == e3.Error() {
3739
t.Fatalf("expected %q != %q", e1.Error(), e3.Error())
3840
}
39-
if ev2, ok := status.FromError(e2); ok && ev2.Code() != e3.(EtcdError).Code() {
40-
t.Fatalf("expected them to be equal, got %v / %v", ev2.Code(), e3.(EtcdError).Code())
41+
if ev2, ok := status.FromError(e2); ok && ev2.Code() != e3.Code() {
42+
t.Fatalf("expected them to be equal, got %v / %v", ev2.Code(), e3.Code())
4143
}
4244
}

client/pkg/fileutil/lock_linux.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package fileutil
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"io"
2223
"os"
@@ -58,13 +59,13 @@ func TryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
5859
func ofdTryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
5960
f, err := os.OpenFile(path, flag, perm)
6061
if err != nil {
61-
return nil, fmt.Errorf("ofdTryLockFile failed to open %q (%v)", path, err)
62+
return nil, fmt.Errorf("ofdTryLockFile failed to open %q (%w)", path, err)
6263
}
6364

6465
flock := wrlck
6566
if err = syscall.FcntlFlock(f.Fd(), unix.F_OFD_SETLK, &flock); err != nil {
6667
f.Close()
67-
if err == syscall.EWOULDBLOCK {
68+
if errors.Is(err, syscall.EWOULDBLOCK) {
6869
err = ErrLocked
6970
}
7071
return nil, err
@@ -79,7 +80,7 @@ func LockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
7980
func ofdLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
8081
f, err := os.OpenFile(path, flag, perm)
8182
if err != nil {
82-
return nil, fmt.Errorf("ofdLockFile failed to open %q (%v)", path, err)
83+
return nil, fmt.Errorf("ofdLockFile failed to open %q (%w)", path, err)
8384
}
8485

8586
flock := wrlck

client/pkg/fileutil/preallocate_unix.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package fileutil
1818

1919
import (
20+
"errors"
2021
"os"
2122
"syscall"
2223
)
@@ -25,10 +26,10 @@ func preallocExtend(f *os.File, sizeInBytes int64) error {
2526
// use mode = 0 to change size
2627
err := syscall.Fallocate(int(f.Fd()), 0, 0, sizeInBytes)
2728
if err != nil {
28-
errno, ok := err.(syscall.Errno)
29+
var errno syscall.Errno
2930
// not supported; fallback
3031
// fallocate EINTRs frequently in some environments; fallback
31-
if ok && (errno == syscall.ENOTSUP || errno == syscall.EINTR) {
32+
if errors.As(err, &errno) && (errno == syscall.ENOTSUP || errno == syscall.EINTR) {
3233
return preallocExtendTrunc(f, sizeInBytes)
3334
}
3435
}
@@ -39,9 +40,9 @@ func preallocFixed(f *os.File, sizeInBytes int64) error {
3940
// use mode = 1 to keep size; see FALLOC_FL_KEEP_SIZE
4041
err := syscall.Fallocate(int(f.Fd()), 1, 0, sizeInBytes)
4142
if err != nil {
42-
errno, ok := err.(syscall.Errno)
43+
var errno syscall.Errno
4344
// treat not supported as nil error
44-
if ok && errno == syscall.ENOTSUP {
45+
if errors.As(err, &errno) && errno == syscall.ENOTSUP {
4546
return nil
4647
}
4748
}

client/pkg/srv/srv.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func GetCluster(serviceScheme, service, name, dns string, apurls types.URLs) ([]
8787

8888
err := updateNodeMap(service, serviceScheme)
8989
if err != nil {
90-
return nil, fmt.Errorf("error querying DNS SRV records for _%s %s", service, err)
90+
return nil, fmt.Errorf("error querying DNS SRV records for _%s %w", service, err)
9191
}
9292
return stringParts, nil
9393
}
@@ -123,7 +123,7 @@ func GetClient(service, domain string, serviceName string) (*SRVClients, error)
123123
errHTTP := updateURLs(GetSRVService(service, serviceName, "http"), "http")
124124

125125
if errHTTPS != nil && errHTTP != nil {
126-
return nil, fmt.Errorf("dns lookup errors: %s and %s", errHTTPS, errHTTP)
126+
return nil, fmt.Errorf("dns lookup errors: %w and %w", errHTTPS, errHTTP)
127127
}
128128

129129
endpoints := make([]string, len(urls))

client/pkg/transport/timeout_dialer_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package transport
1616

1717
import (
18+
"errors"
1819
"net"
1920
"testing"
2021
"time"
@@ -57,7 +58,8 @@ func TestReadWriteTimeoutDialer(t *testing.T) {
5758
t.Fatal("wait timeout")
5859
}
5960

60-
if operr, ok := err.(*net.OpError); !ok || operr.Op != "write" || !operr.Timeout() {
61+
var operr *net.OpError
62+
if !errors.As(err, &operr) || operr.Op != "write" || !operr.Timeout() {
6163
t.Errorf("err = %v, want write i/o timeout error", err)
6264
}
6365

@@ -77,7 +79,7 @@ func TestReadWriteTimeoutDialer(t *testing.T) {
7779
t.Fatal("wait timeout")
7880
}
7981

80-
if operr, ok := err.(*net.OpError); !ok || operr.Op != "read" || !operr.Timeout() {
82+
if !errors.As(err, &operr) || operr.Op != "read" || !operr.Timeout() {
8183
t.Errorf("err = %v, want read i/o timeout error", err)
8284
}
8385
}

client/pkg/transport/timeout_listener_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package transport
1616

1717
import (
18+
"errors"
1819
"net"
1920
"testing"
2021
"time"
@@ -82,7 +83,8 @@ func TestWriteReadTimeoutListener(t *testing.T) {
8283
t.Fatal("wait timeout")
8384
}
8485

85-
if operr, ok := err.(*net.OpError); !ok || operr.Op != "write" || !operr.Timeout() {
86+
var operr *net.OpError
87+
if !errors.As(err, &operr) || operr.Op != "write" || !operr.Timeout() {
8688
t.Errorf("err = %v, want write i/o timeout error", err)
8789
}
8890
writerStopCh <- struct{}{}
@@ -109,7 +111,7 @@ func TestWriteReadTimeoutListener(t *testing.T) {
109111
t.Fatal("wait timeout")
110112
}
111113

112-
if operr, ok := err.(*net.OpError); !ok || operr.Op != "read" || !operr.Timeout() {
114+
if !errors.As(err, &operr) || operr.Op != "read" || !operr.Timeout() {
113115
t.Errorf("err = %v, want read i/o timeout error", err)
114116
}
115117
readerStopCh <- struct{}{}

client/v3/client.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ func (c *Client) getToken(ctx context.Context) error {
294294

295295
resp, err := c.Auth.Authenticate(ctx, c.Username, c.Password)
296296
if err != nil {
297-
if err == rpctypes.ErrAuthNotEnabled {
297+
if errors.Is(err, rpctypes.ErrAuthNotEnabled) {
298298
c.authTokenBundle.UpdateAuthToken("")
299299
return nil
300300
}
@@ -605,7 +605,8 @@ func ContextError(ctx context.Context, err error) error {
605605
return nil
606606
}
607607
err = rpctypes.Error(err)
608-
if _, ok := err.(rpctypes.EtcdError); ok {
608+
var serverErr rpctypes.EtcdError
609+
if errors.As(err, &serverErr) {
609610
return err
610611
}
611612
if ev, ok := status.FromError(err); ok {
@@ -627,7 +628,7 @@ func canceledByCaller(stopCtx context.Context, err error) bool {
627628
return false
628629
}
629630

630-
return err == context.Canceled || err == context.DeadlineExceeded
631+
return errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded)
631632
}
632633

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

647648
// >= gRPC v1.10.x
648-
if err == context.Canceled {
649+
if errors.Is(err, context.Canceled) {
649650
return true
650651
}
651652

client/v3/client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,8 @@ func TestClientRejectOldCluster(t *testing.T) {
430430
},
431431
}
432432

433-
if err := c.checkVersion(); err != tt.expectedError {
434-
t.Errorf("heckVersion err:%v", err)
433+
if err := c.checkVersion(); !errors.Is(err, tt.expectedError) {
434+
t.Errorf("checkVersion err:%v", err)
435435
}
436436
})
437437
}

client/v3/experimental/recipes/key.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package recipe
1616

1717
import (
1818
"context"
19+
"errors"
1920
"fmt"
2021
"strings"
2122
"time"
@@ -51,7 +52,7 @@ func newUniqueKV(kv v3.KV, prefix string, val string) (*RemoteKV, error) {
5152
if err == nil {
5253
return &RemoteKV{kv, newKey, rev, val}, nil
5354
}
54-
if err != ErrKeyExists {
55+
if !errors.Is(err, ErrKeyExists) {
5556
return nil, err
5657
}
5758
}
@@ -155,7 +156,7 @@ func newUniqueEphemeralKV(s *concurrency.Session, prefix, val string) (ek *Ephem
155156
for {
156157
newKey := fmt.Sprintf("%s/%v", prefix, time.Now().UnixNano())
157158
ek, err = newEphemeralKV(s, newKey, val)
158-
if err == nil || err != ErrKeyExists {
159+
if err == nil || !errors.Is(err, ErrKeyExists) {
159160
break
160161
}
161162
}

client/v3/lease.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package clientv3
1616

1717
import (
1818
"context"
19+
"errors"
1920
"sync"
2021
"time"
2122

@@ -464,7 +465,7 @@ func (l *lessor) recvKeepAliveLoop() (gerr error) {
464465
return err
465466
}
466467

467-
if ContextError(l.stopCtx, err) == rpctypes.ErrNoLeader {
468+
if errors.Is(ContextError(l.stopCtx, err), rpctypes.ErrNoLeader) {
468469
l.closeRequireLeader()
469470
}
470471
break

0 commit comments

Comments
 (0)