status: Remove IsSuccessful and ErrToStatus functions

Signed-off-by: Evgenii Baidakov <evgenii@nspcc.io>
This commit is contained in:
Evgenii Baidakov 2023-05-22 15:30:28 +04:00
parent 7002b3b0df
commit b9ec85e5e3
No known key found for this signature in database
GPG key ID: 8733EE3D72CDB4DE
4 changed files with 6 additions and 55 deletions

View file

@ -199,10 +199,10 @@ func (x *contextCall) processResponse() bool {
// get result status
st := apistatus.FromStatusV2(x.resp.GetMetaHeader().GetStatus())
successfulStatus := apistatus.IsSuccessful(st)
x.err = apistatus.ErrFromStatus(st)
var errorExists bool
x.err, errorExists = st.(error)
return successfulStatus
return !errorExists
}
// processResponse verifies response signature.

View file

@ -27,18 +27,3 @@ func ErrFromStatus(st Status) error {
return nil
}
// ErrToStatus converts the error instance to Status instance.
//
// Note: direct assignment may not be compatibility-safe.
func ErrToStatus(err error) Status {
return err
}
// IsSuccessful checks if status is successful.
//
// Note: direct cast may not be compatibility-safe.
func IsSuccessful(st Status) bool {
_, ok := st.(error)
return !ok
}

View file

@ -1,35 +0,0 @@
package apistatus_test
import (
"errors"
"testing"
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
"github.com/stretchr/testify/require"
)
func TestErrors(t *testing.T) {
t.Run("error source", func(t *testing.T) {
err := errors.New("some error")
st := apistatus.ErrToStatus(err)
success := apistatus.IsSuccessful(st)
require.False(t, success)
res := apistatus.ErrFromStatus(st)
require.ErrorIs(t, res, err)
})
t.Run("non-error source", func(t *testing.T) {
var st apistatus.Status = "any non-error type"
success := apistatus.IsSuccessful(st)
require.True(t, success)
res := apistatus.ErrFromStatus(st)
require.Nil(t, res)
})
}

View file

@ -126,12 +126,13 @@ func ToStatusV2(st Status) *status.Status {
return v.ToStatusV2()
}
if IsSuccessful(st) {
err, isErrorExists := st.(error)
if !isErrorExists {
return newStatusV2WithLocalCode(status.OK, status.GlobalizeSuccess)
}
internalErrorStatus := newStatusV2WithLocalCode(status.Internal, status.GlobalizeCommonFail)
internalErrorStatus.SetMessage(st.(error).Error()) // type cast never panics because IsSuccessful() checks cast
internalErrorStatus.SetMessage(err.Error())
return internalErrorStatus
}