[#199] sdk: Refactor string parse errors

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-16 16:33:41 +03:00 committed by Alex Vanin
parent fe336fd5ba
commit 79f72e10c9
9 changed files with 15 additions and 35 deletions

View file

@ -1,9 +0,0 @@
package internal
// Error is constant type error.
type Error string
// Error implementation of error interface.
func (e Error) Error() string {
return string(e)
}

View file

@ -5,7 +5,6 @@ import (
"crypto/sha256"
"github.com/mr-tron/base58"
"github.com/nspcc-dev/neofs-api-go/internal"
"github.com/nspcc-dev/neofs-api-go/v2/refs"
"github.com/pkg/errors"
)
@ -13,9 +12,7 @@ import (
// ID represents v2-compatible container identifier.
type ID refs.ContainerID
// ErrBadID should be returned when bytes slice isn't Container.ID size (sha256.Size).
// Notice: if byte slice changed, please, replace error message.
const ErrBadID = internal.Error("object.ID should be 32 bytes length")
var errInvalidIDString = errors.New("incorrect format of the string container ID")
// NewIDFromV2 wraps v2 ContainerID message to ID.
func NewIDFromV2(idV2 *refs.ContainerID) *ID {
@ -53,7 +50,7 @@ func (id *ID) Parse(s string) error {
if err != nil {
return errors.Wrap(err, "could not parse container.ID from string")
} else if len(data) != sha256.Size {
return ErrBadID
return errInvalidIDString
}
(*refs.ContainerID)(id).SetValue(data)

View file

@ -70,7 +70,7 @@ func TestID_Parse(t *testing.T) {
str := base58.Encode(cs)
cid := NewID()
require.EqualError(t, cid.Parse(str), ErrBadID.Error())
require.Error(t, cid.Parse(str))
})
}
})

View file

@ -1,9 +1,9 @@
package object
import (
"errors"
"strings"
"github.com/nspcc-dev/neofs-api-go/internal"
"github.com/nspcc-dev/neofs-api-go/pkg/container"
"github.com/nspcc-dev/neofs-api-go/v2/refs"
)
@ -11,9 +11,7 @@ import (
// Address represents v2-compatible object address.
type Address refs.Address
// ErrBadAddress returns when string representation doesn't
// contains Container.ID and Object.ID separated by `/`.
const ErrBadAddress = internal.Error("address should contains container.ID and object.ID separated with `/`")
var errInvalidAddressString = errors.New("incorrect format of the string object address")
const (
addressParts = 2
@ -71,7 +69,7 @@ func (a *Address) Parse(s string) error {
)
if len(parts) != addressParts {
return ErrBadAddress
return errInvalidAddressString
} else if err = cid.Parse(parts[0]); err != nil {
return err
} else if err = oid.Parse(parts[1]); err != nil {

View file

@ -47,17 +47,17 @@ func TestAddress_Parse(t *testing.T) {
t.Run("should fail for bad address", func(t *testing.T) {
s := strings.Join([]string{cid.String()}, addressSeparator)
require.EqualError(t, NewAddress().Parse(s), ErrBadAddress.Error())
require.EqualError(t, NewAddress().Parse(s), errInvalidAddressString.Error())
})
t.Run("should fail on container.ID", func(t *testing.T) {
s := strings.Join([]string{"1", "2"}, addressSeparator)
require.EqualError(t, NewAddress().Parse(s), container.ErrBadID.Error())
require.Error(t, NewAddress().Parse(s))
})
t.Run("should fail on object.ID", func(t *testing.T) {
s := strings.Join([]string{cid.String(), "2"}, addressSeparator)
require.EqualError(t, NewAddress().Parse(s), ErrBadID.Error())
require.Error(t, NewAddress().Parse(s))
})
}

View file

@ -5,7 +5,6 @@ import (
"crypto/sha256"
"github.com/mr-tron/base58"
"github.com/nspcc-dev/neofs-api-go/internal"
"github.com/nspcc-dev/neofs-api-go/v2/refs"
"github.com/pkg/errors"
)
@ -13,9 +12,7 @@ import (
// ID represents v2-compatible object identifier.
type ID refs.ObjectID
// ErrBadID should be returned when bytes slice hasn't sha256.Size
// Notice: if byte slice changed, please, replace error message.
const ErrBadID = internal.Error("object.ID should be 32 bytes length")
var errInvalidIDString = errors.New("incorrect format of the string object ID")
// NewIDFromV2 wraps v2 ObjectID message to ID.
func NewIDFromV2(idV2 *refs.ObjectID) *ID {
@ -53,7 +50,7 @@ func (id *ID) Parse(s string) error {
if err != nil {
return errors.Wrap(err, "could not parse object.ID from string")
} else if len(data) != sha256.Size {
return ErrBadID
return errInvalidIDString
}
(*refs.ObjectID)(id).SetValue(data)

View file

@ -63,7 +63,7 @@ func TestID_Parse(t *testing.T) {
str := base58.Encode(cs)
oid := NewID()
require.EqualError(t, oid.Parse(str), ErrBadID.Error())
require.Error(t, oid.Parse(str))
})
}
})

View file

@ -3,7 +3,6 @@ package owner
import (
"github.com/mr-tron/base58"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neofs-api-go/internal"
"github.com/nspcc-dev/neofs-api-go/v2/refs"
"github.com/pkg/errors"
)
@ -11,9 +10,7 @@ import (
// ID represents v2-compatible owner identifier.
type ID refs.OwnerID
// ErrBadID should be returned when bytes slice isn't Owner.ID size (NEO3WalletSize).
// Notice: if byte slice changed, please, replace error message.
const ErrBadID = internal.Error("owner.ID should be 25 bytes length")
var errInvalidIDString = errors.New("incorrect format of the string owner ID")
// NewIDFromV2 wraps v2 OwnerID message to ID.
func NewIDFromV2(idV2 *refs.OwnerID) *ID {
@ -64,7 +61,7 @@ func (id *ID) Parse(s string) error {
if err != nil {
return errors.Wrap(err, "could not parse owner.ID from string")
} else if len(data) != NEO3WalletSize {
return ErrBadID
return errInvalidIDString
}
(*refs.OwnerID)(id).SetValue(data)

View file

@ -64,7 +64,7 @@ func TestID_Parse(t *testing.T) {
str := base58.Encode(cs)
cid := NewID()
require.EqualError(t, cid.Parse(str), ErrBadID.Error())
require.Error(t, cid.Parse(str))
})
}
})