[#135] Make all error status receivers pointers

Signed-off-by: Alejandro Lopez <a.lopez@yadro.com>
This commit is contained in:
Alejandro Lopez 2023-08-02 10:01:03 +03:00 committed by Evgenii Stratonikov
parent 55c52c8d5d
commit 3dc8129ed7
9 changed files with 49 additions and 80 deletions

View file

@ -22,9 +22,7 @@ func IsErrContainerNotFound(err error) bool {
switch unwrapErr(err).(type) { switch unwrapErr(err).(type) {
default: default:
return false return false
case case *apistatus.ContainerNotFound:
apistatus.ContainerNotFound,
*apistatus.ContainerNotFound:
return true return true
} }
} }
@ -35,9 +33,7 @@ func IsErrEACLNotFound(err error) bool {
switch unwrapErr(err).(type) { switch unwrapErr(err).(type) {
default: default:
return false return false
case case *apistatus.EACLNotFound:
apistatus.EACLNotFound,
*apistatus.EACLNotFound:
return true return true
} }
} }
@ -48,9 +44,7 @@ func IsErrObjectNotFound(err error) bool {
switch unwrapErr(err).(type) { switch unwrapErr(err).(type) {
default: default:
return false return false
case case *apistatus.ObjectNotFound:
apistatus.ObjectNotFound,
*apistatus.ObjectNotFound:
return true return true
} }
} }
@ -61,9 +55,7 @@ func IsErrObjectAlreadyRemoved(err error) bool {
switch unwrapErr(err).(type) { switch unwrapErr(err).(type) {
default: default:
return false return false
case case *apistatus.ObjectAlreadyRemoved:
apistatus.ObjectAlreadyRemoved,
*apistatus.ObjectAlreadyRemoved:
return true return true
} }
} }
@ -74,9 +66,7 @@ func IsErrSessionExpired(err error) bool {
switch unwrapErr(err).(type) { switch unwrapErr(err).(type) {
default: default:
return false return false
case case *apistatus.SessionTokenExpired:
apistatus.SessionTokenExpired,
*apistatus.SessionTokenExpired:
return true return true
} }
} }
@ -87,9 +77,7 @@ func IsErrSessionNotFound(err error) bool {
switch unwrapErr(err).(type) { switch unwrapErr(err).(type) {
default: default:
return false return false
case case *apistatus.SessionTokenNotFound:
apistatus.SessionTokenNotFound,
*apistatus.SessionTokenNotFound:
return true return true
} }
} }

View file

@ -10,59 +10,40 @@ import (
) )
func TestErrors(t *testing.T) { func TestErrors(t *testing.T) {
for _, tc := range []struct { errs := []struct {
check func(error) bool check func(error) bool
errs []error err error
}{ }{
{ {
check: client.IsErrContainerNotFound, check: client.IsErrContainerNotFound,
errs: []error{ err: new(apistatus.ContainerNotFound),
apistatus.ContainerNotFound{},
new(apistatus.ContainerNotFound),
},
}, },
{ {
check: client.IsErrEACLNotFound, check: client.IsErrEACLNotFound,
errs: []error{ err: new(apistatus.EACLNotFound),
apistatus.EACLNotFound{},
new(apistatus.EACLNotFound),
},
}, },
{ {
check: client.IsErrObjectNotFound, check: client.IsErrObjectNotFound,
errs: []error{ err: new(apistatus.ObjectNotFound),
apistatus.ObjectNotFound{},
new(apistatus.ObjectNotFound),
},
}, },
{ {
check: client.IsErrObjectAlreadyRemoved, check: client.IsErrObjectAlreadyRemoved,
errs: []error{ err: new(apistatus.ObjectAlreadyRemoved),
apistatus.ObjectAlreadyRemoved{},
new(apistatus.ObjectAlreadyRemoved),
},
}, },
{ {
check: client.IsErrSessionExpired, check: client.IsErrSessionExpired,
errs: []error{ err: new(apistatus.SessionTokenExpired),
apistatus.SessionTokenExpired{},
new(apistatus.SessionTokenExpired),
},
}, { }, {
check: client.IsErrSessionNotFound, check: client.IsErrSessionNotFound,
errs: []error{ err: new(apistatus.SessionTokenNotFound),
apistatus.SessionTokenNotFound{},
new(apistatus.SessionTokenNotFound),
},
}, },
} { }
require.NotEmpty(t, tc.errs)
for i := range tc.errs { for i := range errs {
require.True(t, tc.check(tc.errs[i]), tc.errs[i]) for j := range errs {
require.True(t, tc.check(fmt.Errorf("top-level context: :%w", nestedErr := fmt.Errorf("top-level context: :%w", fmt.Errorf("inner context: %w", errs[j].err))
fmt.Errorf("inner context: %w", tc.errs[i])), require.Equal(t, i == j, errs[i].check(errs[j].err))
), tc.errs[i]) require.Equal(t, i == j, errs[i].check(nestedErr))
} }
} }
} }

