[#521] *: use stdlib errors package

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2021-05-18 11:12:51 +03:00 committed by Alex Vanin
parent 43e575cec2
commit 71b87155ef
171 changed files with 825 additions and 674 deletions

View file

@ -1,7 +1,7 @@
package netmap
import (
"github.com/pkg/errors"
"fmt"
)
// AddPeerArgs groups the arguments
@ -18,10 +18,8 @@ func (a *AddPeerArgs) SetInfo(v []byte) {
// AddPeer invokes the call of add peer method
// of NeoFS Netmap contract.
func (c *Client) AddPeer(args AddPeerArgs) error {
info := args.info
return errors.Wrapf(c.client.Invoke(
c.addPeerMethod,
info,
), "could not invoke method (%s)", c.addPeerMethod)
if err := c.client.Invoke(c.addPeerMethod, args.info); err != nil {
return fmt.Errorf("could not invoke method (%s): %w", c.addPeerMethod, err)
}
return nil
}

View file

@ -1,9 +1,10 @@
package netmap
import (
"fmt"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
"github.com/pkg/errors"
)
// ConfigArgs groups the arguments
@ -36,19 +37,18 @@ func (c *Client) Config(args ConfigArgs, assert func(stackitem.Item) (interface{
args.key,
)
if err != nil {
return nil, errors.Wrapf(err,
"could not perform test invocation (%s)",
c.configMethod)
return nil, fmt.Errorf("could not perform test invocation (%s): %w",
c.configMethod, err)
}
if ln := len(items); ln != 1 {
return nil, errors.Errorf("unexpected stack item count (%s): %d",
return nil, fmt.Errorf("unexpected stack item count (%s): %d",
c.configMethod, ln)
}
val, err := assert(items[0])
if err != nil {
return nil, errors.Wrap(err, "value type assertion failed")
return nil, fmt.Errorf("value type assertion failed: %w", err)
}
return &ConfigValues{

View file

@ -1,8 +1,9 @@
package netmap
import (
"fmt"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
"github.com/pkg/errors"
)
// EpochArgs groups the arguments
@ -28,19 +29,18 @@ func (c *Client) Epoch(_ EpochArgs) (*EpochValues, error) {
c.epochMethod,
)
if err != nil {
return nil, errors.Wrapf(err,
"could not perform test invocation (%s)",
c.epochMethod)
return nil, fmt.Errorf("could not perform test invocation (%s): %w",
c.epochMethod, err)
}
if ln := len(items); ln != 1 {
return nil, errors.Errorf("unexpected stack item count (%s): %d",
return nil, fmt.Errorf("unexpected stack item count (%s): %d",
c.epochMethod, ln)
}
num, err := client.IntFromStackItem(items[0])
if err != nil {
return nil, errors.Wrapf(err, "could not get number from stack item (%s)", c.epochMethod)
return nil, fmt.Errorf("could not get number from stack item (%s): %w", c.epochMethod, err)
}
return &EpochValues{

View file

@ -1,9 +1,10 @@
package netmap
import (
"fmt"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
"github.com/pkg/errors"
)
// GetNetMapArgs groups the arguments
@ -61,9 +62,8 @@ func (c *Client) NetMap(_ GetNetMapArgs) (*GetNetMapValues, error) {
c.netMapMethod,
)
if err != nil {
return nil, errors.Wrapf(err,
"could not perform test invocation (%s)",
c.netMapMethod)
return nil, fmt.Errorf("could not perform test invocation (%s): %w",
c.netMapMethod, err)
}
return peersFromStackItems(prms, c.netMapMethod)
@ -78,9 +78,8 @@ func (c *Client) Snapshot(a GetSnapshotArgs) (*GetNetMapValues, error) {
int64(a.diff),
)
if err != nil {
return nil, errors.Wrapf(err,
"could not perform test invocation (%s)",
c.netMapMethod)
return nil, fmt.Errorf("could not perform test invocation (%s): %w",
c.netMapMethod, err)
}
return peersFromStackItems(prms, c.snapshotMethod)
@ -94,9 +93,8 @@ func (c *Client) EpochSnapshot(args EpochSnapshotArgs) (*EpochSnapshotValues, er
int64(args.epoch),
)
if err != nil {
return nil, errors.Wrapf(err,
"could not perform test invocation (%s)",
c.epochSnapshotMethod)
return nil, fmt.Errorf("could not perform test invocation (%s): %w",
c.epochSnapshotMethod, err)
}
nmVals, err := peersFromStackItems(prms, c.epochSnapshotMethod)
@ -111,15 +109,14 @@ func (c *Client) EpochSnapshot(args EpochSnapshotArgs) (*EpochSnapshotValues, er
func peersFromStackItems(stack []stackitem.Item, method string) (*GetNetMapValues, error) {
if ln := len(stack); ln != 1 {
return nil, errors.Errorf("unexpected stack item count (%s): %d",
return nil, fmt.Errorf("unexpected stack item count (%s): %d",
method, ln)
}
peers, err := client.ArrayFromStackItem(stack[0])
if err != nil {
return nil, errors.Wrapf(err,
"could not get stack item array from stack item (%s)",
method)
return nil, fmt.Errorf("could not get stack item array from stack item (%s): %w",
method, err)
}
res := &GetNetMapValues{
@ -129,8 +126,7 @@ func peersFromStackItems(stack []stackitem.Item, method string) (*GetNetMapValue
for i := range peers {
peer, err := peerInfoFromStackItem(peers[i])
if err != nil {
return nil, errors.Wrapf(err,
"could not parse stack item (Peer #%d)", i)
return nil, fmt.Errorf("could not parse stack item (Peer #%d): %w", i, err)
}
res.peers = append(res.peers, peer)
@ -142,9 +138,9 @@ func peersFromStackItems(stack []stackitem.Item, method string) (*GetNetMapValue
func peerInfoFromStackItem(prm stackitem.Item) ([]byte, error) {
prms, err := client.ArrayFromStackItem(prm)
if err != nil {
return nil, errors.Wrapf(err, "could not get stack item array (PeerInfo)")
return nil, fmt.Errorf("could not get stack item array (PeerInfo): %w", err)
} else if ln := len(prms); ln != nodeInfoFixedPrmNumber {
return nil, errors.Errorf("unexpected stack item count (PeerInfo): expected %d, has %d", 1, ln)
return nil, fmt.Errorf("unexpected stack item count (PeerInfo): expected %d, has %d", 1, ln)
}
return client.BytesFromStackItem(prms[0])

View file

@ -1,6 +1,8 @@
package netmap
import "github.com/pkg/errors"
import (
"fmt"
)
// NewEpochArgs groups the arguments
// of new epoch invocation call.
@ -16,8 +18,8 @@ func (a *NewEpochArgs) SetEpochNumber(v int64) {
// NewEpoch invokes the call of new epoch method
// of NeoFS Netmap contract.
func (c *Client) NewEpoch(args NewEpochArgs) error {
return errors.Wrapf(c.client.Invoke(
c.addPeerMethod,
args.number,
), "could not invoke method (%s)", c.newEpochMethod)
if err := c.client.Invoke(c.addPeerMethod, args.number); err != nil {
return fmt.Errorf("could not invoke method (%s): %w", c.newEpochMethod, err)
}
return nil
}

View file

@ -1,7 +1,7 @@
package netmap
import (
"github.com/pkg/errors"
"fmt"
)
// UpdateStateArgs groups the arguments
@ -26,9 +26,15 @@ func (u *UpdateStateArgs) SetState(v int64) {
// UpdateState invokes the call of update state method
// of NeoFS Netmap contract.
func (c *Client) UpdateState(args UpdateStateArgs) error {
return errors.Wrapf(c.client.Invoke(
err := c.client.Invoke(
c.updateStateMethod,
args.state,
args.key,
), "could not invoke method (%s)", c.updateStateMethod)
)
if err != nil {
return fmt.Errorf("could not invoke method (%s): %w", c.updateStateMethod, err)
}
return nil
}

View file

@ -1,8 +1,10 @@
package wrapper
import (
"errors"
"fmt"
"github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
"github.com/pkg/errors"
)
// AddPeer registers peer in NeoFS network through
@ -20,8 +22,8 @@ func (w *Wrapper) AddPeer(nodeInfo *netmap.NodeInfo) error {
args := netmap.AddPeerArgs{}
args.SetInfo(rawNodeInfo)
return errors.Wrap(
w.client.AddPeer(args),
"could not invoke smart contract",
)
if err := w.client.AddPeer(args); err != nil {
return fmt.Errorf("could not invoke smart contract: %w", err)
}
return nil
}

View file

@ -1,10 +1,10 @@
package wrapper
import (
"fmt"
"strconv"
"github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
"github.com/pkg/errors"
)
const (
@ -24,7 +24,7 @@ const (
func (w *Wrapper) MaxObjectSize() (uint64, error) {
objectSize, err := w.readUInt64Config(maxObjectSizeConfig)
if err != nil {
return 0, errors.Wrapf(err, "(%T) could not get epoch number", w)
return 0, fmt.Errorf("(%T) could not get epoch number: %w", w, err)
}
return objectSize, nil
@ -35,7 +35,7 @@ func (w *Wrapper) MaxObjectSize() (uint64, error) {
func (w *Wrapper) BasicIncomeRate() (uint64, error) {
rate, err := w.readUInt64Config(basicIncomeRateConfig)
if err != nil {
return 0, errors.Wrapf(err, "(%T) could not get basic income rate", w)
return 0, fmt.Errorf("(%T) could not get basic income rate: %w", w, err)
}
return rate, nil
@ -46,7 +46,7 @@ func (w *Wrapper) BasicIncomeRate() (uint64, error) {
func (w *Wrapper) AuditFee() (uint64, error) {
fee, err := w.readUInt64Config(auditFeeConfig)
if err != nil {
return 0, errors.Wrapf(err, "(%T) could not get audit fee", w)
return 0, fmt.Errorf("(%T) could not get audit fee: %w", w, err)
}
return fee, nil
@ -56,7 +56,7 @@ func (w *Wrapper) AuditFee() (uint64, error) {
func (w *Wrapper) EpochDuration() (uint64, error) {
epochDuration, err := w.readUInt64Config(epochDurationConfig)
if err != nil {
return 0, errors.Wrapf(err, "(%T) could not get epoch duration", w)
return 0, fmt.Errorf("(%T) could not get epoch duration: %w", w, err)
}
return epochDuration, nil
@ -67,7 +67,7 @@ func (w *Wrapper) EpochDuration() (uint64, error) {
func (w *Wrapper) ContainerFee() (uint64, error) {
fee, err := w.readUInt64Config(containerFeeConfig)
if err != nil {
return 0, errors.Wrapf(err, "(%T) could not get container fee", w)
return 0, fmt.Errorf("(%T) could not get container fee: %w", w, err)
}
return fee, nil
@ -78,7 +78,7 @@ func (w *Wrapper) ContainerFee() (uint64, error) {
func (w *Wrapper) EigenTrustIterations() (uint64, error) {
iterations, err := w.readUInt64Config(etIterationsConfig)
if err != nil {
return 0, errors.Wrapf(err, "(%T) could not get eigen trust iterations", w)
return 0, fmt.Errorf("(%T) could not get eigen trust iterations: %w", w, err)
}
return iterations, nil
@ -89,7 +89,7 @@ func (w *Wrapper) EigenTrustIterations() (uint64, error) {
func (w *Wrapper) EigenTrustAlpha() (float64, error) {
strAlpha, err := w.readStringConfig(etAlphaConfig)
if err != nil {
return 0, errors.Wrapf(err, "(%T) could not get eigen trust alpha", w)
return 0, fmt.Errorf("(%T) could not get eigen trust alpha: %w", w, err)
}
return strconv.ParseFloat(strAlpha, 64)
@ -100,7 +100,7 @@ func (w *Wrapper) EigenTrustAlpha() (float64, error) {
func (w *Wrapper) InnerRingCandidateFee() (uint64, error) {
fee, err := w.readUInt64Config(irCandidateFeeConfig)
if err != nil {
return 0, errors.Wrapf(err, "(%T) could not get inner ring candidate fee", w)
return 0, fmt.Errorf("(%T) could not get inner ring candidate fee: %w", w, err)
}
return fee, nil
@ -111,7 +111,7 @@ func (w *Wrapper) InnerRingCandidateFee() (uint64, error) {
func (w *Wrapper) WithdrawFee() (uint64, error) {
fee, err := w.readUInt64Config(withdrawFeeConfig)
if err != nil {
return 0, errors.Wrapf(err, "(%T) could not get withdraw fee", w)
return 0, fmt.Errorf("(%T) could not get withdraw fee: %w", w, err)
}
return fee, nil
@ -130,7 +130,7 @@ func (w *Wrapper) readUInt64Config(key string) (uint64, error) {
numeric, ok := v.(int64)
if !ok {
return 0, errors.Errorf("(%T) invalid value type %T", w, v)
return 0, fmt.Errorf("(%T) invalid value type %T", w, v)
}
return uint64(numeric), nil
@ -149,7 +149,7 @@ func (w *Wrapper) readStringConfig(key string) (string, error) {
str, ok := v.(string)
if !ok {
return "", errors.Errorf("(%T) invalid value type %T", w, v)
return "", fmt.Errorf("(%T) invalid value type %T", w, v)
}
return str, nil

View file

@ -1,8 +1,9 @@
package wrapper
import (
"fmt"
"github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
"github.com/pkg/errors"
)
// Epoch receives number of current NeoFS epoch
@ -12,7 +13,7 @@ func (w *Wrapper) Epoch() (uint64, error) {
vals, err := w.client.Epoch(args)
if err != nil {
return 0, errors.Wrapf(err, "(%T) could not get epoch number", w)
return 0, fmt.Errorf("(%T) could not get epoch number: %w", w, err)
}
return uint64(vals.Number()), nil

View file

@ -1,9 +1,10 @@
package wrapper
import (
"fmt"
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
client "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
"github.com/pkg/errors"
)
// GetNetMap receives information list about storage nodes
@ -43,7 +44,7 @@ func unmarshalNetmap(rawPeers [][]byte) (*netmap.Netmap, error) {
for _, peer := range rawPeers {
nodeInfo := netmap.NewNodeInfo()
if err := nodeInfo.Unmarshal(peer); err != nil {
return nil, errors.Wrap(err, "can't unmarshal peer info")
return nil, fmt.Errorf("can't unmarshal peer info: %w", err)
}
infos = append(infos, *nodeInfo)

View file

@ -1,9 +1,10 @@
package wrapper
import (
"fmt"
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
contract "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
"github.com/pkg/errors"
)
// UpdatePeerState changes peer status through Netmap contract
@ -14,10 +15,8 @@ func (w *Wrapper) UpdatePeerState(key []byte, state netmap.NodeState) error {
args.SetState(int64(state.ToV2()))
// invoke smart contract call
//
// Note: errors.Wrap returns nil on nil error arg.
return errors.Wrap(
w.client.UpdateState(args),
"could not invoke smart contract",
)
if err := w.client.UpdateState(args); err != nil {
return fmt.Errorf("could not invoke smart contract: %w", err)
}
return nil
}