client: Remove ErrFromStatus function

Signed-off-by: Evgenii Baidakov <evgenii@nspcc.io>
This commit is contained in:
Evgenii Baidakov 2023-05-22 16:07:57 +04:00
parent e2a88bc258
commit 277b8f7de4
No known key found for this signature in database
GPG key ID: 8733EE3D72CDB4DE
5 changed files with 28 additions and 40 deletions

View file

@ -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.

View file

@ -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

View file

@ -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
}

View file

@ -224,8 +224,9 @@ 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)
@ -235,4 +236,5 @@ func TestFromStatusV2(t *testing.T) {
require.True(t, testItem.checkAsErr(errFromStatus))
}
}
}
}

View file

@ -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) {