From c62764879814b65db8b4e1bf9dcabb5f16366a43 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 28 Feb 2022 11:55:25 +0300 Subject: [PATCH] [#140] apistatus: Support `CONTAINER_NOT_FOUND` error Define `ContainerNotFound` type for `CONTAINER_NOT_FOUND` code. Signed-off-by: Leonard Lyubich --- client/status/container.go | 36 ++++++++++++++++++++++++++++++++++++ client/status/v2.go | 7 +++++++ client/status/v2_test.go | 12 ++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 client/status/container.go diff --git a/client/status/container.go b/client/status/container.go new file mode 100644 index 0000000..7f71114 --- /dev/null +++ b/client/status/container.go @@ -0,0 +1,36 @@ +package apistatus + +import ( + "github.com/nspcc-dev/neofs-api-go/v2/container" + "github.com/nspcc-dev/neofs-api-go/v2/status" +) + +// ContainerNotFound describes status of the failure because of the missing container. +// Instances provide Status and StatusV2 interfaces. +type ContainerNotFound struct { + v2 status.Status +} + +func (x ContainerNotFound) Error() string { + return errMessageStatusV2( + globalizeCodeV2(container.StatusNotFound, container.GlobalizeFail), + x.v2.Message(), + ) +} + +// implements local interface defined in FromStatusV2 func. +func (x *ContainerNotFound) 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: CONTAINER_NOT_FOUND; +// * string message: "container not found"; +// * details: empty. +func (x ContainerNotFound) ToStatusV2() *status.Status { + x.v2.SetCode(globalizeCodeV2(container.StatusNotFound, container.GlobalizeFail)) + x.v2.SetMessage("container not found") + return &x.v2 +} diff --git a/client/status/v2.go b/client/status/v2.go index bf219a8..b573e13 100644 --- a/client/status/v2.go +++ b/client/status/v2.go @@ -3,6 +3,7 @@ package apistatus import ( "fmt" + "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/status" ) @@ -67,6 +68,12 @@ func FromStatusV2(st *status.Status) Status { case object.StatusAlreadyRemoved: decoder = new(ObjectAlreadyRemoved) } + case container.LocalizeFailStatus(&code): + //nolint:exhaustive + switch code { + case container.StatusNotFound: + decoder = new(ContainerNotFound) + } } if decoder == nil { diff --git a/client/status/v2_test.go b/client/status/v2_test.go index 46379ff..0ee0723 100644 --- a/client/status/v2_test.go +++ b/client/status/v2_test.go @@ -93,6 +93,12 @@ func TestToStatusV2(t *testing.T) { }), codeV2: 2052, }, + { + status: (statusConstructor)(func() apistatus.Status { + return new(apistatus.ContainerNotFound) + }), + codeV2: 3072, + }, } { var st apistatus.Status @@ -205,6 +211,12 @@ func TestFromStatusV2(t *testing.T) { }), codeV2: 2052, }, + { + status: (statusConstructor)(func() apistatus.Status { + return new(apistatus.ContainerNotFound) + }), + codeV2: 3072, + }, } { var st apistatus.Status