View file

@ -14,7 +14,7 @@ type ServerInternal struct {
v2 status.Status v2 status.Status
} }
func (x ServerInternal) Error() string { func (x *ServerInternal) Error() string {
return errMessageStatusV2( return errMessageStatusV2(
globalizeCodeV2(status.Internal, status.GlobalizeCommonFail), globalizeCodeV2(status.Internal, status.GlobalizeCommonFail),
x.v2.Message(), x.v2.Message(),
@ -62,7 +62,7 @@ type WrongMagicNumber struct {
v2 status.Status v2 status.Status
} }
func (x WrongMagicNumber) Error() string { func (x *WrongMagicNumber) Error() string {
return errMessageStatusV2( return errMessageStatusV2(
globalizeCodeV2(status.WrongMagicNumber, status.GlobalizeCommonFail), globalizeCodeV2(status.WrongMagicNumber, status.GlobalizeCommonFail),
x.v2.Message(), x.v2.Message(),
@ -132,7 +132,7 @@ type SignatureVerification struct {
const defaultSignatureVerificationMsg = "signature verification failed" const defaultSignatureVerificationMsg = "signature verification failed"
func (x SignatureVerification) Error() string { func (x *SignatureVerification) Error() string {
msg := x.v2.Message() msg := x.v2.Message()
if msg == "" { if msg == "" {
msg = defaultSignatureVerificationMsg msg = defaultSignatureVerificationMsg
@ -191,7 +191,7 @@ type NodeUnderMaintenance struct {
const defaultNodeUnderMaintenanceMsg = "node is under maintenance" const defaultNodeUnderMaintenanceMsg = "node is under maintenance"
// Error implements the error interface. // Error implements the error interface.
func (x NodeUnderMaintenance) Error() string { func (x *NodeUnderMaintenance) Error() string {
msg := x.Message() msg := x.Message()
if msg == "" { if msg == "" {
msg = defaultNodeUnderMaintenanceMsg msg = defaultNodeUnderMaintenanceMsg

View file

@ -13,7 +13,7 @@ type ContainerNotFound struct {
const defaultContainerNotFoundMsg = "container not found" const defaultContainerNotFoundMsg = "container not found"
func (x ContainerNotFound) Error() string { func (x *ContainerNotFound) Error() string {
msg := x.v2.Message() msg := x.v2.Message()
if msg == "" { if msg == "" {
msg = defaultContainerNotFoundMsg msg = defaultContainerNotFoundMsg
@ -51,7 +51,7 @@ type EACLNotFound struct {
const defaultEACLNotFoundMsg = "eACL not found" const defaultEACLNotFoundMsg = "eACL not found"
func (x EACLNotFound) Error() string { func (x *EACLNotFound) Error() string {
msg := x.v2.Message() msg := x.v2.Message()
if msg == "" { if msg == "" {
msg = defaultEACLNotFoundMsg msg = defaultEACLNotFoundMsg

View file

@ -13,7 +13,7 @@ type ObjectLocked struct {
const defaultObjectLockedMsg = "object is locked" const defaultObjectLockedMsg = "object is locked"
func (x ObjectLocked) Error() string { func (x *ObjectLocked) Error() string {
msg := x.v2.Message() msg := x.v2.Message()
if msg == "" { if msg == "" {
msg = defaultObjectLockedMsg msg = defaultObjectLockedMsg
@ -50,7 +50,7 @@ type LockNonRegularObject struct {
const defaultLockNonRegularObjectMsg = "locking non-regular object is forbidden" const defaultLockNonRegularObjectMsg = "locking non-regular object is forbidden"
func (x LockNonRegularObject) Error() string { func (x *LockNonRegularObject) Error() string {
msg := x.v2.Message() msg := x.v2.Message()
if msg == "" { if msg == "" {
msg = defaultLockNonRegularObjectMsg msg = defaultLockNonRegularObjectMsg
@ -87,7 +87,7 @@ type ObjectAccessDenied struct {
const defaultObjectAccessDeniedMsg = "access to object operation denied" const defaultObjectAccessDeniedMsg = "access to object operation denied"
func (x ObjectAccessDenied) Error() string { func (x *ObjectAccessDenied) Error() string {
msg := x.v2.Message() msg := x.v2.Message()
if msg == "" { if msg == "" {
msg = defaultObjectAccessDeniedMsg msg = defaultObjectAccessDeniedMsg
@ -135,7 +135,7 @@ type ObjectNotFound struct {
const defaultObjectNotFoundMsg = "object not found" const defaultObjectNotFoundMsg = "object not found"
func (x ObjectNotFound) Error() string { func (x *ObjectNotFound) Error() string {
msg := x.v2.Message() msg := x.v2.Message()
if msg == "" { if msg == "" {
msg = defaultObjectNotFoundMsg msg = defaultObjectNotFoundMsg
@ -172,7 +172,7 @@ type ObjectAlreadyRemoved struct {
const defaultObjectAlreadyRemovedMsg = "object already removed" const defaultObjectAlreadyRemovedMsg = "object already removed"
func (x ObjectAlreadyRemoved) Error() string { func (x *ObjectAlreadyRemoved) Error() string {
msg := x.v2.Message() msg := x.v2.Message()
if msg == "" { if msg == "" {
msg = defaultObjectAlreadyRemovedMsg msg = defaultObjectAlreadyRemovedMsg
@ -210,7 +210,7 @@ type ObjectOutOfRange struct {
const defaultObjectOutOfRangeMsg = "out of range" const defaultObjectOutOfRangeMsg = "out of range"
func (x ObjectOutOfRange) Error() string { func (x *ObjectOutOfRange) Error() string {
msg := x.v2.Message() msg := x.v2.Message()
if msg == "" { if msg == "" {
msg = defaultObjectOutOfRangeMsg msg = defaultObjectOutOfRangeMsg

View file

@ -13,7 +13,7 @@ type SessionTokenNotFound struct {
const defaultSessionTokenNotFoundMsg = "session token not found" const defaultSessionTokenNotFoundMsg = "session token not found"
func (x SessionTokenNotFound) Error() string { func (x *SessionTokenNotFound) Error() string {
msg := x.v2.Message() msg := x.v2.Message()
if msg == "" { if msg == "" {
msg = defaultSessionTokenNotFoundMsg msg = defaultSessionTokenNotFoundMsg
@ -50,7 +50,7 @@ type SessionTokenExpired struct {
const defaultSessionTokenExpiredMsg = "expired session token" const defaultSessionTokenExpiredMsg = "expired session token"
func (x SessionTokenExpired) Error() string { func (x *SessionTokenExpired) Error() string {
msg := x.v2.Message() msg := x.v2.Message()
if msg == "" { if msg == "" {
msg = defaultSessionTokenExpiredMsg msg = defaultSessionTokenExpiredMsg

View file

@ -8,7 +8,7 @@ type unrecognizedStatusV2 struct {
v2 status.Status v2 status.Status
} }
func (x unrecognizedStatusV2) Error() string { func (x *unrecognizedStatusV2) Error() string {
return errMessageStatusV2("unrecognized", x.v2.Message()) return errMessageStatusV2("unrecognized", x.v2.Message())
} }

View file

@ -1015,10 +1015,10 @@ func (c *clientStatusMonitor) handleError(ctx context.Context, st apistatus.Stat
err = apistatus.ErrFromStatus(st) err = apistatus.ErrFromStatus(st)
switch err.(type) { switch err.(type) {
case apistatus.ServerInternal, *apistatus.ServerInternal, case *apistatus.ServerInternal,
apistatus.WrongMagicNumber, *apistatus.WrongMagicNumber, *apistatus.WrongMagicNumber,
apistatus.SignatureVerification, *apistatus.SignatureVerification, *apistatus.SignatureVerification,
apistatus.NodeUnderMaintenance, *apistatus.NodeUnderMaintenance: *apistatus.NodeUnderMaintenance:
c.incErrorRate() c.incErrorRate()
} }

View file

@ -271,7 +271,7 @@ func TestSessionCache(t *testing.T) {
mockClientBuilder := func(addr string) client { mockClientBuilder := func(addr string) client {
mockCli := newMockClient(addr, *key) mockCli := newMockClient(addr, *key)
mockCli.statusOnGetObject(apistatus.SessionTokenNotFound{}) mockCli.statusOnGetObject(new(apistatus.SessionTokenNotFound))
return mockCli return mockCli
} }
@ -548,14 +548,14 @@ func TestHandleError(t *testing.T) {
}, },
{ {
ctx: ctx, ctx: ctx,
status: apistatus.SuccessDefaultV2{}, status: new(apistatus.SuccessDefaultV2),
err: nil, err: nil,
expectedError: false, expectedError: false,
countError: false, countError: false,
}, },
{ {
ctx: ctx, ctx: ctx,
status: apistatus.SuccessDefaultV2{}, status: new(apistatus.SuccessDefaultV2),
err: errors.New("error"), err: errors.New("error"),
expectedError: true, expectedError: true,
countError: true, countError: true,
@ -569,42 +569,42 @@ func TestHandleError(t *testing.T) {
}, },
{ {
ctx: ctx, ctx: ctx,
status: apistatus.ObjectNotFound{}, status: new(apistatus.ObjectNotFound),
err: nil, err: nil,
expectedError: true, expectedError: true,
countError: false, countError: false,
}, },
{ {
ctx: ctx, ctx: ctx,
status: apistatus.ServerInternal{}, status: new(apistatus.ServerInternal),
err: nil, err: nil,
expectedError: true, expectedError: true,
countError: true, countError: true,
}, },
{ {
ctx: ctx, ctx: ctx,
status: apistatus.WrongMagicNumber{}, status: new(apistatus.WrongMagicNumber),
err: nil, err: nil,
expectedError: true, expectedError: true,
countError: true, countError: true,
}, },
{ {
ctx: ctx, ctx: ctx,
status: apistatus.SignatureVerification{}, status: new(apistatus.SignatureVerification),
err: nil, err: nil,
expectedError: true, expectedError: true,
countError: true, countError: true,
}, },
{ {
ctx: ctx, ctx: ctx,
status: &apistatus.SignatureVerification{}, status: new(apistatus.SignatureVerification),
err: nil, err: nil,
expectedError: true, expectedError: true,
countError: true, countError: true,
}, },
{ {
ctx: ctx, ctx: ctx,
status: apistatus.NodeUnderMaintenance{}, status: new(apistatus.NodeUnderMaintenance),
err: nil, err: nil,
expectedError: true, expectedError: true,
countError: true, countError: true,
@ -649,7 +649,7 @@ func TestSwitchAfterErrorThreshold(t *testing.T) {
if addr == nodes[0].address { if addr == nodes[0].address {
mockCli := newMockClient(addr, *key) mockCli := newMockClient(addr, *key)
mockCli.setThreshold(uint32(errorThreshold)) mockCli.setThreshold(uint32(errorThreshold))
mockCli.statusOnGetObject(apistatus.ServerInternal{}) mockCli.statusOnGetObject(new(apistatus.ServerInternal))
return mockCli return mockCli
} }