Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use errors.New to replace fmt.Errorf with no parameters #20943

Merged
merged 2 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion baseapp/abci_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func validateExtendedCommitAgainstLastCommit(ec abci.ExtendedCommitInfo, lc come
}
return -int(vote1.Validator.Power - vote2.Validator.Power) // vp sorted in descending order
}) {
return fmt.Errorf("extended commit votes are not sorted by voting power")
return errors.New("extended commit votes are not sorted by voting power")
}

addressCache := make(map[string]struct{}, len(ec.Votes))
Expand Down
3 changes: 2 additions & 1 deletion client/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"crypto/tls"
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -68,7 +69,7 @@ func CreateClientConfig(ctx client.Context, customClientTemplate string, customC
}

if (customClientTemplate != "" && customConfig == nil) || (customClientTemplate == "" && customConfig != nil) {
return ctx, fmt.Errorf("customClientTemplate and customConfig should be both nil or not nil")
return ctx, errors.New("customClientTemplate and customConfig should be both nil or not nil")
}

if customClientTemplate != "" {
Expand Down
3 changes: 2 additions & 1 deletion client/prompt_validation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"errors"
"fmt"
"net/url"
"unicode"
Expand All @@ -11,7 +12,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
3 changes: 2 additions & 1 deletion client/pruning/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pruning

import (
"errors"
"fmt"
"path/filepath"

Expand Down Expand Up @@ -76,7 +77,7 @@ Supported app-db-backend types include 'goleveldb', 'rocksdb', 'pebbledb'.`,

rootMultiStore, ok := cms.(*rootmulti.Store)
if !ok {
return fmt.Errorf("currently only support the pruning of rootmulti.Store type")
return errors.New("currently only support the pruning of rootmulti.Store type")
}
latestHeight := rootmulti.GetLatestVersion(db)
// valid heights should be greater than 0.
Expand Down
7 changes: 4 additions & 3 deletions client/rpc/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"strings"
Expand All @@ -17,7 +18,7 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/version"
)

Expand Down Expand Up @@ -187,7 +188,7 @@ $ %[1]s tx [flags] | %[1]s q wait-tx
return clientCtx.PrintProto(newResponseFormatBroadcastTxCommit(res))
}
case <-ctx.Done():
return errors.ErrLogic.Wrapf("timed out waiting for transaction %X to be included in a block", hash)
return sdkerrors.ErrLogic.Wrapf("timed out waiting for transaction %X to be included in a block", hash)
}
return nil
},
Expand Down Expand Up @@ -222,5 +223,5 @@ func parseHashFromInput(in []byte) ([]byte, error) {
return hex.DecodeString(hash)
}
}
return nil, fmt.Errorf("txhash not found")
return nil, errors.New("txhash not found")
}
3 changes: 2 additions & 1 deletion client/v2/autocli/flag/pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package flag

import (
"context"
"errors"
"fmt"

"google.golang.org/protobuf/reflect/protoreflect"
Expand Down Expand Up @@ -47,7 +48,7 @@ func (a *pubkeyValue) Set(s string) error {

any, err := types.NewAnyWithValue(pk)
if err != nil {
return fmt.Errorf("error converting to any type")
return errors.New("error converting to any type")
}

a.value = any
Expand Down
9 changes: 5 additions & 4 deletions client/v2/autocli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package autocli

import (
"context"
"errors"
"fmt"
"io"
"strings"
Expand Down Expand Up @@ -176,14 +177,14 @@ func encoder(encoder aminojson.Encoder) aminojson.Encoder {
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()

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

nanos := msg.Get(nanosField).Int()
Expand All @@ -199,14 +200,14 @@ func encoder(encoder aminojson.Encoder) aminojson.Encoder {
fields := msg.Descriptor().Fields()
denomField := fields.ByName(denomName)
if denomField == nil {
return fmt.Errorf("expected denom field")
return errors.New("expected denom field")
}

denom := msg.Get(denomField).String()

amountField := fields.ByName(amountName)
if amountField == nil {
return fmt.Errorf("expected amount field")
return errors.New("expected amount field")
}

amount := msg.Get(amountField).String()
Expand Down
5 changes: 3 additions & 2 deletions runtime/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package runtime

import (
"context"
"errors"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -31,7 +32,7 @@ type msgRouterService struct {
// CanInvoke returns an error if the given message cannot be invoked.
func (m *msgRouterService) CanInvoke(ctx context.Context, typeURL string) error {
if typeURL == "" {
return fmt.Errorf("missing type url")
return errors.New("missing type url")
}

typeURL = strings.TrimPrefix(typeURL, "/")
Expand Down Expand Up @@ -94,7 +95,7 @@ type queryRouterService struct {
// CanInvoke returns an error if the given request cannot be invoked.
func (m *queryRouterService) CanInvoke(ctx context.Context, typeURL string) error {
if typeURL == "" {
return fmt.Errorf("missing type url")
return errors.New("missing type url")
}

typeURL = strings.TrimPrefix(typeURL, "/")
Expand Down
4 changes: 2 additions & 2 deletions schema/decoding/resolver_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package decoding

import (
"fmt"
"errors"
"testing"

"cosmossdk.io/schema"
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestModuleSetDecoderResolver_LookupDecoder(t *testing.T) {
type modD struct{}

func (m modD) ModuleCodec() (schema.ModuleCodec, error) {
return schema.ModuleCodec{}, fmt.Errorf("an error")
return schema.ModuleCodec{}, errors.New("an error")
}

func TestModuleSetDecoderResolver_IterateAll_Error(t *testing.T) {
Expand Down
7 changes: 5 additions & 2 deletions schema/enum.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package schema

import "fmt"
import (
"errors"
"fmt"
)

// EnumDefinition represents the definition of an enum type.
type EnumDefinition struct {
Expand All @@ -22,7 +25,7 @@ func (e EnumDefinition) Validate() error {
}

if len(e.Values) == 0 {
return fmt.Errorf("enum definition values cannot be empty")
return errors.New("enum definition values cannot be empty")
}
seen := make(map[string]bool, len(e.Values))
for i, v := range e.Values {
Expand Down
2 changes: 1 addition & 1 deletion server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func interceptConfigs(rootViper *viper.Viper, customAppTemplate string, customCo
appCfgFilePath := filepath.Join(configPath, "app.toml")
if _, err := os.Stat(appCfgFilePath); os.IsNotExist(err) {
if (customAppTemplate != "" && customConfig == nil) || (customAppTemplate == "" && customConfig != nil) {
return nil, fmt.Errorf("customAppTemplate and customConfig should be both nil or not nil")
return nil, errors.New("customAppTemplate and customConfig should be both nil or not nil")
}

if customAppTemplate != "" {
Expand Down
5 changes: 3 additions & 2 deletions store/v2/root/factory.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package root

import (
"errors"
"fmt"
"os"

Expand Down Expand Up @@ -76,7 +77,7 @@ func CreateRootStore(opts *FactoryOptions) (store.RootStore, error) {
ssDb, err = pebbledb.New(dir)
case SSTypeRocks:
// TODO: rocksdb requires build tags so is not supported here by default
return nil, fmt.Errorf("rocksdb not supported")
return nil, errors.New("rocksdb not supported")
}
if err != nil {
return nil, err
Expand Down Expand Up @@ -110,7 +111,7 @@ func CreateRootStore(opts *FactoryOptions) (store.RootStore, error) {
case SCTypeIavl:
trees[key] = iavl.NewIavlTree(db.NewPrefixDB(opts.SCRawDB, []byte(key)), opts.Logger, opts.IavlConfig)
case SCTypeIavlV2:
return nil, fmt.Errorf("iavl v2 not supported")
return nil, errors.New("iavl v2 not supported")
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions telemetry/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package telemetry
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
Expand Down Expand Up @@ -192,7 +193,7 @@ func (m *Metrics) Gather(format string) (GatherResponse, error) {
// If Prometheus metrics are not enabled, it returns an error.
func (m *Metrics) gatherPrometheus() (GatherResponse, error) {
if !m.prometheusEnabled {
return GatherResponse{}, fmt.Errorf("prometheus metrics are not enabled")
return GatherResponse{}, errors.New("prometheus metrics are not enabled")
}

metricsFamilies, err := prometheus.DefaultGatherer.Gather()
Expand All @@ -218,7 +219,7 @@ func (m *Metrics) gatherPrometheus() (GatherResponse, error) {
func (m *Metrics) gatherGeneric() (GatherResponse, error) {
gm, ok := m.sink.(DisplayableSink)
if !ok {
return GatherResponse{}, fmt.Errorf("non in-memory metrics sink does not support generic format")
return GatherResponse{}, errors.New("non in-memory metrics sink does not support generic format")
}

summary, err := gm.DisplayMetrics(nil, nil)
Expand Down
6 changes: 3 additions & 3 deletions testutil/key.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package testutil

import (
"fmt"
"errors"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
Expand Down Expand Up @@ -47,12 +47,12 @@ func GenerateSaveCoinKey(

// ensure no overwrite
if !overwrite && exists {
return sdk.AccAddress{}, "", fmt.Errorf("key already exists, overwrite is disabled")
return sdk.AccAddress{}, "", errors.New("key already exists, overwrite is disabled")
}

if exists {
if err := keybase.Delete(keyName); err != nil {
return sdk.AccAddress{}, "", fmt.Errorf("failed to overwrite key")
return sdk.AccAddress{}, "", errors.New("failed to overwrite key")
}
}

Expand Down
4 changes: 2 additions & 2 deletions tools/cosmovisor/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (l Launcher) doBackup() error {
}

if uInfo.Name == "" {
return fmt.Errorf("upgrade-info.json is empty")
return errors.New("upgrade-info.json is empty")
}

// a destination directory, Format YYYY-MM-DD
Expand Down Expand Up @@ -241,7 +241,7 @@ func (l Launcher) doCustomPreUpgrade() error {
if oldMode != newMode {
if err := os.Chmod(preupgradeFile, newMode); err != nil {
l.logger.Info("COSMOVISOR_CUSTOM_PREUPGRADE could not add execute permission")
return fmt.Errorf("COSMOVISOR_CUSTOM_PREUPGRADE could not add execute permission")
return errors.New("COSMOVISOR_CUSTOM_PREUPGRADE could not add execute permission")
}
}

Expand Down
10 changes: 5 additions & 5 deletions types/tx/types.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tx

import (
"fmt"
"errors"

"google.golang.org/protobuf/reflect/protoreflect"

Expand Down Expand Up @@ -40,22 +40,22 @@ func (t *Tx) GetMsgs() []sdk.Msg {
// ValidateBasic implements the ValidateBasic method on sdk.Tx.
func (t *Tx) ValidateBasic() error {
if t == nil {
return fmt.Errorf("bad Tx")
return errors.New("bad Tx")
}

body := t.Body
if body == nil {
return fmt.Errorf("missing TxBody")
return errors.New("missing TxBody")
}

authInfo := t.AuthInfo
if authInfo == nil {
return fmt.Errorf("missing AuthInfo")
return errors.New("missing AuthInfo")
}

fee := authInfo.Fee
if fee == nil {
return fmt.Errorf("missing fee")
return errors.New("missing fee")
}

if fee.GasLimit > MaxGasWanted {
Expand Down
Loading
Loading