Skip to content

Commit

Permalink
refactor: use errors.New to replace fmt.Errorf with no parameters (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
0x2d3c authored Feb 24, 2024
1 parent 95cc64b commit aa9ff3d
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 34 deletions.
2 changes: 1 addition & 1 deletion codec/proto_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (pc *ProtoCodec) MustUnmarshalLengthPrefixed(bz []byte, ptr gogoproto.Messa
// implements proto.Message. For interface please use the codec.MarshalInterfaceJSON
func (pc *ProtoCodec) MarshalJSON(o gogoproto.Message) ([]byte, error) { //nolint:stdmethods // we don't want to implement Marshaler interface
if o == nil {
return nil, fmt.Errorf("cannot protobuf JSON encode nil")
return nil, errors.New("cannot protobuf JSON encode nil")
}
return ProtoMarshalJSON(o, pc.interfaceRegistry)
}
Expand Down
4 changes: 2 additions & 2 deletions codec/types/any_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package types_test

import (
"fmt"
"errors"
"runtime"
"testing"

Expand All @@ -17,7 +17,7 @@ type errOnMarshal struct {

var _ proto.Message = (*errOnMarshal)(nil)

var errAlways = fmt.Errorf("always erroring")
var errAlways = errors.New("always erroring")

func (eom *errOnMarshal) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return nil, errAlways
Expand Down
9 changes: 5 additions & 4 deletions codec/types/interface_registry.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"errors"
"fmt"
"reflect"

Expand Down Expand Up @@ -138,7 +139,7 @@ type InterfaceRegistryOptions struct {
// NewInterfaceRegistryWithOptions returns a new InterfaceRegistry with the given options.
func NewInterfaceRegistryWithOptions(options InterfaceRegistryOptions) (InterfaceRegistry, error) {
if options.ProtoFiles == nil {
return nil, fmt.Errorf("proto files must be provided")
return nil, errors.New("proto files must be provided")
}

options.SigningOptions.FileResolver = options.ProtoFiles
Expand Down Expand Up @@ -284,7 +285,7 @@ func (registry *interfaceRegistry) UnpackAny(any *Any, iface interface{}) error

rv := reflect.ValueOf(iface)
if rv.Kind() != reflect.Ptr {
return fmt.Errorf("UnpackAny expects a pointer")
return errors.New("UnpackAny expects a pointer")
}

rt := rv.Elem().Type()
Expand Down Expand Up @@ -366,9 +367,9 @@ func UnpackInterfaces(x interface{}, unpacker AnyUnpacker) error {
type failingAddressCodec struct{}

func (f failingAddressCodec) StringToBytes(string) ([]byte, error) {
return nil, fmt.Errorf("InterfaceRegistry requires a proper address codec implementation to do address conversion")
return nil, errors.New("InterfaceRegistry requires a proper address codec implementation to do address conversion")
}

func (f failingAddressCodec) BytesToString([]byte) (string, error) {
return "", fmt.Errorf("InterfaceRegistry requires a proper address codec implementation to do address conversion")
return "", errors.New("InterfaceRegistry requires a proper address codec implementation to do address conversion")
}
2 changes: 1 addition & 1 deletion math/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ var stringsBuilderPool = &sync.Pool{
// (instead of manipulating the int or math.Int object).
func FormatInt(v string) (string, error) {
if len(v) == 0 {
return "", fmt.Errorf("cannot format empty string")
return "", errors.New("cannot format empty string")
}

sign := ""
Expand Down
22 changes: 11 additions & 11 deletions x/tx/decode/decode.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package decode

import (
"fmt"
"errors"

"github.com/cosmos/cosmos-proto/anyutil"
"google.golang.org/protobuf/proto"

v1beta1 "cosmossdk.io/api/cosmos/tx/v1beta1"
"cosmossdk.io/errors"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/x/tx/signing"
)

Expand All @@ -33,7 +33,7 @@ type Options struct {
// NewDecoder creates a new Decoder for decoding transactions.
func NewDecoder(options Options) (*Decoder, error) {
if options.SigningContext == nil {
return nil, fmt.Errorf("signing context is required")
return nil, errors.New("signing context is required")
}

return &Decoder{
Expand All @@ -46,7 +46,7 @@ func (d *Decoder) Decode(txBytes []byte) (*DecodedTx, error) {
// Make sure txBytes follow ADR-027.
err := rejectNonADR027TxRaw(txBytes)
if err != nil {
return nil, errors.Wrap(ErrTxDecode, err.Error())
return nil, errorsmod.Wrap(ErrTxDecode, err.Error())
}

var raw v1beta1.TxRaw
Expand All @@ -55,7 +55,7 @@ func (d *Decoder) Decode(txBytes []byte) (*DecodedTx, error) {
fileResolver := d.signingCtx.FileResolver()
err = RejectUnknownFieldsStrict(txBytes, raw.ProtoReflect().Descriptor(), fileResolver)
if err != nil {
return nil, errors.Wrap(ErrTxDecode, err.Error())
return nil, errorsmod.Wrap(ErrTxDecode, err.Error())
}

err = proto.Unmarshal(txBytes, &raw)
Expand All @@ -68,25 +68,25 @@ func (d *Decoder) Decode(txBytes []byte) (*DecodedTx, error) {
// allow non-critical unknown fields in TxBody
txBodyHasUnknownNonCriticals, err := RejectUnknownFields(raw.BodyBytes, body.ProtoReflect().Descriptor(), true, fileResolver)
if err != nil {
return nil, errors.Wrap(ErrTxDecode, err.Error())
return nil, errorsmod.Wrap(ErrTxDecode, err.Error())
}

err = proto.Unmarshal(raw.BodyBytes, &body)
if err != nil {
return nil, errors.Wrap(ErrTxDecode, err.Error())
return nil, errorsmod.Wrap(ErrTxDecode, err.Error())
}

var authInfo v1beta1.AuthInfo

// reject all unknown proto fields in AuthInfo
err = RejectUnknownFieldsStrict(raw.AuthInfoBytes, authInfo.ProtoReflect().Descriptor(), fileResolver)
if err != nil {
return nil, errors.Wrap(ErrTxDecode, err.Error())
return nil, errorsmod.Wrap(ErrTxDecode, err.Error())
}

err = proto.Unmarshal(raw.AuthInfoBytes, &authInfo)
if err != nil {
return nil, errors.Wrap(ErrTxDecode, err.Error())
return nil, errorsmod.Wrap(ErrTxDecode, err.Error())
}

theTx := &v1beta1.Tx{
Expand All @@ -101,12 +101,12 @@ func (d *Decoder) Decode(txBytes []byte) (*DecodedTx, error) {
for _, anyMsg := range body.Messages {
msg, signerErr := anyutil.Unpack(anyMsg, fileResolver, d.signingCtx.TypeResolver())
if signerErr != nil {
return nil, errors.Wrap(ErrTxDecode, signerErr.Error())
return nil, errorsmod.Wrap(ErrTxDecode, signerErr.Error())
}
msgs = append(msgs, msg)
ss, signerErr := d.signingCtx.GetSigners(msg)
if signerErr != nil {
return nil, errors.Wrap(ErrTxDecode, signerErr.Error())
return nil, errorsmod.Wrap(ErrTxDecode, signerErr.Error())
}
for _, s := range ss {
_, seen := seenSigners[string(s)]
Expand Down
3 changes: 2 additions & 1 deletion x/tx/signing/aminojson/aminojson.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aminojson

import (
"context"
"errors"
"fmt"

"google.golang.org/protobuf/reflect/protoregistry"
Expand Down Expand Up @@ -78,7 +79,7 @@ func (h SignModeHandler) GetSignBytes(_ context.Context, signerData signing.Sign

f := txData.AuthInfo.Fee
if f == nil {
return nil, fmt.Errorf("fee cannot be nil when tipper is not signer")
return nil, errors.New("fee cannot be nil when tipper is not signer")
}
fee = &aminojsonpb.AminoSignFee{
Amount: f.Amount,
Expand Down
3 changes: 2 additions & 1 deletion x/tx/signing/aminojson/json_marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aminojson_test

import (
"encoding/json"
"errors"
"fmt"
"io"
"reflect"
Expand Down Expand Up @@ -197,7 +198,7 @@ func TestMarshalDuration(t *testing.T) {
fields := msg.Descriptor().Fields()
secondsField := fields.ByName(secondsName)
if secondsField == nil {
return fmt.Errorf("expected seconds field")
return errors.New("expected seconds field")
}
seconds := msg.Get(secondsField).Int()

Expand Down
9 changes: 5 additions & 4 deletions x/tx/signing/aminojson/time.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aminojson

import (
"errors"
"fmt"
"io"
"math"
Expand All @@ -19,12 +20,12 @@ func marshalTimestamp(_ *Encoder, message protoreflect.Message, writer io.Writer
fields := message.Descriptor().Fields()
secondsField := fields.ByName(secondsName)
if secondsField == nil {
return fmt.Errorf("expected seconds field")
return errors.New("expected seconds field")
}

nanosField := fields.ByName(nanosName)
if nanosField == nil {
return fmt.Errorf("expected nanos field")
return errors.New("expected nanos field")
}

seconds := message.Get(secondsField).Int()
Expand Down Expand Up @@ -53,7 +54,7 @@ func marshalDuration(_ *Encoder, message protoreflect.Message, writer io.Writer)
fields := message.Descriptor().Fields()
secondsField := fields.ByName(secondsName)
if secondsField == nil {
return fmt.Errorf("expected seconds field")
return errors.New("expected seconds field")
}

// todo
Expand All @@ -65,7 +66,7 @@ func marshalDuration(_ *Encoder, message protoreflect.Message, writer io.Writer)

nanosField := fields.ByName(nanosName)
if nanosField == nil {
return fmt.Errorf("expected nanos field")
return errors.New("expected nanos field")
}

nanos := message.Get(nanosField).Int()
Expand Down
2 changes: 1 addition & 1 deletion x/tx/signing/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (c *Context) makeGetSignersFunc(descriptor protoreflect.MessageDescriptor)
var fieldGetter func(protoreflect.Message, int) ([][]byte, error)
fieldGetter = func(msg protoreflect.Message, depth int) ([][]byte, error) {
if depth > c.maxRecursionDepth {
return nil, fmt.Errorf("maximum recursion depth exceeded")
return nil, errors.New("maximum recursion depth exceeded")
}
desc := msg.Descriptor()
signerFields, err := getSignersFieldNames(desc)
Expand Down
5 changes: 3 additions & 2 deletions x/tx/signing/directaux/direct_aux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package directaux

import (
"context"
"errors"
"fmt"

"github.com/cosmos/cosmos-proto/anyutil"
Expand Down Expand Up @@ -34,7 +35,7 @@ func NewSignModeHandler(options SignModeHandlerOptions) (SignModeHandler, error)
h := SignModeHandler{}

if options.SignersContext == nil {
return h, fmt.Errorf("signers context is required")
return h, errors.New("signers context is required")
}
h.signersContext = options.SignersContext

Expand All @@ -60,7 +61,7 @@ func (h SignModeHandler) Mode() signingv1beta1.SignMode {
// https://github.com/cosmos/cosmos-sdk/blob/4a6a1e3cb8de459891cb0495052589673d14ef51/x/auth/tx/builder.go#L142
func (h SignModeHandler) getFirstSigner(txData signing.TxData) ([]byte, error) {
if len(txData.Body.Messages) == 0 {
return nil, fmt.Errorf("no signer found")
return nil, errors.New("no signer found")
}

msg, err := anyutil.Unpack(txData.Body.Messages[0], h.fileResolver, h.typeResolver)
Expand Down
3 changes: 2 additions & 1 deletion x/tx/signing/textual/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package textual

import (
"context"
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -74,7 +75,7 @@ func (ar anyValueRenderer) Format(ctx context.Context, v protoreflect.Value) ([]
// Parse implements the ValueRenderer interface.
func (ar anyValueRenderer) Parse(ctx context.Context, screens []Screen) (protoreflect.Value, error) {
if len(screens) == 0 {
return nilValue, fmt.Errorf("expect at least one screen")
return nilValue, errors.New("expect at least one screen")
}
if screens[0].Indent != 0 {
return nilValue, fmt.Errorf("bad indentation: want 0, got %d", screens[0].Indent)
Expand Down
3 changes: 2 additions & 1 deletion x/tx/signing/textual/coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package textual_test
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"testing"
Expand Down Expand Up @@ -55,7 +56,7 @@ func TestMetadataQuerier(t *testing.T) {
require.Error(t, err)

// Errors if metadata querier returns an error
expErr := fmt.Errorf("mock error")
expErr := errors.New("mock error")
txt, err := textual.NewSignModeHandler(textual.SignModeOptions{
CoinMetadataQuerier: func(_ context.Context, _ string) (*bankv1beta1.Metadata, error) {
return nil, expErr
Expand Down
5 changes: 3 additions & 2 deletions x/tx/signing/textual/coins.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package textual

import (
"context"
"errors"
"fmt"
"sort"
"strings"
Expand Down Expand Up @@ -32,7 +33,7 @@ var _ RepeatedValueRenderer = coinsValueRenderer{}

func (vr coinsValueRenderer) Format(ctx context.Context, v protoreflect.Value) ([]Screen, error) {
if vr.coinMetadataQuerier == nil {
return nil, fmt.Errorf("expected non-nil coin metadata querier")
return nil, errors.New("expected non-nil coin metadata querier")
}

// Since this value renderer has a FormatRepeated method, the Format one
Expand All @@ -58,7 +59,7 @@ func (vr coinsValueRenderer) Format(ctx context.Context, v protoreflect.Value) (

func (vr coinsValueRenderer) FormatRepeated(ctx context.Context, v protoreflect.Value) ([]Screen, error) {
if vr.coinMetadataQuerier == nil {
return nil, fmt.Errorf("expected non-nil coin metadata querier")
return nil, errors.New("expected non-nil coin metadata querier")
}

protoCoins := v.List()
Expand Down
5 changes: 3 additions & 2 deletions x/tx/signing/textual/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package textual
import (
"bytes"
"context"
"errors"
"fmt"
"reflect"

Expand Down Expand Up @@ -67,7 +68,7 @@ type SignModeHandler struct {
// NewSignModeHandler returns a new SignModeHandler which generates sign bytes and provides value renderers.
func NewSignModeHandler(o SignModeOptions) (*SignModeHandler, error) {
if o.CoinMetadataQuerier == nil {
return nil, fmt.Errorf("coinMetadataQuerier must be non-empty")
return nil, errors.New("coinMetadataQuerier must be non-empty")
}
if o.FileResolver == nil {
o.FileResolver = protoregistry.GlobalFiles
Expand Down Expand Up @@ -134,7 +135,7 @@ func (r *SignModeHandler) GetFieldValueRenderer(fd protoreflect.FieldDescriptor)
}

if fd.IsMap() {
return nil, fmt.Errorf("value renderers cannot format value of type map")
return nil, errors.New("value renderers cannot format value of type map")
}
return NewMessageValueRenderer(r, md), nil
case fd.Kind() == protoreflect.BoolKind:
Expand Down

0 comments on commit aa9ff3d

Please sign in to comment.