From 277b8f7de4709bac57866e1feab050190c9e0d76 Mon Sep 17 00:00:00 2001 From: Evgenii Baidakov Date: Mon, 22 May 2023 16:07:57 +0400 Subject: [PATCH] client: Remove ErrFromStatus function Signed-off-by: Evgenii Baidakov --- client/common.go | 9 ++++++--- client/object_put.go | 9 ++++----- client/status/status.go | 18 ++---------------- client/status/v2_test.go | 18 ++++++++++-------- pool/mock_test.go | 14 ++++++-------- 5 files changed, 28 insertions(+), 40 deletions(-) diff --git a/client/common.go b/client/common.go index e22a8a4..7399d42 100644 --- a/client/common.go +++ b/client/common.go @@ -207,13 +207,16 @@ func (x *contextCall) processResponse() bool { // processResponse verifies response signature. func (c *Client) processResponse(resp responseV2) error { - err := verifyServiceMessage(resp) - if err != nil { + if err := verifyServiceMessage(resp); err != nil { return fmt.Errorf("invalid response signature: %w", err) } st := apistatus.FromStatusV2(resp.GetMetaHeader().GetStatus()) - return apistatus.ErrFromStatus(st) + if err, ok := st.(error); ok { + return err + } + + return nil } // reads response (if rResp is set) and processes it. Result means success. diff --git a/client/object_put.go b/client/object_put.go index b495a6b..806b502 100644 --- a/client/object_put.go +++ b/client/object_put.go @@ -12,7 +12,6 @@ import ( "github.com/nspcc-dev/neofs-api-go/v2/rpc/client" v2session "github.com/nspcc-dev/neofs-api-go/v2/session" "github.com/nspcc-dev/neofs-sdk-go/bearer" - apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto" "github.com/nspcc-dev/neofs-sdk-go/object" @@ -274,12 +273,12 @@ func (x *objectWriter) InitDataStream(header object.Object) (io.Writer, error) { }, nil } - res, err := stream.Close() + _, err = stream.Close() if err != nil { return nil, err } - return nil, apistatus.ErrFromStatus(res) + return nil, errors.New("unexpected error") } type payloadWriter struct { @@ -295,12 +294,12 @@ func (x *payloadWriter) Write(p []byte) (int, error) { } func (x *payloadWriter) Close() error { - res, err := x.stream.Close() + _, err := x.stream.Close() if err != nil { return err } - return apistatus.ErrFromStatus(res) + return nil } // CreateObject creates new NeoFS object with given payload data and stores it diff --git a/client/status/status.go b/client/status/status.go index fb77310..ea352bf 100644 --- a/client/status/status.go +++ b/client/status/status.go @@ -6,24 +6,10 @@ package apistatus // - statuses that implement the build-in error interface are considered failed statuses; // - all other value types are considered successes (nil is a default success). // -// In Go code type of success can be determined by a type switch, failure - by a switch with errors.As calls. +// In Go code type of success can be determined by a type switch, failure - by a switch with [errors.As] calls. // Nil should be considered as a success, and default switch section - as an unrecognized Status. // -// To convert statuses into errors and vice versa, use functions ErrToStatus and ErrFromStatus, respectively. -// ErrFromStatus function returns nil for successful statuses. However, to simplify the check of statuses for success, -// IsSuccessful function should be used (try to avoid nil comparison). // It should be noted that using direct typecasting is not a compatible approach. // -// To transport statuses using the NeoFS API V2 protocol, see StatusV2 interface and FromStatusV2 and ToStatusV2 functions. +// To transport statuses using the NeoFS API V2 protocol, see [StatusV2] interface and [FromStatusV2] and [ToStatusV2] functions. type Status any - -// ErrFromStatus converts Status instance to error if it is failed. Returns nil on successful Status. -// -// Note: direct assignment may not be compatibility-safe. -func ErrFromStatus(st Status) error { - if err, ok := st.(error); ok { - return err - } - - return nil -} diff --git a/client/status/v2_test.go b/client/status/v2_test.go index 8993153..0eb9dc8 100644 --- a/client/status/v2_test.go +++ b/client/status/v2_test.go @@ -224,15 +224,17 @@ func TestFromStatusV2(t *testing.T) { } randomError := errors.New("garbage") - errFromStatus := apistatus.ErrFromStatus(st) + // some `st` in test-cases is int, bool, string + errFromStatus, ok := st.(error) + if ok { + for _, err := range testItem.compatibleErrs { + require.ErrorIs(t, errFromStatus, err) + require.NotErrorIs(t, randomError, err) + } - for _, err := range testItem.compatibleErrs { - require.ErrorIs(t, errFromStatus, err) - require.NotErrorIs(t, randomError, err) - } - - if testItem.checkAsErr != nil { - require.True(t, testItem.checkAsErr(errFromStatus)) + if testItem.checkAsErr != nil { + require.True(t, testItem.checkAsErr(errFromStatus)) + } } } } diff --git a/pool/mock_test.go b/pool/mock_test.go index ccbe070..39a61da 100644 --- a/pool/mock_test.go +++ b/pool/mock_test.go @@ -8,7 +8,6 @@ import ( sessionv2 "github.com/nspcc-dev/neofs-api-go/v2/session" "github.com/nspcc-dev/neofs-sdk-go/accounting" sdkClient "github.com/nspcc-dev/neofs-sdk-go/client" - apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" "github.com/nspcc-dev/neofs-sdk-go/container" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto" @@ -27,7 +26,7 @@ type mockClient struct { errorOnCreateSession bool errorOnEndpointInfo bool errorOnNetworkInfo bool - stOnGetObject apistatus.Status + errOnGetObject error } func newMockClient(addr string, signer neofscrypto.Signer) *mockClient { @@ -60,8 +59,8 @@ func (m *mockClient) errOnDial() { m.errOnNetworkInfo() } -func (m *mockClient) statusOnGetObject(st apistatus.Status) { - m.stOnGetObject = st +func (m *mockClient) statusOnGetObject(err error) { + m.errOnGetObject = err } func newToken(signer neofscrypto.Signer) *session.Object { @@ -139,13 +138,12 @@ func (m *mockClient) objectDelete(context.Context, PrmObjectDelete) error { func (m *mockClient) objectGet(context.Context, PrmObjectGet) (ResGetObject, error) { var res ResGetObject - if m.stOnGetObject == nil { + if m.errOnGetObject == nil { return res, nil } - err := apistatus.ErrFromStatus(m.stOnGetObject) - m.updateErrorRate(err) - return res, err + m.updateErrorRate(m.errOnGetObject) + return res, m.errOnGetObject } func (m *mockClient) objectHead(context.Context, PrmObjectHead) (object.Object, error) {