From f1e46d35dee12b2353bfaf12e08d2fe11be8eecb Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 28 Feb 2022 11:49:34 +0300 Subject: [PATCH] [#140] apistatus: Support `OBJECT_ALREADY_REMOVED` error Define `ObjectAlreadyRemoved` type for `OBJECT_ALREADY_REMOVED` code. Signed-off-by: Leonard Lyubich --- client/status/object.go | 30 ++++++++++++++++++++++++++++++ client/status/v2.go | 2 ++ client/status/v2_test.go | 12 ++++++++++++ 3 files changed, 44 insertions(+) diff --git a/client/status/object.go b/client/status/object.go index 1ecfd235..cf6674a6 100644 --- a/client/status/object.go +++ b/client/status/object.go @@ -135,3 +135,33 @@ func (x ObjectNotFound) ToStatusV2() *status.Status { x.v2.SetMessage("object not found") return &x.v2 } + +// ObjectAlreadyRemoved describes status of the failure because object has been +// already removed. Instances provide Status and StatusV2 interfaces. +type ObjectAlreadyRemoved struct { + v2 status.Status +} + +func (x ObjectAlreadyRemoved) Error() string { + return errMessageStatusV2( + globalizeCodeV2(object.StatusAlreadyRemoved, object.GlobalizeFail), + x.v2.Message(), + ) +} + +// implements local interface defined in FromStatusV2 func. +func (x *ObjectAlreadyRemoved) 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: OBJECT_ALREADY_REMOVED; +// * string message: "object already removed"; +// * details: empty. +func (x ObjectAlreadyRemoved) ToStatusV2() *status.Status { + x.v2.SetCode(globalizeCodeV2(object.StatusAlreadyRemoved, object.GlobalizeFail)) + x.v2.SetMessage("object already removed") + return &x.v2 +} diff --git a/client/status/v2.go b/client/status/v2.go index ae58193d..bf219a8d 100644 --- a/client/status/v2.go +++ b/client/status/v2.go @@ -64,6 +64,8 @@ func FromStatusV2(st *status.Status) Status { decoder = new(ObjectAccessDenied) case object.StatusNotFound: decoder = new(ObjectNotFound) + case object.StatusAlreadyRemoved: + decoder = new(ObjectAlreadyRemoved) } } diff --git a/client/status/v2_test.go b/client/status/v2_test.go index 7fe7935c..46379ff2 100644 --- a/client/status/v2_test.go +++ b/client/status/v2_test.go @@ -87,6 +87,12 @@ func TestToStatusV2(t *testing.T) { }), codeV2: 2049, }, + { + status: (statusConstructor)(func() apistatus.Status { + return new(apistatus.ObjectAlreadyRemoved) + }), + codeV2: 2052, + }, } { var st apistatus.Status @@ -193,6 +199,12 @@ func TestFromStatusV2(t *testing.T) { }), codeV2: 2049, }, + { + status: (statusConstructor)(func() apistatus.Status { + return new(apistatus.ObjectAlreadyRemoved) + }), + codeV2: 2052, + }, } { var st apistatus.Status