chore: make fmt.Errorf use %w to wrap error instead of %v (#20350)

This commit is contained in:
Hoa Nguyen 2024-05-12 15:11:25 +07:00 committed by GitHub
parent 6853ba0468
commit 22aa95ccbd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
20 changed files with 42 additions and 37 deletions

View File

@ -24,6 +24,7 @@ linters:
enable:
- dogsled
- errcheck
- errorlint
- exportloopref
- gci
- goconst

View File

@ -262,15 +262,15 @@ func EncodeArmor(blockType string, headers map[string]string, data []byte) strin
buf := new(bytes.Buffer)
w, err := armor.Encode(buf, blockType, headers)
if err != nil {
panic(fmt.Errorf("could not encode ascii armor: %v", err))
panic(fmt.Errorf("could not encode ascii armor: %w", err))
}
_, err = w.Write(data)
if err != nil {
panic(fmt.Errorf("could not encode ascii armor: %v", err))
panic(fmt.Errorf("could not encode ascii armor: %w", err))
}
err = w.Close()
if err != nil {
panic(fmt.Errorf("could not encode ascii armor: %v", err))
panic(fmt.Errorf("could not encode ascii armor: %w", err))
}
return buf.String()
}

View File

@ -39,7 +39,7 @@ func (u Uint) IsNil() bool {
func NewUintFromBigInt(i *big.Int) Uint {
u, err := checkNewUint(i)
if err != nil {
panic(fmt.Errorf("overflow: %s", err))
panic(fmt.Errorf("overflow: %w", err))
}
return u
}

View File

@ -823,11 +823,11 @@ func testnetify[T types.Application](ctx *Context, testnetAppCreator types.AppCr
_, _, _, _, _, proxyMetrics, _, _ := metrics(genDoc.ChainID) // nolint: dogsled // function from comet
proxyApp := proxy.NewAppConns(clientCreator, proxyMetrics)
if err := proxyApp.Start(); err != nil {
return nil, fmt.Errorf("error starting proxy app connections: %v", err)
return nil, fmt.Errorf("error starting proxy app connections: %w", err)
}
res, err := proxyApp.Query().Info(context, proxy.InfoRequest)
if err != nil {
return nil, fmt.Errorf("error calling Info: %v", err)
return nil, fmt.Errorf("error calling Info: %w", err)
}
err = proxyApp.Stop()
if err != nil {

View File

@ -3,7 +3,7 @@ package appmanager
// Config represents the configuration options for the app manager.
// TODO: implement comments for toml
type Config struct {
ValidateTxGasLimit uint64 `mapstructure:"validate-tx-gas-limit"` //TODO: check how this works on app mempool
ValidateTxGasLimit uint64 `mapstructure:"validate-tx-gas-limit"` // TODO: check how this works on app mempool
QueryGasLimit uint64 `mapstructure:"query-gas-limit"`
SimulationGasLimit uint64 `mapstructure:"simulation-gas-limit"`
}

View File

@ -553,7 +553,7 @@ func NewSimApp(
unorderedtx.NewSnapshotter(app.UnorderedTxManager),
)
if err != nil {
panic(fmt.Errorf("failed to register snapshot extension: %s", err))
panic(fmt.Errorf("failed to register snapshot extension: %w", err))
}
}

View File

@ -292,7 +292,7 @@ func NewSimApp(
unorderedtx.NewSnapshotter(app.UnorderedTxManager),
)
if err != nil {
panic(fmt.Errorf("failed to register snapshot extension: %s", err))
panic(fmt.Errorf("failed to register snapshot extension: %w", err))
}
}

View File

@ -167,7 +167,7 @@ func (itr *iterator) parseRow() {
value []byte
)
if err := itr.rows.Scan(&key, &value); err != nil {
itr.err = fmt.Errorf("failed to scan row: %s", err)
itr.err = fmt.Errorf("failed to scan row: %w", err)
itr.valid = false
return
}

View File

@ -935,18 +935,18 @@ func copyFile(src, dest string) (*os.File, error) {
func copyFilesInDir(src, dest string) error {
err := os.MkdirAll(dest, 0o750)
if err != nil {
return fmt.Errorf("mkdirs: %s", err)
return fmt.Errorf("mkdirs: %w", err)
}
fs, err := os.ReadDir(src)
if err != nil {
return fmt.Errorf("read dir: %s", err)
return fmt.Errorf("read dir: %w", err)
}
for _, f := range fs {
if f.IsDir() {
continue
}
if _, err := copyFile(filepath.Join(src, f.Name()), filepath.Join(dest, f.Name())); err != nil {
return fmt.Errorf("copy file: %q: %s", f.Name(), err)
return fmt.Errorf("copy file: %q: %w", f.Name(), err)
}
}
return nil

View File

@ -2,6 +2,7 @@ package cosmovisor
import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
@ -270,15 +271,18 @@ func (l *Launcher) doPreUpgrade() error {
if err := l.executePreUpgradeCmd(); err != nil {
counter++
switch err.(*exec.ExitError).ProcessState.ExitCode() {
case 1:
l.logger.Info("pre-upgrade command does not exist. continuing the upgrade.")
return nil
case 30:
return fmt.Errorf("pre-upgrade command failed : %w", err)
case 31:
l.logger.Error("pre-upgrade command failed. retrying", "error", err, "attempt", counter)
continue
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
switch exitErr.ProcessState.ExitCode() {
case 1:
l.logger.Info("pre-upgrade command does not exist. continuing the upgrade.")
return nil
case 30:
return fmt.Errorf("pre-upgrade command failed : %w", err)
case 31:
l.logger.Error("pre-upgrade command failed. retrying", "error", err, "attempt", counter)
continue
}
}
}

View File

@ -64,7 +64,7 @@ func (k BaseKeeper) InitGenesis(ctx context.Context, genState *types.GenesisStat
func (k BaseKeeper) ExportGenesis(ctx context.Context) (*types.GenesisState, error) {
totalSupply, _, err := k.GetPaginatedTotalSupply(ctx, &query.PageRequest{Limit: query.PaginationMaxLimit})
if err != nil {
return nil, fmt.Errorf("unable to fetch total supply %v", err)
return nil, fmt.Errorf("unable to fetch total supply %w", err)
}
rv := types.NewGenesisState(

View File

@ -91,7 +91,7 @@ func (am AppModule) InitGenesis(ctx context.Context, bz json.RawMessage) error {
var gs types.GenesisState
err := am.cdc.UnmarshalJSON(bz, &gs)
if err != nil {
return (fmt.Errorf("failed to unmarshal %s genesis state: %s", types.ModuleName, err))
return (fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err))
}
return am.keeper.InitGenesis(ctx, gs)

View File

@ -16,7 +16,7 @@ import (
// state.
func InitGenesis(ctx context.Context, k keeper.Keeper, gs *types.GenesisState) error {
if err := gs.Validate(); err != nil {
return fmt.Errorf("failed to validate %s genesis state: %s", types.ModuleName, err)
return fmt.Errorf("failed to validate %s genesis state: %w", types.ModuleName, err)
}
for _, e := range gs.Evidence {

View File

@ -47,7 +47,7 @@ func CreateDefaultCometConfig(rootDir string) (*cmtcfg.Config, error) {
cmtcfg.EnsureRoot(rootDir)
if err := conf.ValidateBasic(); err != nil {
return nil, fmt.Errorf("error in config file: %v", err)
return nil, fmt.Errorf("error in config file: %w", err)
}
return conf, nil

View File

@ -109,7 +109,7 @@ func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error {
func (am AppModule) RegisterMigrations(mr appmodule.MigrationRegistrar) error {
m := keeper.NewMigrator(am.keeper)
if err := mr.Register(group.ModuleName, 1, m.Migrate1to2); err != nil {
return fmt.Errorf("failed to migrate x/%s from version 1 to 2: %v", group.ModuleName, err)
return fmt.Errorf("failed to migrate x/%s from version 1 to 2: %w", group.ModuleName, err)
}
return nil

View File

@ -170,7 +170,7 @@ func (k Keeper) withdrawRecipientFunds(ctx context.Context, recipientAddr string
withdrawnAmount := sdk.NewCoin(denom, fundsAllocated)
err = k.DistributeFromStreamFunds(ctx, sdk.NewCoins(withdrawnAmount), recipient)
if err != nil {
return sdk.Coin{}, fmt.Errorf("error while distributing funds to the recipient %s: %v", recipientAddr, err)
return sdk.Coin{}, fmt.Errorf("error while distributing funds to the recipient %s: %w", recipientAddr, err)
}
// reset fund distribution
@ -220,7 +220,7 @@ func (k Keeper) SetToDistribute(ctx context.Context, amount sdk.Coins, addr stri
err = k.ToDistribute.Set(ctx, amount.AmountOf(denom))
if err != nil {
return fmt.Errorf("error while setting ToDistribute: %v", err)
return fmt.Errorf("error while setting ToDistribute: %w", err)
}
return nil
}

View File

@ -194,7 +194,7 @@ func (k MsgServer) CancelContinuousFund(ctx context.Context, msg *types.MsgCance
// withdraw funds if any are allocated
withdrawnFunds, err := k.withdrawRecipientFunds(ctx, msg.RecipientAddress)
if err != nil && !errorspkg.Is(err, types.ErrNoRecipientFund) {
return nil, fmt.Errorf("error while withdrawing already allocated funds for recipient %s: %v", msg.RecipientAddress, err)
return nil, fmt.Errorf("error while withdrawing already allocated funds for recipient %s: %w", msg.RecipientAddress, err)
}
if err := k.ContinuousFund.Remove(ctx, recipient); err != nil {

View File

@ -834,7 +834,7 @@ func (k Keeper) Unbond(
validator.TokensFromShares(delegation.Shares).TruncateInt().LT(validator.MinSelfDelegation) {
err = k.jailValidator(ctx, validator)
if err != nil {
return amount, fmt.Errorf("failed to jail validator: %v", err)
return amount, fmt.Errorf("failed to jail validator: %w", err)
}
validator, err = k.GetValidator(ctx, valbz)
if err != nil {

View File

@ -85,7 +85,7 @@ func (k Keeper) InitGenesis(ctx context.Context, data *types.GenesisState) ([]ap
for _, delegation := range data.Delegations {
delegatorAddress, err := k.authKeeper.AddressCodec().StringToBytes(delegation.DelegatorAddress)
if err != nil {
return nil, fmt.Errorf("invalid delegator address: %s", err)
return nil, fmt.Errorf("invalid delegator address: %w", err)
}
valAddr, err := k.validatorAddressCodec.StringToBytes(delegation.GetValidatorAddr())

View File

@ -114,19 +114,19 @@ func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error {
func (am AppModule) RegisterMigrations(mr appmodule.MigrationRegistrar) error {
m := keeper.NewMigrator(am.keeper)
if err := mr.Register(types.ModuleName, 1, m.Migrate1to2); err != nil {
return fmt.Errorf("failed to migrate x/%s from version 1 to 2: %v", types.ModuleName, err)
return fmt.Errorf("failed to migrate x/%s from version 1 to 2: %w", types.ModuleName, err)
}
if err := mr.Register(types.ModuleName, 2, m.Migrate2to3); err != nil {
return fmt.Errorf("failed to migrate x/%s from version 2 to 3: %v", types.ModuleName, err)
return fmt.Errorf("failed to migrate x/%s from version 2 to 3: %w", types.ModuleName, err)
}
if err := mr.Register(types.ModuleName, 3, m.Migrate3to4); err != nil {
return fmt.Errorf("failed to migrate x/%s from version 3 to 4: %v", types.ModuleName, err)
return fmt.Errorf("failed to migrate x/%s from version 3 to 4: %w", types.ModuleName, err)
}
if err := mr.Register(types.ModuleName, 4, m.Migrate4to5); err != nil {
return fmt.Errorf("failed to migrate x/%s from version 4 to 5: %v", types.ModuleName, err)
return fmt.Errorf("failed to migrate x/%s from version 4 to 5: %w", types.ModuleName, err)
}
if err := mr.Register(types.ModuleName, 5, m.Migrate5to6); err != nil {
return fmt.Errorf("failed to migrate x/%s from version 5 to 6: %v", types.ModuleName, err)
return fmt.Errorf("failed to migrate x/%s from version 5 to 6: %w", types.ModuleName, err)
}
return nil