diff --git a/client/status/common.go b/client/status/common.go index af0dc8e..598631b 100644 --- a/client/status/common.go +++ b/client/status/common.go @@ -238,3 +238,61 @@ func (x *NodeUnderMaintenance) SetMessage(v string) { func (x NodeUnderMaintenance) Message() string { return x.v2.Message() } + +// InvalidArgument describes failure status related to invalid argument. +// Instances provide Status and StatusV2 interfaces. +type InvalidArgument struct { + v2 status.Status +} + +const defaultInvalidArgumentMsg = "argument is invalid" + +// Error implements the error interface. +func (x *InvalidArgument) Error() string { + msg := x.v2.Message() + if msg == "" { + msg = defaultInvalidArgumentMsg + } + + return errMessageStatusV2( + globalizeCodeV2(status.InvalidArgument, status.GlobalizeCommonFail), + msg, + ) +} + +// implements local interface defined in FromStatusV2 func. +func (x *InvalidArgument) fromStatusV2(st *status.Status) { + x.v2 = *st +} + +// ToStatusV2 implements StatusV2 interface method. +// If the value was returned by FromStatusV2, returns the source message. +// Otherwise, returns message with +// - code: INVALID_ARGUMENT; +// - string message: written message via SetMessage or +// "argument is invalid" as a default message; +// - details: empty. +func (x InvalidArgument) ToStatusV2() *status.Status { + x.v2.SetCode(globalizeCodeV2(status.InvalidArgument, status.GlobalizeCommonFail)) + if x.v2.Message() == "" { + x.v2.SetMessage(defaultInvalidArgumentMsg) + } + + return &x.v2 +} + +// SetMessage writes invalid argument failure message. +// Message should be used for debug purposes only. +// +// See also Message. +func (x *InvalidArgument) SetMessage(v string) { + x.v2.SetMessage(v) +} + +// Message returns status message. Zero status returns empty message. +// Message should be used for debug purposes only. +// +// See also SetMessage. +func (x InvalidArgument) Message() string { + return x.v2.Message() +} diff --git a/client/status/common_test.go b/client/status/common_test.go index d66b5b5..61125d9 100644 --- a/client/status/common_test.go +++ b/client/status/common_test.go @@ -128,3 +128,42 @@ func TestNodeUnderMaintenance(t *testing.T) { require.Equal(t, msg, stV2.Message()) }) } + +func TestInvalidArgument(t *testing.T) { + t.Run("default", func(t *testing.T) { + var st apistatus.InvalidArgument + + require.Empty(t, st.Message()) + }) + + t.Run("custom message", func(t *testing.T) { + var st apistatus.InvalidArgument + msg := "some message" + + st.SetMessage(msg) + + stV2 := st.ToStatusV2() + + require.Equal(t, msg, st.Message()) + require.Equal(t, msg, stV2.Message()) + }) + + t.Run("empty to V2", func(t *testing.T) { + var st apistatus.InvalidArgument + + stV2 := st.ToStatusV2() + + require.Equal(t, "argument is invalid", stV2.Message()) + }) + + t.Run("non-empty to V2", func(t *testing.T) { + var st apistatus.InvalidArgument + msg := "some other msg" + + st.SetMessage(msg) + + stV2 := st.ToStatusV2() + + require.Equal(t, msg, stV2.Message()) + }) +} diff --git a/client/status/v2.go b/client/status/v2.go index f5bef6a..5cee3be 100644 --- a/client/status/v2.go +++ b/client/status/v2.go @@ -33,12 +33,29 @@ type StatusV2 interface { // // Common failures: // - status.Internal: *ServerInternal; -// - status.SignatureVerificationFail: *SignatureVerification. +// - status.WrongMagicNumber: *WrongMagicNumber; +// - status.SignatureVerificationFail: *SignatureVerification; +// - status.NodeUnderMaintenance: *NodeUnderMaintenance; +// - status.InvalidArgument: *InvalidArgument. // // Object failures: // - object.StatusLocked: *ObjectLocked; -// - object.StatusLockNonRegularObject: *LockNonRegularObject. -// - object.StatusAccessDenied: *ObjectAccessDenied. +// - object.StatusLockNonRegularObject: *LockNonRegularObject; +// - object.StatusAccessDenied: *ObjectAccessDenied; +// - object.StatusNotFound: *ObjectNotFound; +// - object.StatusAlreadyRemoved: *ObjectAlreadyRemoved; +// - object.StatusOutOfRange: *ObjectOutOfRange. +// +// Container failures: +// - container.StatusNotFound: *ContainerNotFound; +// - container.StatusEACLNotFound: *EACLNotFound. +// +// Session failures: +// - session.StatusTokenNotFound: *SessionTokenNotFound; +// - session.StatusTokenExpired: *SessionTokenExpired. +// +// APE Manager failures +// - apemanager.StatusAPEManagerAccessDenied: *APEManagerAccessDenied. func FromStatusV2(st *status.Status) Status { var decoder interface { fromStatusV2(*status.Status) @@ -61,6 +78,8 @@ func FromStatusV2(st *status.Status) Status { decoder = new(SignatureVerification) case status.NodeUnderMaintenance: decoder = new(NodeUnderMaintenance) + case status.InvalidArgument: + decoder = new(InvalidArgument) } case object.LocalizeFailStatus(&code): switch code { diff --git a/client/status/v2_test.go b/client/status/v2_test.go index 2295c47..242c365 100644 --- a/client/status/v2_test.go +++ b/client/status/v2_test.go @@ -137,6 +137,12 @@ func TestToStatusV2(t *testing.T) { }), codeV2: 1027, }, + { + status: (statusConstructor)(func() apistatus.Status { + return new(apistatus.InvalidArgument) + }), + codeV2: 1028, + }, } { var st apistatus.Status diff --git a/pool/pool_test.go b/pool/pool_test.go index 656524a..0c61115 100644 --- a/pool/pool_test.go +++ b/pool/pool_test.go @@ -649,17 +649,17 @@ func TestHandleError(t *testing.T) { }, { ctx: ctx, - status: new(apistatus.SignatureVerification), + status: new(apistatus.NodeUnderMaintenance), err: nil, expectedError: true, countError: true, }, { ctx: ctx, - status: new(apistatus.NodeUnderMaintenance), + status: new(apistatus.InvalidArgument), err: nil, expectedError: true, - countError: true, + countError: false, }, { ctx: canceledCtx,