forked from TrueCloudLab/frostfs-sdk-go
[#274] client/status: Support INVALID_ARGUMENT status
Signed-off-by: Aleksey Savchuk <a.savchuk@yadro.com>
This commit is contained in:
parent
b9092aeb0c
commit
d00892f418
5 changed files with 128 additions and 6 deletions
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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())
|
||||
})
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue