Merge pull request #18614 from redwrasse/redwrasse/client/rest-of-errors-is

client: remaining errors.Is conversions
This commit is contained in:
James Blair 2024-09-26 04:09:31 +10:00 committed by GitHub
commit 1a08fb2809
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 10 additions and 8 deletions

View File

@ -88,11 +88,12 @@ if err != nil {
kapi := client.NewKeysAPI(c)
resp, err := kapi.Set(ctx, "test", "bar", nil)
if err != nil {
if err == context.Canceled {
var cerr *client.ClusterError
if errors.Is(err, context.Canceled) {
// ctx is canceled by another routine
} else if err == context.DeadlineExceeded {
} else if errors.Is(err, context.DeadlineExceeded) {
// ctx is attached with a deadline and it exceeded
} else if cerr, ok := err.(*client.ClusterError); ok {
} else if errors.As(err, &cerr) {
// process (cerr.Errors)
} else {
// bad cluster endpoints, which are not etcd servers

View File

@ -17,6 +17,7 @@
package fileutil
import (
"errors"
"os"
"syscall"
@ -39,7 +40,7 @@ func preallocFixed(f *os.File, sizeInBytes int64) error {
Length: sizeInBytes,
}
err := unix.FcntlFstore(f.Fd(), unix.F_PREALLOCATE, fstore)
if err == nil || err == unix.ENOTSUP {
if err == nil || errors.Is(err, unix.ENOTSUP) {
return nil
}

View File

@ -220,7 +220,7 @@ func (c *Client) autoSync() {
ctx, cancel := context.WithTimeout(c.ctx, 5*time.Second)
err := c.Sync(ctx)
cancel()
if err != nil && err != c.ctx.Err() {
if err != nil && !errors.Is(err, c.ctx.Err()) {
c.lg.Info("Auto sync endpoints failed.", zap.Error(err))
}
}

View File

@ -146,14 +146,14 @@ func (c *Client) streamClientInterceptor(optFuncs ...retryOption) grpc.StreamCli
// shouldRefreshToken checks whether there's a need to refresh the token based on the error and callOptions,
// and returns a boolean value.
func (c *Client) shouldRefreshToken(err error, callOpts *options) bool {
if rpctypes.Error(err) == rpctypes.ErrUserEmpty {
if errors.Is(rpctypes.Error(err), rpctypes.ErrUserEmpty) {
// refresh the token when username, password is present but the server returns ErrUserEmpty
// which is possible when the client token is cleared somehow
return c.authTokenBundle != nil // equal to c.Username != "" && c.Password != ""
}
return callOpts.retryAuth &&
(rpctypes.Error(err) == rpctypes.ErrInvalidAuthToken || rpctypes.Error(err) == rpctypes.ErrAuthOldRevision)
(errors.Is(rpctypes.Error(err), rpctypes.ErrInvalidAuthToken) || errors.Is(rpctypes.Error(err), rpctypes.ErrAuthOldRevision))
}
func (c *Client) refreshToken(ctx context.Context) error {
@ -254,7 +254,7 @@ func (s *serverStreamingRetryingStream) receiveMsgAndIndicateRetry(m any) (bool,
wasGood := s.receivedGood
s.mu.RUnlock()
err := s.getStream().RecvMsg(m)
if err == nil || err == io.EOF {
if err == nil || errors.Is(err, io.EOF) {
s.mu.Lock()
s.receivedGood = true
s.mu.Unlock()