From 12aa7ae14491fc38540edf0419b9d33592cb9d6d Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 28 Feb 2022 13:19:54 +0300 Subject: [PATCH] [#140] apistatus: Support session errors Define `SessionTokenNotFound`/`SessionTokenExpired` types for `TOKEN_NOT_FOUND`/`TOKEN_EXPIRED` codes. Signed-off-by: Leonard Lyubich --- client/status/session.go | 66 ++++++++++++++++++++++++++++++++++++++++ client/status/v2.go | 9 ++++++ client/status/v2_test.go | 24 +++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 client/status/session.go diff --git a/client/status/session.go b/client/status/session.go new file mode 100644 index 0000000..102b512 --- /dev/null +++ b/client/status/session.go @@ -0,0 +1,66 @@ +package apistatus + +import ( + "github.com/nspcc-dev/neofs-api-go/v2/session" + "github.com/nspcc-dev/neofs-api-go/v2/status" +) + +// SessionTokenNotFound describes status of the failure because of the missing session token. +// Instances provide Status and StatusV2 interfaces. +type SessionTokenNotFound struct { + v2 status.Status +} + +func (x SessionTokenNotFound) Error() string { + return errMessageStatusV2( + globalizeCodeV2(session.StatusTokenNotFound, session.GlobalizeFail), + x.v2.Message(), + ) +} + +// implements local interface defined in FromStatusV2 func. +func (x *SessionTokenNotFound) 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: TOKEN_NOT_FOUND; +// * string message: "session token not found"; +// * details: empty. +func (x SessionTokenNotFound) ToStatusV2() *status.Status { + x.v2.SetCode(globalizeCodeV2(session.StatusTokenNotFound, session.GlobalizeFail)) + x.v2.SetMessage("session token not found") + return &x.v2 +} + +// SessionTokenExpired describes status of the failure because of the expired session token. +// Instances provide Status and StatusV2 interfaces. +type SessionTokenExpired struct { + v2 status.Status +} + +func (x SessionTokenExpired) Error() string { + return errMessageStatusV2( + globalizeCodeV2(session.StatusTokenExpired, session.GlobalizeFail), + x.v2.Message(), + ) +} + +// implements local interface defined in FromStatusV2 func. +func (x *SessionTokenExpired) 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: TOKEN_EXPIRED; +// * string message: "expired session token"; +// * details: empty. +func (x SessionTokenExpired) ToStatusV2() *status.Status { + x.v2.SetCode(globalizeCodeV2(session.StatusTokenExpired, session.GlobalizeFail)) + x.v2.SetMessage("expired session token") + return &x.v2 +} diff --git a/client/status/v2.go b/client/status/v2.go index b573e13..2890f8c 100644 --- a/client/status/v2.go +++ b/client/status/v2.go @@ -5,6 +5,7 @@ import ( "github.com/nspcc-dev/neofs-api-go/v2/container" "github.com/nspcc-dev/neofs-api-go/v2/object" + "github.com/nspcc-dev/neofs-api-go/v2/session" "github.com/nspcc-dev/neofs-api-go/v2/status" ) @@ -74,6 +75,14 @@ func FromStatusV2(st *status.Status) Status { case container.StatusNotFound: decoder = new(ContainerNotFound) } + case session.LocalizeFailStatus(&code): + //nolint:exhaustive + switch code { + case session.StatusTokenNotFound: + decoder = new(SessionTokenNotFound) + case session.StatusTokenExpired: + decoder = new(SessionTokenExpired) + } } if decoder == nil { diff --git a/client/status/v2_test.go b/client/status/v2_test.go index 0ee0723..b80486c 100644 --- a/client/status/v2_test.go +++ b/client/status/v2_test.go @@ -99,6 +99,18 @@ func TestToStatusV2(t *testing.T) { }), codeV2: 3072, }, + { + status: (statusConstructor)(func() apistatus.Status { + return new(apistatus.SessionTokenNotFound) + }), + codeV2: 4096, + }, + { + status: (statusConstructor)(func() apistatus.Status { + return new(apistatus.SessionTokenExpired) + }), + codeV2: 4097, + }, } { var st apistatus.Status @@ -217,6 +229,18 @@ func TestFromStatusV2(t *testing.T) { }), codeV2: 3072, }, + { + status: (statusConstructor)(func() apistatus.Status { + return new(apistatus.SessionTokenNotFound) + }), + codeV2: 4096, + }, + { + status: (statusConstructor)(func() apistatus.Status { + return new(apistatus.SessionTokenExpired) + }), + codeV2: 4097, + }, } { var st apistatus.Status