Skip to content

Commit

Permalink
refactor: use errors.New to replace fmt.Errorf with no parameters (co…
Browse files Browse the repository at this point in the history
  • Loading branch information
0x2d3c authored Feb 26, 2024
1 parent 62ef5ca commit acdb356
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 18 deletions.
7 changes: 4 additions & 3 deletions x/accounts/defaults/base/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package base

import (
"context"
"errors"
"fmt"

dcrd_secp256k1 "github.com/decred/dcrd/dcrec/secp256k1/v4"
Expand Down Expand Up @@ -59,7 +60,7 @@ func (a Account) Init(ctx context.Context, msg *v1.MsgInit) (*v1.MsgInitResponse

func (a Account) SwapPubKey(ctx context.Context, msg *v1.MsgSwapPubKey) (*v1.MsgSwapPubKeyResponse, error) {
if !accountstd.SenderIsSelf(ctx) {
return nil, fmt.Errorf("unauthorized")
return nil, errors.New("unauthorized")
}

return &v1.MsgSwapPubKeyResponse{}, a.verifyAndSetPubKey(ctx, msg.NewPubKey)
Expand All @@ -76,7 +77,7 @@ func (a Account) verifyAndSetPubKey(ctx context.Context, key []byte) error {
// Authenticate implements the authentication flow of an abstracted base account.
func (a Account) Authenticate(ctx context.Context, msg *aa_interface_v1.MsgAuthenticate) (*aa_interface_v1.MsgAuthenticateResponse, error) {
if !accountstd.SenderIsAccountsModule(ctx) {
return nil, fmt.Errorf("unauthorized: only accounts module is allowed to call this")
return nil, errors.New("unauthorized: only accounts module is allowed to call this")
}

pubKey, signerData, err := a.computeSignerData(ctx)
Expand Down Expand Up @@ -107,7 +108,7 @@ func (a Account) Authenticate(ctx context.Context, msg *aa_interface_v1.MsgAuthe
}

if !pubKey.VerifySignature(signBytes, signature) {
return nil, fmt.Errorf("signature verification failed")
return nil, errors.New("signature verification failed")
}

return &aa_interface_v1.MsgAuthenticateResponse{}, nil
Expand Down
6 changes: 3 additions & 3 deletions x/accounts/testing/account_abstraction/minimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package account_abstraction

import (
"context"
"fmt"
"errors"

"cosmossdk.io/api/cosmos/crypto/secp256k1"
"cosmossdk.io/collections"
Expand Down Expand Up @@ -39,7 +39,7 @@ func (a MinimalAbstractedAccount) Init(ctx context.Context, msg *rotationv1.MsgI
}

func (a MinimalAbstractedAccount) RotatePubKey(ctx context.Context, msg *rotationv1.MsgRotatePubKey) (*rotationv1.MsgRotatePubKeyResponse, error) {
return nil, fmt.Errorf("not implemented")
return nil, errors.New("not implemented")
}

// Authenticate authenticates the account, auth always passess.
Expand All @@ -50,7 +50,7 @@ func (a MinimalAbstractedAccount) Authenticate(ctx context.Context, msg *account

// QueryAuthenticateMethods queries the authentication methods of the account.
func (a MinimalAbstractedAccount) QueryAuthenticateMethods(ctx context.Context, req *account_abstractionv1.QueryAuthenticationMethods) (*account_abstractionv1.QueryAuthenticationMethodsResponse, error) {
return nil, fmt.Errorf("not implemented")
return nil, errors.New("not implemented")
}

func (a MinimalAbstractedAccount) RegisterInitHandler(builder *accountstd.InitBuilder) {
Expand Down
6 changes: 3 additions & 3 deletions x/accounts/testing/counter/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package counter
import (
"bytes"
"context"
"fmt"
"errors"

"cosmossdk.io/collections"
"cosmossdk.io/core/address"
Expand Down Expand Up @@ -71,7 +71,7 @@ func (a Account) IncreaseCounter(ctx context.Context, msg *counterv1.MsgIncrease
return nil, err
}
if !bytes.Equal(sender, owner) {
return nil, fmt.Errorf("sender is not the owner of the account")
return nil, errors.New("sender is not the owner of the account")
}
counter, err := a.Counter.Get(ctx)
if err != nil {
Expand Down Expand Up @@ -123,7 +123,7 @@ func (a Account) TestDependencies(ctx context.Context, _ *counterv1.MsgTestDepen
// test funds
funds := accountstd.Funds(ctx)
if len(funds) == 0 {
return nil, fmt.Errorf("expected funds")
return nil, errors.New("expected funds")
}

return &counterv1.MsgTestDependenciesResponse{
Expand Down
3 changes: 2 additions & 1 deletion x/distribution/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 @@ -60,7 +61,7 @@ func NewWithdrawAllRewardsCmd() *cobra.Command {
// The transaction cannot be generated offline since it requires a query
// to get all the validators.
if clientCtx.Offline {
return fmt.Errorf("cannot generate tx in offline mode")
return errors.New("cannot generate tx in offline mode")
}

queryClient := types.NewQueryClient(clientCtx)
Expand Down
3 changes: 2 additions & 1 deletion x/distribution/migrations/v4/migrate_funds.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v4

import (
"context"
"errors"
"fmt"

"cosmossdk.io/x/distribution/types"
Expand All @@ -26,7 +27,7 @@ func MigrateFunds(ctx context.Context, bankKeeper types.BankKeeper, feePool type
// check the migrated balance from pool module account is same as fee pool balance
balances := bankKeeper.GetAllBalances(ctx, poolMacc.GetAddress())
if !balances.Equal(poolBal) {
return types.FeePool{}, fmt.Errorf("pool module account balance is not same as FeePool balance after migration")
return types.FeePool{}, errors.New("pool module account balance is not same as FeePool balance after migration")
}

return types.FeePool{DecimalPool: remainder}, nil
Expand Down
5 changes: 3 additions & 2 deletions x/feegrant/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"errors"
"fmt"
"strings"
"time"
Expand Down Expand Up @@ -136,11 +137,11 @@ Examples:
}

if periodClock <= 0 {
return fmt.Errorf("period clock was not set")
return errors.New("period clock was not set")
}

if periodLimit == nil {
return fmt.Errorf("period limit was not set")
return errors.New("period limit was not set")
}

periodReset := getPeriodReset(periodClock)
Expand Down
9 changes: 4 additions & 5 deletions x/staking/client/cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cli
import (
"encoding/json"
"errors"
"fmt"
"os"

errorsmod "cosmossdk.io/errors"
Expand Down Expand Up @@ -56,23 +55,23 @@ func parseAndValidateValidatorJSON(cdc codec.Codec, path string) (validator, err
}

if v.Amount == "" {
return validator{}, fmt.Errorf("must specify amount of coins to bond")
return validator{}, errors.New("must specify amount of coins to bond")
}
amount, err := sdk.ParseCoinNormalized(v.Amount)
if err != nil {
return validator{}, err
}

if v.PubKey == nil {
return validator{}, fmt.Errorf("must specify the JSON encoded pubkey")
return validator{}, errors.New("must specify the JSON encoded pubkey")
}
var pk cryptotypes.PubKey
if err := cdc.UnmarshalInterfaceJSON(v.PubKey, &pk); err != nil {
return validator{}, err
}

if v.Moniker == "" {
return validator{}, fmt.Errorf("must specify the moniker name")
return validator{}, errors.New("must specify the moniker name")
}

commissionRates, err := buildCommissionRates(v.CommissionRate, v.CommissionMaxRate, v.CommissionMaxChange)
Expand All @@ -81,7 +80,7 @@ func parseAndValidateValidatorJSON(cdc codec.Codec, path string) (validator, err
}

if v.MinSelfDelegation == "" {
return validator{}, fmt.Errorf("must specify minimum self delegation")
return validator{}, errors.New("must specify minimum self delegation")
}
minSelfDelegation, ok := math.NewIntFromString(v.MinSelfDelegation)
if !ok {
Expand Down

0 comments on commit acdb356

Please sign in to comment.