diff --git a/client/errors.go b/client/errors.go index bf2b1ea..769b442 100644 --- a/client/errors.go +++ b/client/errors.go @@ -22,9 +22,7 @@ func IsErrContainerNotFound(err error) bool { switch unwrapErr(err).(type) { default: return false - case - apistatus.ContainerNotFound, - *apistatus.ContainerNotFound: + case *apistatus.ContainerNotFound: return true } } @@ -35,9 +33,7 @@ func IsErrEACLNotFound(err error) bool { switch unwrapErr(err).(type) { default: return false - case - apistatus.EACLNotFound, - *apistatus.EACLNotFound: + case *apistatus.EACLNotFound: return true } } @@ -48,9 +44,7 @@ func IsErrObjectNotFound(err error) bool { switch unwrapErr(err).(type) { default: return false - case - apistatus.ObjectNotFound, - *apistatus.ObjectNotFound: + case *apistatus.ObjectNotFound: return true } } @@ -61,9 +55,7 @@ func IsErrObjectAlreadyRemoved(err error) bool { switch unwrapErr(err).(type) { default: return false - case - apistatus.ObjectAlreadyRemoved, - *apistatus.ObjectAlreadyRemoved: + case *apistatus.ObjectAlreadyRemoved: return true } } @@ -74,9 +66,7 @@ func IsErrSessionExpired(err error) bool { switch unwrapErr(err).(type) { default: return false - case - apistatus.SessionTokenExpired, - *apistatus.SessionTokenExpired: + case *apistatus.SessionTokenExpired: return true } } @@ -87,9 +77,7 @@ func IsErrSessionNotFound(err error) bool { switch unwrapErr(err).(type) { default: return false - case - apistatus.SessionTokenNotFound, - *apistatus.SessionTokenNotFound: + case *apistatus.SessionTokenNotFound: return true } } diff --git a/client/errors_test.go b/client/errors_test.go index 657d0fe..90114d7 100644 --- a/client/errors_test.go +++ b/client/errors_test.go @@ -10,59 +10,40 @@ import ( ) func TestErrors(t *testing.T) { - for _, tc := range []struct { + errs := []struct { check func(error) bool - errs []error + err error }{ { check: client.IsErrContainerNotFound, - errs: []error{ - apistatus.ContainerNotFound{}, - new(apistatus.ContainerNotFound), - }, + err: new(apistatus.ContainerNotFound), }, { check: client.IsErrEACLNotFound, - errs: []error{ - apistatus.EACLNotFound{}, - new(apistatus.EACLNotFound), - }, + err: new(apistatus.EACLNotFound), }, { check: client.IsErrObjectNotFound, - errs: []error{ - apistatus.ObjectNotFound{}, - new(apistatus.ObjectNotFound), - }, + err: new(apistatus.ObjectNotFound), }, { check: client.IsErrObjectAlreadyRemoved, - errs: []error{ - apistatus.ObjectAlreadyRemoved{}, - new(apistatus.ObjectAlreadyRemoved), - }, + err: new(apistatus.ObjectAlreadyRemoved), }, { check: client.IsErrSessionExpired, - errs: []error{ - apistatus.SessionTokenExpired{}, - new(apistatus.SessionTokenExpired), - }, + err: new(apistatus.SessionTokenExpired), }, { check: client.IsErrSessionNotFound, - errs: []error{ - apistatus.SessionTokenNotFound{}, - new(apistatus.SessionTokenNotFound), - }, + err: new(apistatus.SessionTokenNotFound), }, - } { - require.NotEmpty(t, tc.errs) + } - for i := range tc.errs { - require.True(t, tc.check(tc.errs[i]), tc.errs[i]) - require.True(t, tc.check(fmt.Errorf("top-level context: :%w", - fmt.Errorf("inner context: %w", tc.errs[i])), - ), tc.errs[i]) + for i := range errs { + for j := range errs { + nestedErr := fmt.Errorf("top-level context: :%w", fmt.Errorf("inner context: %w", errs[j].err)) + require.Equal(t, i == j, errs[i].check(errs[j].err)) + require.Equal(t, i == j, errs[i].check(nestedErr)) } } } diff --git a/client/status/common.go b/client/status/common.go index 421d532..af0dc8e 100644 --- a/client/status/common.go +++ b/client/status/common.go @@ -14,7 +14,7 @@ type ServerInternal struct { v2 status.Status } -func (x ServerInternal) Error() string { +func (x *ServerInternal) Error() string { return errMessageStatusV2( globalizeCodeV2(status.Internal, status.GlobalizeCommonFail), x.v2.Message(), @@ -62,7 +62,7 @@ type WrongMagicNumber struct { v2 status.Status } -func (x WrongMagicNumber) Error() string { +func (x *WrongMagicNumber) Error() string { return errMessageStatusV2( globalizeCodeV2(status.WrongMagicNumber, status.GlobalizeCommonFail), x.v2.Message(), @@ -132,7 +132,7 @@ type SignatureVerification struct { const defaultSignatureVerificationMsg = "signature verification failed" -func (x SignatureVerification) Error() string { +func (x *SignatureVerification) Error() string { msg := x.v2.Message() if msg == "" { msg = defaultSignatureVerificationMsg @@ -191,7 +191,7 @@ type NodeUnderMaintenance struct { const defaultNodeUnderMaintenanceMsg = "node is under maintenance" // Error implements the error interface. -func (x NodeUnderMaintenance) Error() string { +func (x *NodeUnderMaintenance) Error() string { msg := x.Message() if msg == "" { msg = defaultNodeUnderMaintenanceMsg diff --git a/client/status/container.go b/client/status/container.go index c62aaac..9f06b27 100644 --- a/client/status/container.go +++ b/client/status/container.go @@ -13,7 +13,7 @@ type ContainerNotFound struct { const defaultContainerNotFoundMsg = "container not found" -func (x ContainerNotFound) Error() string { +func (x *ContainerNotFound) Error() string { msg := x.v2.Message() if msg == "" { msg = defaultContainerNotFoundMsg @@ -51,7 +51,7 @@ type EACLNotFound struct { const defaultEACLNotFoundMsg = "eACL not found" -func (x EACLNotFound) Error() string { +func (x *EACLNotFound) Error() string { msg := x.v2.Message() if msg == "" { msg = defaultEACLNotFoundMsg diff --git a/client/status/object.go b/client/status/object.go index 579cb93..27ea86c 100644 --- a/client/status/object.go +++ b/client/status/object.go @@ -13,7 +13,7 @@ type ObjectLocked struct { const defaultObjectLockedMsg = "object is locked" -func (x ObjectLocked) Error() string { +func (x *ObjectLocked) Error() string { msg := x.v2.Message() if msg == "" { msg = defaultObjectLockedMsg @@ -50,7 +50,7 @@ type LockNonRegularObject struct { const defaultLockNonRegularObjectMsg = "locking non-regular object is forbidden" -func (x LockNonRegularObject) Error() string { +func (x *LockNonRegularObject) Error() string { msg := x.v2.Message() if msg == "" { msg = defaultLockNonRegularObjectMsg @@ -87,7 +87,7 @@ type ObjectAccessDenied struct { const defaultObjectAccessDeniedMsg = "access to object operation denied" -func (x ObjectAccessDenied) Error() string { +func (x *ObjectAccessDenied) Error() string { msg := x.v2.Message() if msg == "" { msg = defaultObjectAccessDeniedMsg @@ -135,7 +135,7 @@ type ObjectNotFound struct { const defaultObjectNotFoundMsg = "object not found" -func (x ObjectNotFound) Error() string { +func (x *ObjectNotFound) Error() string { msg := x.v2.Message() if msg == "" { msg = defaultObjectNotFoundMsg @@ -172,7 +172,7 @@ type ObjectAlreadyRemoved struct { const defaultObjectAlreadyRemovedMsg = "object already removed" -func (x ObjectAlreadyRemoved) Error() string { +func (x *ObjectAlreadyRemoved) Error() string { msg := x.v2.Message() if msg == "" { msg = defaultObjectAlreadyRemovedMsg @@ -210,7 +210,7 @@ type ObjectOutOfRange struct { const defaultObjectOutOfRangeMsg = "out of range" -func (x ObjectOutOfRange) Error() string { +func (x *ObjectOutOfRange) Error() string { msg := x.v2.Message() if msg == "" { msg = defaultObjectOutOfRangeMsg diff --git a/client/status/session.go b/client/status/session.go index 6c54965..6f60758 100644 --- a/client/status/session.go +++ b/client/status/session.go @@ -13,7 +13,7 @@ type SessionTokenNotFound struct { const defaultSessionTokenNotFoundMsg = "session token not found" -func (x SessionTokenNotFound) Error() string { +func (x *SessionTokenNotFound) Error() string { msg := x.v2.Message() if msg == "" { msg = defaultSessionTokenNotFoundMsg @@ -50,7 +50,7 @@ type SessionTokenExpired struct { const defaultSessionTokenExpiredMsg = "expired session token" -func (x SessionTokenExpired) Error() string { +func (x *SessionTokenExpired) Error() string { msg := x.v2.Message() if msg == "" { msg = defaultSessionTokenExpiredMsg diff --git a/client/status/unrecognized.go b/client/status/unrecognized.go index b9a8cdb..19e481e 100644 --- a/client/status/unrecognized.go +++ b/client/status/unrecognized.go @@ -8,7 +8,7 @@ type unrecognizedStatusV2 struct { v2 status.Status } -func (x unrecognizedStatusV2) Error() string { +func (x *unrecognizedStatusV2) Error() string { return errMessageStatusV2("unrecognized", x.v2.Message()) } diff --git a/pool/pool.go b/pool/pool.go index 04b84aa..4f6a9ba 100644 --- a/pool/pool.go +++ b/pool/pool.go @@ -1014,10 +1014,10 @@ func (c *clientStatusMonitor) handleError(ctx context.Context, st apistatus.Stat err = apistatus.ErrFromStatus(st) switch err.(type) { - case apistatus.ServerInternal, *apistatus.ServerInternal, - apistatus.WrongMagicNumber, *apistatus.WrongMagicNumber, - apistatus.SignatureVerification, *apistatus.SignatureVerification, - apistatus.NodeUnderMaintenance, *apistatus.NodeUnderMaintenance: + case *apistatus.ServerInternal, + *apistatus.WrongMagicNumber, + *apistatus.SignatureVerification, + *apistatus.NodeUnderMaintenance: c.incErrorRate() } diff --git a/pool/pool_test.go b/pool/pool_test.go index bc0407e..623d429 100644 --- a/pool/pool_test.go +++ b/pool/pool_test.go @@ -271,7 +271,7 @@ func TestSessionCache(t *testing.T) { mockClientBuilder := func(addr string) client { mockCli := newMockClient(addr, *key) - mockCli.statusOnGetObject(apistatus.SessionTokenNotFound{}) + mockCli.statusOnGetObject(new(apistatus.SessionTokenNotFound)) return mockCli } @@ -548,14 +548,14 @@ func TestHandleError(t *testing.T) { }, { ctx: ctx, - status: apistatus.SuccessDefaultV2{}, + status: new(apistatus.SuccessDefaultV2), err: nil, expectedError: false, countError: false, }, { ctx: ctx, - status: apistatus.SuccessDefaultV2{}, + status: new(apistatus.SuccessDefaultV2), err: errors.New("error"), expectedError: true, countError: true, @@ -569,42 +569,42 @@ func TestHandleError(t *testing.T) { }, { ctx: ctx, - status: apistatus.ObjectNotFound{}, + status: new(apistatus.ObjectNotFound), err: nil, expectedError: true, countError: false, }, { ctx: ctx, - status: apistatus.ServerInternal{}, + status: new(apistatus.ServerInternal), err: nil, expectedError: true, countError: true, }, { ctx: ctx, - status: apistatus.WrongMagicNumber{}, + status: new(apistatus.WrongMagicNumber), err: nil, expectedError: true, countError: true, }, { ctx: ctx, - status: apistatus.SignatureVerification{}, + status: new(apistatus.SignatureVerification), err: nil, expectedError: true, countError: true, }, { ctx: ctx, - status: &apistatus.SignatureVerification{}, + status: new(apistatus.SignatureVerification), err: nil, expectedError: true, countError: true, }, { ctx: ctx, - status: apistatus.NodeUnderMaintenance{}, + status: new(apistatus.NodeUnderMaintenance), err: nil, expectedError: true, countError: true, @@ -649,7 +649,7 @@ func TestSwitchAfterErrorThreshold(t *testing.T) { if addr == nodes[0].address { mockCli := newMockClient(addr, *key) mockCli.setThreshold(uint32(errorThreshold)) - mockCli.statusOnGetObject(apistatus.ServerInternal{}) + mockCli.statusOnGetObject(new(apistatus.ServerInternal)) return mockCli }