From 79f72e10c9b1a988cadba270668a9b2e4e4adbff Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 16 Nov 2020 16:33:41 +0300 Subject: [PATCH] [#199] sdk: Refactor string parse errors Signed-off-by: Leonard Lyubich --- internal/error.go | 9 --------- pkg/container/id.go | 7 ++----- pkg/container/id_test.go | 2 +- pkg/object/address.go | 8 +++----- pkg/object/address_test.go | 6 +++--- pkg/object/id.go | 7 ++----- pkg/object/id_test.go | 2 +- pkg/owner/id.go | 7 ++----- pkg/owner/id_test.go | 2 +- 9 files changed, 15 insertions(+), 35 deletions(-) delete mode 100644 internal/error.go diff --git a/internal/error.go b/internal/error.go deleted file mode 100644 index 537c02ca..00000000 --- a/internal/error.go +++ /dev/null @@ -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) -} diff --git a/pkg/container/id.go b/pkg/container/id.go index 659828fd..bdf3f8ea 100644 --- a/pkg/container/id.go +++ b/pkg/container/id.go @@ -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) diff --git a/pkg/container/id_test.go b/pkg/container/id_test.go index 33469751..346a1873 100644 --- a/pkg/container/id_test.go +++ b/pkg/container/id_test.go @@ -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)) }) } }) diff --git a/pkg/object/address.go b/pkg/object/address.go index 7f2bfe77..a2281a73 100644 --- a/pkg/object/address.go +++ b/pkg/object/address.go @@ -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 { diff --git a/pkg/object/address_test.go b/pkg/object/address_test.go index d0316705..e17fc4d6 100644 --- a/pkg/object/address_test.go +++ b/pkg/object/address_test.go @@ -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)) }) } diff --git a/pkg/object/id.go b/pkg/object/id.go index c62451d0..813793dd 100644 --- a/pkg/object/id.go +++ b/pkg/object/id.go @@ -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) diff --git a/pkg/object/id_test.go b/pkg/object/id_test.go index 14ab6c67..a11e80b1 100644 --- a/pkg/object/id_test.go +++ b/pkg/object/id_test.go @@ -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)) }) } }) diff --git a/pkg/owner/id.go b/pkg/owner/id.go index 49acb2cc..e6a240d6 100644 --- a/pkg/owner/id.go +++ b/pkg/owner/id.go @@ -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) diff --git a/pkg/owner/id_test.go b/pkg/owner/id_test.go index 8d867df4..49816bf8 100644 --- a/pkg/owner/id_test.go +++ b/pkg/owner/id_test.go @@ -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)) }) } })