Skip to content

Commit

Permalink
chore(all): replace all fmt.Errorf without paramters with `errors.N…
Browse files Browse the repository at this point in the history
…ew` (#21068)
  • Loading branch information
yukionfire authored Jul 25, 2024
1 parent 6a2d039 commit d6ad92d
Show file tree
Hide file tree
Showing 57 changed files with 127 additions and 103 deletions.
3 changes: 2 additions & 1 deletion baseapp/grpcrouter_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package baseapp

import (
gocontext "context"
"errors"
"fmt"

abci "github.com/cometbft/cometbft/api/cometbft/abci/v1"
Expand Down Expand Up @@ -60,5 +61,5 @@ func (q *QueryServiceTestHelper) Invoke(_ gocontext.Context, method string, args

// NewStream implements the grpc ClientConn.NewStream method
func (q *QueryServiceTestHelper) NewStream(gocontext.Context, *grpc.StreamDesc, string, ...grpc.CallOption) (grpc.ClientStream, error) {
return nil, fmt.Errorf("not supported")
return nil, errors.New("not supported")
}
2 changes: 1 addition & 1 deletion client/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func readTxCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Context, err

if keyType == keyring.TypeLedger && clientCtx.SignModeStr == flags.SignModeTextual {
if !slices.Contains(clientCtx.TxConfig.SignModeHandler().SupportedModes(), signingv1beta1.SignMode_SIGN_MODE_TEXTUAL) {
return clientCtx, fmt.Errorf("SIGN_MODE_TEXTUAL is not available")
return clientCtx, errors.New("SIGN_MODE_TEXTUAL is not available")
}
}

Expand Down
4 changes: 2 additions & 2 deletions client/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package client

import (
gocontext "context"
"fmt"
"errors"
"reflect"
"strconv"

Expand Down Expand Up @@ -123,7 +123,7 @@ func (ctx Context) Invoke(grpcCtx gocontext.Context, method string, req, reply i

// NewStream implements the grpc ClientConn.NewStream method
func (Context) NewStream(gocontext.Context, *grpc.StreamDesc, string, ...grpc.CallOption) (grpc.ClientStream, error) {
return nil, fmt.Errorf("streaming rpc not supported")
return nil, errors.New("streaming rpc not supported")
}

// gRPCCodec checks if Context's Codec is codec.GRPCCodecProvider
Expand Down
4 changes: 2 additions & 2 deletions client/snapshot/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ func LoadArchiveCmd() *cobra.Command {

savedSnapshot := <-quitChan
if savedSnapshot == nil {
return fmt.Errorf("failed to save snapshot")
return errors.New("failed to save snapshot")
}

if !reflect.DeepEqual(&snapshot, savedSnapshot) {
_ = snapshotStore.Delete(snapshot.Height, snapshot.Format)
return fmt.Errorf("invalid archive, the saved snapshot is not equal to the original one")
return errors.New("invalid archive, the saved snapshot is not equal to the original one")
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion client/tx/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ func (f Factory) BuildSimTx(msgs ...sdk.Msg) ([]byte, error) {

encoder := f.txConfig.TxEncoder()
if encoder == nil {
return nil, fmt.Errorf("cannot simulate tx: tx encoder is nil")
return nil, errors.New("cannot simulate tx: tx encoder is nil")
}

return encoder(txb.GetTx())
Expand Down
3 changes: 2 additions & 1 deletion client/tx/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tx

import (
"context"
"errors"
"fmt"
"strings"
"testing"
Expand Down Expand Up @@ -44,7 +45,7 @@ type mockContext struct {

func (m mockContext) Invoke(_ context.Context, _ string, _, reply interface{}, _ ...grpc.CallOption) (err error) {
if m.wantErr {
return fmt.Errorf("mock err")
return errors.New("mock err")
}

*(reply.(*txtypes.SimulateResponse)) = txtypes.SimulateResponse{
Expand Down
4 changes: 2 additions & 2 deletions client/v2/autocli/flag/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package flag

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

"google.golang.org/protobuf/reflect/protoreflect"
Expand Down Expand Up @@ -38,7 +38,7 @@ func (c *coinValue) String() string {

func (c *coinValue) Set(stringValue string) error {
if strings.Contains(stringValue, ",") {
return fmt.Errorf("coin flag must be a single coin, specific multiple coins with multiple flags or spaces")
return errors.New("coin flag must be a single coin, specific multiple coins with multiple flags or spaces")
}

coin, err := coins.ParseCoin(stringValue)
Expand Down
6 changes: 3 additions & 3 deletions client/v2/internal/coins/format.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package coins

import (
"fmt"
"errors"
"regexp"
"strings"

Expand All @@ -19,13 +19,13 @@ func ParseCoin(input string) (*basev1beta1.Coin, error) {
input = strings.TrimSpace(input)

if input == "" {
return nil, fmt.Errorf("empty input when parsing coin")
return nil, errors.New("empty input when parsing coin")
}

matches := coinRegex.FindStringSubmatch(input)

if len(matches) == 0 {
return nil, fmt.Errorf("invalid input format")
return nil, errors.New("invalid input format")
}

return &basev1beta1.Coin{
Expand Down
3 changes: 2 additions & 1 deletion client/v2/internal/prompt/validation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package prompt

import (
"errors"
"fmt"
"net/url"

Expand All @@ -10,7 +11,7 @@ import (
// ValidatePromptNotEmpty validates that the input is not empty.
func ValidatePromptNotEmpty(input string) error {
if input == "" {
return fmt.Errorf("input cannot be empty")
return errors.New("input cannot be empty")
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion client/v2/offchain/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func verifySignature(
return err
}
if !pubKey.VerifySignature(signBytes, data.Signature) {
return fmt.Errorf("unable to verify single signer signature")
return errors.New("unable to verify single signer signature")
}
return nil
default:
Expand Down
3 changes: 2 additions & 1 deletion collections/iter_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package collections

import (
"errors"
"fmt"
"testing"

Expand Down Expand Up @@ -188,7 +189,7 @@ func TestWalk(t *testing.T) {
})
require.NoError(t, err)

sentinelErr := fmt.Errorf("sentinel error")
sentinelErr := errors.New("sentinel error")
err = m.Walk(ctx, nil, func(key, value uint64) (stop bool, err error) {
require.LessOrEqual(t, key, uint64(3)) // asserts that after the number three we stop
if key == 3 {
Expand Down
4 changes: 2 additions & 2 deletions collections/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package collections

import (
"context"
"fmt"
"errors"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestMap_Clear(t *testing.T) {
err := m.Clear(ctx, nil)
require.NoError(t, err)
err = m.Walk(ctx, nil, func(key, value uint64) (bool, error) {
return false, fmt.Errorf("should never be called")
return false, errors.New("should never be called")
})
require.NoError(t, err)
})
Expand Down
3 changes: 2 additions & 1 deletion depinject/appconfig/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package appconfig

import (
"errors"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -95,7 +96,7 @@ func Compose(appConfig gogoproto.Message) depinject.Config {

for _, module := range appConfigConcrete.Modules {
if module.Name == "" {
return depinject.Error(fmt.Errorf("module is missing name"))
return depinject.Error(errors.New("module is missing name"))
}

if module.Config == nil {
Expand Down
5 changes: 3 additions & 2 deletions depinject/container_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package depinject_test

import (
"errors"
"fmt"
"os"
"testing"
Expand Down Expand Up @@ -300,7 +301,7 @@ func TestCyclic(t *testing.T) {
}

func TestErrorOption(t *testing.T) {
err := depinject.Inject(depinject.Error(fmt.Errorf("an error")))
err := depinject.Inject(depinject.Error(errors.New("an error")))
require.Error(t, err)
}

Expand Down Expand Up @@ -606,7 +607,7 @@ func ProvideTestOutput() (TestOutput, error) {
}

func ProvideTestOutputErr() (TestOutput, error) {
return TestOutput{}, fmt.Errorf("error")
return TestOutput{}, errors.New("error")
}

func TestStructArgs(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions depinject/internal/codegen/file.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package codegen

import (
"fmt"
"errors"
"go/ast"
"go/token"
"strconv"
Expand Down Expand Up @@ -61,7 +61,7 @@ func NewFileGen(file *ast.File, codegenPkgPath string) (*FileGen, error) {
if spec.Name != nil {
name := spec.Name.Name
if name == "." {
return nil, fmt.Errorf(". package imports are not allowed")
return nil, errors.New(". package imports are not allowed")
}

info = &importInfo{importPrefix: name, ImportSpec: spec}
Expand Down
3 changes: 2 additions & 1 deletion indexer/postgres/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package postgres
import (
"context"
"database/sql"
"errors"
"fmt"

"cosmossdk.io/schema/appdata"
Expand All @@ -23,7 +24,7 @@ type SqlLogger = func(msg, sql string, params ...interface{})

func StartIndexer(ctx context.Context, logger SqlLogger, config Config) (appdata.Listener, error) {
if config.DatabaseURL == "" {
return appdata.Listener{}, fmt.Errorf("missing database URL")
return appdata.Listener{}, errors.New("missing database URL")
}

driver := config.DatabaseDriver
Expand Down
3 changes: 2 additions & 1 deletion internal/testutil/cmd_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testutil_test

import (
"errors"
"fmt"
"testing"

Expand All @@ -21,7 +22,7 @@ func TestSetArgsWithOriginalMethod(t *testing.T) {
c, _ := cmd.Flags().GetBool("c")
switch {
case a && b, a && c, b && c:
return fmt.Errorf("a,b,c only one could be true")
return errors.New("a,b,c only one could be true")
}
return nil
},
Expand Down
4 changes: 2 additions & 2 deletions orm/encoding/ormfield/uint32.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ormfield

import (
"encoding/binary"
"fmt"
"errors"
"io"

"google.golang.org/protobuf/reflect/protoreflect"
Expand Down Expand Up @@ -183,6 +183,6 @@ func DecodeCompactUint32(reader io.Reader) (uint32, error) {
x |= uint32(buf[4])
return x, nil
default:
return 0, fmt.Errorf("unexpected case")
return 0, errors.New("unexpected case")
}
}
4 changes: 2 additions & 2 deletions orm/encoding/ormfield/uint64.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ormfield

import (
"encoding/binary"
"fmt"
"errors"
"io"

"google.golang.org/protobuf/reflect/protoreflect"
Expand Down Expand Up @@ -213,6 +213,6 @@ func DecodeCompactUint64(reader io.Reader) (uint64, error) {
x |= uint64(buf[8])
return x, nil
default:
return 0, fmt.Errorf("unexpected case")
return 0, errors.New("unexpected case")
}
}
4 changes: 2 additions & 2 deletions orm/internal/listinternal/options.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package listinternal

import (
"fmt"
"errors"

"google.golang.org/protobuf/proto"
)
Expand All @@ -17,7 +17,7 @@ type Options struct {
func (o Options) Validate() error {
if len(o.Cursor) != 0 {
if o.Offset > 0 {
return fmt.Errorf("can only specify one of cursor or offset")
return errors.New("can only specify one of cursor or offset")
}
}
return nil
Expand Down
5 changes: 3 additions & 2 deletions orm/model/ormdb/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/binary"
"errors"
"fmt"
"math"

Expand Down Expand Up @@ -111,7 +112,7 @@ func NewModuleDB(schema *ormv1alpha1.ModuleSchemaDescriptor, options ModuleDBOpt
case ormv1alpha1.StorageType_STORAGE_TYPE_MEMORY:
service := options.MemoryStoreService
if service == nil {
return nil, fmt.Errorf("missing MemoryStoreService")
return nil, errors.New("missing MemoryStoreService")
}

backendResolver = func(ctx context.Context) (ormtable.ReadBackend, error) {
Expand All @@ -124,7 +125,7 @@ func NewModuleDB(schema *ormv1alpha1.ModuleSchemaDescriptor, options ModuleDBOpt
case ormv1alpha1.StorageType_STORAGE_TYPE_TRANSIENT:
service := options.TransientStoreService
if service == nil {
return nil, fmt.Errorf("missing TransientStoreService")
return nil, errors.New("missing TransientStoreService")
}

backendResolver = func(ctx context.Context) (ormtable.ReadBackend, error) {
Expand Down
6 changes: 3 additions & 3 deletions orm/model/ormdb/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"errors"
"strings"
"testing"

Expand Down Expand Up @@ -103,7 +103,7 @@ func (k keeper) Burn(ctx context.Context, acct, denom string, amount uint64) err
}

if amount > supply.Amount {
return fmt.Errorf("insufficient supply")
return errors.New("insufficient supply")
}

supply.Amount -= amount
Expand Down Expand Up @@ -171,7 +171,7 @@ func (k keeper) safeSubBalance(ctx context.Context, acct, denom string, amount u
}

if amount > balance.Amount {
return fmt.Errorf("insufficient funds")
return errors.New("insufficient funds")
}

balance.Amount -= amount
Expand Down
Loading

0 comments on commit d6ad92d

Please sign in to comment.