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

chore(x): replace fmt.Errorf without parameters with errors.New #21032

Merged
merged 1 commit into from
Jul 23, 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
3 changes: 2 additions & 1 deletion x/bank/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"errors"
"fmt"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -64,7 +65,7 @@ When using '--dry-run' a key name cannot be used, only a bech32 address.`,
}

if coins.IsZero() {
return fmt.Errorf("must send positive amount")
return errors.New("must send positive amount")
}

split, err := cmd.Flags().GetBool(FlagSplit)
Expand Down
6 changes: 3 additions & 3 deletions x/distribution/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ func (k Keeper) calculateDelegationRewardsBetween(ctx context.Context, val sdk.V
) (sdk.DecCoins, error) {
// sanity check
if startingPeriod > endingPeriod {
return sdk.DecCoins{}, fmt.Errorf("startingPeriod cannot be greater than endingPeriod")
return sdk.DecCoins{}, errors.New("startingPeriod cannot be greater than endingPeriod")
}

// sanity check
if stake.IsNegative() {
return sdk.DecCoins{}, fmt.Errorf("stake should not be negative")
return sdk.DecCoins{}, errors.New("stake should not be negative")
}

valBz, err := k.stakingKeeper.ValidatorAddressCodec().StringToBytes(val.GetOperator())
Expand All @@ -78,7 +78,7 @@ func (k Keeper) calculateDelegationRewardsBetween(ctx context.Context, val sdk.V

difference := ending.CumulativeRewardRatio.Sub(starting.CumulativeRewardRatio)
if difference.IsAnyNegative() {
return sdk.DecCoins{}, fmt.Errorf("negative rewards should not be possible")
return sdk.DecCoins{}, errors.New("negative rewards should not be possible")
}
// note: necessary to truncate so we don't allow withdrawing more rewards than owed
rewards := difference.MulDecTruncate(stake)
Expand Down
3 changes: 2 additions & 1 deletion x/distribution/types/params.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"errors"
"fmt"

"cosmossdk.io/math"
Expand Down Expand Up @@ -28,7 +29,7 @@ func validateCommunityTax(i interface{}) error {
}

if v.IsNil() {
return fmt.Errorf("community tax must be not nil")
return errors.New("community tax must be not nil")
}
if v.IsNegative() {
return fmt.Errorf("community tax must be positive: %s", v)
Expand Down
3 changes: 2 additions & 1 deletion x/evidence/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"errors"
"fmt"

"github.com/cosmos/gogoproto/proto"
Expand Down Expand Up @@ -45,7 +46,7 @@ func (gs GenesisState) Validate() error {
for _, e := range gs.Evidence {
evi, ok := e.GetCachedValue().(exported.Evidence)
if !ok {
return fmt.Errorf("expected evidence")
return errors.New("expected evidence")
}
if err := evi.ValidateBasic(); err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions x/feegrant/basic_fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package feegrant

import (
"context"
"fmt"
"errors"
"time"

"cosmossdk.io/core/appmodule"
Expand All @@ -28,7 +28,7 @@ var _ FeeAllowanceI = (*BasicAllowance)(nil)
func (a *BasicAllowance) Accept(ctx context.Context, fee sdk.Coins, _ []sdk.Msg) (bool, error) {
environment, ok := ctx.Value(corecontext.EnvironmentContextKey).(appmodule.Environment)
if !ok {
return false, fmt.Errorf("environment not set")
return false, errors.New("environment not set")
}
headerInfo := environment.HeaderService.HeaderInfo(ctx)
if a.Expiration != nil && a.Expiration.Before(headerInfo.Time) {
Expand Down
6 changes: 3 additions & 3 deletions x/feegrant/filtered_fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package feegrant

import (
"context"
"fmt"
"errors"
"time"

"github.com/cosmos/gogoproto/proto"
Expand Down Expand Up @@ -100,7 +100,7 @@ func (a *AllowedMsgAllowance) allowedMsgsToMap(ctx context.Context) (map[string]
msgsMap := make(map[string]bool, len(a.AllowedMessages))
environment, ok := ctx.Value(corecontext.EnvironmentContextKey).(appmodule.Environment)
if !ok {
return nil, fmt.Errorf("environment not set")
return nil, errors.New("environment not set")
}
gasMeter := environment.GasService.GasMeter(ctx)
for _, msg := range a.AllowedMessages {
Expand All @@ -120,7 +120,7 @@ func (a *AllowedMsgAllowance) allMsgTypesAllowed(ctx context.Context, msgs []sdk
}
environment, ok := ctx.Value(corecontext.EnvironmentContextKey).(appmodule.Environment)
if !ok {
return false, fmt.Errorf("environment not set")
return false, errors.New("environment not set")
}
gasMeter := environment.GasService.GasMeter(ctx)
for _, msg := range msgs {
Expand Down
6 changes: 3 additions & 3 deletions x/genutil/client/cli/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cli_test
import (
"context"
"encoding/json"
"fmt"
"errors"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -164,7 +164,7 @@ func (e *mockExporter) Export(
modulesToExport []string,
) (types.ExportedApp, error) {
if e.Err == nil && isZeroExportedApp(e.ExportApp) {
panic(fmt.Errorf("(*mockExporter).Export called without setting e.ExportApp or e.Err"))
panic(errors.New("(*mockExporter).Export called without setting e.ExportApp or e.Err"))
}
e.WasCalled = true

Expand Down Expand Up @@ -311,7 +311,7 @@ func TestExportCLI(t *testing.T) {
t.Parallel()

e := new(mockExporter)
e.Err = fmt.Errorf("whoopsie")
e.Err = errors.New("whoopsie")

sys := NewExportSystem(t, e.Export)
_ = sys.MustRun(t, "init", "some_moniker")
Expand Down
5 changes: 3 additions & 2 deletions x/genutil/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package genutil

import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -60,7 +61,7 @@ func InitializeNodeValidatorFilesFromMnemonic(config *cfg.Config, mnemonic, keyT
nodeID string, valPubKey cryptotypes.PubKey, err error,
) {
if len(mnemonic) > 0 && !bip39.IsMnemonicValid(mnemonic) {
return "", nil, fmt.Errorf("invalid mnemonic")
return "", nil, errors.New("invalid mnemonic")
}
nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile())
if err != nil {
Expand Down Expand Up @@ -103,7 +104,7 @@ func InitializeNodeValidatorFilesFromMnemonic(config *cfg.Config, mnemonic, keyT
privKey = tmed25519.GenPrivKeyFromSecret([]byte(mnemonic))
case "bls12_381":
// TODO: need to add support for getting from mnemonic in Comet.
return "", nil, fmt.Errorf("BLS key type does not support mnemonic")
return "", nil, errors.New("BLS key type does not support mnemonic")
default:
privKey = tmed25519.GenPrivKeyFromSecret([]byte(mnemonic))
}
Expand Down
7 changes: 4 additions & 3 deletions x/gov/client/cli/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"encoding/json"
"errors"
"fmt"
"os"
"strings"
Expand All @@ -28,15 +29,15 @@ type legacyProposal struct {
// validate the legacyProposal
func (p legacyProposal) validate() error {
if p.Type == "" {
return fmt.Errorf("proposal type is required")
return errors.New("proposal type is required")
}

if p.Title == "" {
return fmt.Errorf("proposal title is required")
return errors.New("proposal title is required")
}

if p.Description == "" {
return fmt.Errorf("proposal description is required")
return errors.New("proposal description is required")
}
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion x/gov/keeper/common_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"errors"
"fmt"
"testing"
"time"
Expand Down Expand Up @@ -266,7 +267,7 @@ func trackMockBalances(bankKeeper *govtestutil.MockBankKeeper) error {
}
newBalance, negative := balances[senderAddr].SafeSub(coins...)
if negative {
return fmt.Errorf("not enough balance")
return errors.New("not enough balance")
}
balances[senderAddr] = newBalance
return nil
Expand Down
3 changes: 2 additions & 1 deletion x/group/client/cli/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"encoding/json"
"errors"
"fmt"
"os"

Expand All @@ -14,7 +15,7 @@ import (
// parseDecisionPolicy reads and parses the decision policy.
func parseDecisionPolicy(cdc codec.Codec, decisionPolicyFile string) (group.DecisionPolicy, error) {
if decisionPolicyFile == "" {
return nil, fmt.Errorf("decision policy is required")
return nil, errors.New("decision policy is required")
}

contents, err := os.ReadFile(decisionPolicyFile)
Expand Down
18 changes: 9 additions & 9 deletions x/group/internal/math/dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
package math

import (
"fmt"
"errors"

"github.com/cockroachdb/apd/v2"

errorsmod "cosmossdk.io/errors"
"cosmossdk.io/x/group/errors"
grouperrors "cosmossdk.io/x/group/errors"
)

// Dec is a wrapper struct around apd.Decimal that does no mutation of apd.Decimal's when performing
Expand All @@ -23,21 +23,21 @@ type Dec struct {
func NewPositiveDecFromString(s string) (Dec, error) {
d, err := NewDecFromString(s)
if err != nil {
return Dec{}, errors.ErrInvalidDecString.Wrap(err.Error())
return Dec{}, grouperrors.ErrInvalidDecString.Wrap(err.Error())
}
if !d.IsPositive() {
return Dec{}, errors.ErrInvalidDecString.Wrapf("expected a positive decimal, got %s", s)
return Dec{}, grouperrors.ErrInvalidDecString.Wrapf("expected a positive decimal, got %s", s)
}
return d, nil
}

func NewNonNegativeDecFromString(s string) (Dec, error) {
d, err := NewDecFromString(s)
if err != nil {
return Dec{}, errors.ErrInvalidDecString.Wrap(err.Error())
return Dec{}, grouperrors.ErrInvalidDecString.Wrap(err.Error())
}
if d.IsNegative() {
return Dec{}, errors.ErrInvalidDecString.Wrapf("expected a non-negative decimal, got %s", s)
return Dec{}, grouperrors.ErrInvalidDecString.Wrapf("expected a non-negative decimal, got %s", s)
}
return d, nil
}
Expand All @@ -51,11 +51,11 @@ func (x Dec) IsPositive() bool {
func NewDecFromString(s string) (Dec, error) {
d, _, err := apd.NewFromString(s)
if err != nil {
return Dec{}, errors.ErrInvalidDecString.Wrap(err.Error())
return Dec{}, grouperrors.ErrInvalidDecString.Wrap(err.Error())
}

if d.Form != apd.Finite {
return Dec{}, errors.ErrInvalidDecString.Wrapf("expected a finite decimal, got %s", s)
return Dec{}, grouperrors.ErrInvalidDecString.Wrapf("expected a finite decimal, got %s", s)
}

return Dec{*d}, nil
Expand Down Expand Up @@ -136,7 +136,7 @@ func SubNonNegative(x, y Dec) (Dec, error) {
}

if z.IsNegative() {
return z, fmt.Errorf("result negative during non-negative subtraction")
return z, errors.New("result negative during non-negative subtraction")
}

return z, nil
Expand Down
Loading
Loading