Skip to content

Commit

Permalink
nit: panic with error (#4741)
Browse files Browse the repository at this point in the history
* nit: panic with error

* panic with error

* replace sprintf with errorf in panic

* Update upgrade_test.go

* Update upgrade_test.go

* linter

* Update client_state.go

* fix test

* ftm.Errorf -> errors.New

* fix
  • Loading branch information
Carlos Rodriguez authored Sep 22, 2023
1 parent 0ae9d03 commit 65dd3ae
Show file tree
Hide file tree
Showing 40 changed files with 136 additions and 115 deletions.
2 changes: 1 addition & 1 deletion e2e/relayer/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func New(t *testing.T, cfg Config, logger *zap.Logger, dockerClient *dockerclien
case Hermes:
return newHermesRelayer(t, cfg.Tag, logger, dockerClient, network, cfg.Image)
default:
panic(fmt.Sprintf("unknown relayer specified: %s", cfg.Type))
panic(fmt.Errorf("unknown relayer specified: %s", cfg.Type))
}
}

Expand Down
3 changes: 2 additions & 1 deletion e2e/testsuite/testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testsuite

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

Expand Down Expand Up @@ -272,7 +273,7 @@ func (s *E2ETestSuite) RecoverRelayerWallets(ctx context.Context, ibcrelayer ibc
// StartRelayer starts the given ibcrelayer.
func (s *E2ETestSuite) StartRelayer(ibcrelayer ibc.Relayer) {
if s.startRelayerFn == nil {
panic("cannot start relayer before it is created!")
panic(errors.New("cannot start relayer before it is created!"))
}

s.startRelayerFn(ibcrelayer)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package controller

import (
"errors"

errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -239,7 +241,7 @@ func (IBCMiddleware) SendPacket(
timeoutTimestamp uint64,
data []byte,
) (uint64, error) {
panic("SendPacket not supported for ICA controller module. Please use SendTx")
panic(errors.New("SendPacket not supported for ICA controller module. Please use SendTx"))
}

// WriteAcknowledgement implements the ICS4 Wrapper interface
Expand All @@ -249,7 +251,7 @@ func (IBCMiddleware) WriteAcknowledgement(
packet ibcexported.PacketI,
ack ibcexported.Acknowledgement,
) error {
panic("WriteAcknowledgement not supported for ICA controller module")
panic(errors.New("WriteAcknowledgement not supported for ICA controller module"))
}

// GetAppVersion returns the interchain accounts metadata.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, state genesistypes.ControllerGe

// use the controller scoped keeper to claim the port capability
if err := keeper.ClaimCapability(ctx, capability, host.PortPath(portID)); err != nil {
panic(fmt.Sprintf("could not claim port capability: %v", err))
panic(fmt.Errorf("could not claim port capability: %v", err))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (k Keeper) OnChanOpenInit(
if found {
channel, found := k.channelKeeper.GetChannel(ctx, portID, activeChannelID)
if !found {
panic(fmt.Sprintf("active channel mapping set for %s but channel does not exist in channel store", activeChannelID))
panic(fmt.Errorf("active channel mapping set for %s but channel does not exist in channel store", activeChannelID))
}

if channel.IsOpen() {
Expand All @@ -73,7 +73,7 @@ func (k Keeper) OnChanOpenInit(

appVersion, found := k.GetAppVersion(ctx, portID, activeChannelID)
if !found {
panic(fmt.Sprintf("active channel mapping set for %s, but channel does not exist in channel store", activeChannelID))
panic(fmt.Errorf("active channel mapping set for %s, but channel does not exist in channel store", activeChannelID))
}

if !icatypes.IsPreviousMetadataEqual(appVersion, metadata) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"bytes"
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -53,7 +54,7 @@ func NewKeeper(
}

if strings.TrimSpace(authority) == "" {
panic(fmt.Errorf("authority must be non-empty"))
panic(errors.New("authority must be non-empty"))
}

return Keeper{
Expand Down Expand Up @@ -290,7 +291,7 @@ func (k Keeper) GetParams(ctx sdk.Context) types.Params {
store := ctx.KVStore(k.storeKey)
bz := store.Get([]byte(types.ParamsKey))
if bz == nil { // only panic on unset params and not on empty params
panic("ica/controller params are not set in store")
panic(errors.New("ica/controller params are not set in store"))
}

var params types.Params
Expand Down
4 changes: 2 additions & 2 deletions modules/apps/27-interchain-accounts/host/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, state genesistypes.HostGenesisS

// use the host scoped keeper to claim the port capability
if err := keeper.ClaimCapability(ctx, capability, host.PortPath(state.Port)); err != nil {
panic(fmt.Sprintf("could not claim port capability: %v", err))
panic(fmt.Errorf("could not claim port capability: %v", err))
}
}

Expand All @@ -34,7 +34,7 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, state genesistypes.HostGenesisS
}

if err := state.Params.Validate(); err != nil {
panic(fmt.Sprintf("could not set ica host params at genesis: %v", err))
panic(fmt.Errorf("could not set ica host params at genesis: %v", err))
}
keeper.SetParams(ctx, state.Params)
}
Expand Down
4 changes: 2 additions & 2 deletions modules/apps/27-interchain-accounts/host/keeper/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (k Keeper) OnChanOpenTry(
if found {
channel, found := k.channelKeeper.GetChannel(ctx, portID, activeChannelID)
if !found {
panic(fmt.Sprintf("active channel mapping set for %s but channel does not exist in channel store", activeChannelID))
panic(fmt.Errorf("active channel mapping set for %s but channel does not exist in channel store", activeChannelID))
}

if channel.IsOpen() {
Expand All @@ -57,7 +57,7 @@ func (k Keeper) OnChanOpenTry(

appVersion, found := k.GetAppVersion(ctx, portID, activeChannelID)
if !found {
panic(fmt.Sprintf("active channel mapping set for %s, but channel does not exist in channel store", activeChannelID))
panic(fmt.Errorf("active channel mapping set for %s, but channel does not exist in channel store", activeChannelID))
}

if !icatypes.IsPreviousMetadataEqual(appVersion, metadata) {
Expand Down
7 changes: 4 additions & 3 deletions modules/apps/27-interchain-accounts/host/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -51,7 +52,7 @@ func NewKeeper(
) Keeper {
// ensure ibc interchain accounts module account is set
if addr := accountKeeper.GetModuleAddress(icatypes.ModuleName); addr == nil {
panic("the Interchain Accounts module account has not been set")
panic(errors.New("the Interchain Accounts module account has not been set"))
}

// set KeyTable if it has not already been set
Expand All @@ -60,7 +61,7 @@ func NewKeeper(
}

if strings.TrimSpace(authority) == "" {
panic(fmt.Errorf("authority must be non-empty"))
panic(errors.New("authority must be non-empty"))
}

return Keeper{
Expand Down Expand Up @@ -243,7 +244,7 @@ func (k Keeper) GetParams(ctx sdk.Context) types.Params {
store := ctx.KVStore(k.storeKey)
bz := store.Get([]byte(types.ParamsKey))
if bz == nil { // only panic on unset params and not on empty params
panic("ica/host params are not set in store")
panic(errors.New("ica/host params are not set in store"))
}

var params types.Params
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func NewICAPath(chainA, chainB *ibctesting.TestChain, encoding string) *ibctesti
case icatypes.EncodingProto3JSON:
version = TestVersionWithJSONEncoding
default:
panic(fmt.Sprintf("unsupported encoding type: %s", encoding))
panic(fmt.Errorf("unsupported encoding type: %s", encoding))
}

path.EndpointA.ChannelConfig.PortID = icatypes.HostPortID
Expand Down
6 changes: 3 additions & 3 deletions modules/apps/27-interchain-accounts/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (am AppModule) InitModule(ctx sdk.Context, controllerParams controllertypes

if am.hostKeeper != nil {
if err := hostParams.Validate(); err != nil {
panic(fmt.Sprintf("could not set ica host params at initialization: %v", err))
panic(fmt.Errorf("could not set ica host params at initialization: %v", err))
}

hostkeeper.InitGenesis(ctx, *am.hostKeeper, genesistypes.HostGenesisState{
Expand All @@ -156,7 +156,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {

controllerMigrator := controllerkeeper.NewMigrator(am.controllerKeeper)
if err := cfg.RegisterMigration(types.ModuleName, 1, controllerMigrator.AssertChannelCapabilityMigrations); err != nil {
panic(fmt.Sprintf("failed to migrate interchainaccounts app from version 1 to 2 (channel capabilities owned by controller submodule check): %v", err))
panic(fmt.Errorf("failed to migrate interchainaccounts app from version 1 to 2 (channel capabilities owned by controller submodule check): %v", err))
}

hostMigrator := hostkeeper.NewMigrator(am.hostKeeper)
Expand All @@ -166,7 +166,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
}
return controllerMigrator.MigrateParams(ctx)
}); err != nil {
panic(fmt.Sprintf("failed to migrate interchainaccounts app from version 2 to 3 (self-managed params migration): %v", err))
panic(fmt.Errorf("failed to migrate interchainaccounts app from version 2 to 3 (self-managed params migration): %v", err))
}
}

Expand Down
2 changes: 1 addition & 1 deletion modules/apps/27-interchain-accounts/simulation/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewDecodeStore() func(kvA, kvB kv.Pair) string {
return fmt.Sprintf("IsMiddlewareEnabled A: %s\nIsMiddlewareEnabled B: %s", string(kvA.Value), string(kvB.Value))

default:
panic(fmt.Sprintf("invalid %s key prefix %s", types.ModuleName, kvA.Key))
panic(fmt.Errorf("invalid %s key prefix %s", types.ModuleName, kvA.Key))
}
}
}
4 changes: 2 additions & 2 deletions modules/apps/29-fee/keeper/escrow.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (k Keeper) DistributePacketFeesOnAcknowledgement(ctx sdk.Context, forwardRe
// check if refundAcc address works
refundAddr, err := sdk.AccAddressFromBech32(packetFee.RefundAddress)
if err != nil {
panic(fmt.Sprintf("could not parse refundAcc %s to sdk.AccAddress", packetFee.RefundAddress))
panic(fmt.Errorf("could not parse refundAcc %s to sdk.AccAddress", packetFee.RefundAddress))
}

k.distributePacketFeeOnAcknowledgement(cacheCtx, refundAddr, forwardAddr, reverseRelayer, packetFee)
Expand Down Expand Up @@ -122,7 +122,7 @@ func (k Keeper) DistributePacketFeesOnTimeout(ctx sdk.Context, timeoutRelayer sd
// check if refundAcc address works
refundAddr, err := sdk.AccAddressFromBech32(packetFee.RefundAddress)
if err != nil {
panic(fmt.Sprintf("could not parse refundAcc %s to sdk.AccAddress", packetFee.RefundAddress))
panic(fmt.Errorf("could not parse refundAcc %s to sdk.AccAddress", packetFee.RefundAddress))
}

k.distributePacketFeeOnTimeout(cacheCtx, refundAddr, timeoutRelayer, packetFee)
Expand Down
3 changes: 2 additions & 1 deletion modules/apps/callbacks/callbacks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ibccallbacks_test

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

Expand Down Expand Up @@ -46,7 +47,7 @@ func SetupTestingApp() (ibctesting.TestingApp, map[string]json.RawMessage) {
func GetSimApp(chain *ibctesting.TestChain) *simapp.SimApp {
app, ok := chain.App.(*simapp.SimApp)
if !ok {
panic("chain is not a simapp.SimApp")
panic(errors.New("chain is not a simapp.SimApp"))
}
return app
}
Expand Down
7 changes: 4 additions & 3 deletions modules/apps/callbacks/ibc_middleware.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ibccallbacks

import (
"errors"
"fmt"

errorsmod "cosmossdk.io/errors"
Expand Down Expand Up @@ -48,15 +49,15 @@ func NewIBCMiddleware(
}

if ics4Wrapper == nil {
panic(fmt.Errorf("ICS4Wrapper cannot be nil"))
panic(errors.New("ICS4Wrapper cannot be nil"))
}

if contractKeeper == nil {
panic(fmt.Errorf("contract keeper cannot be nil"))
panic(errors.New("contract keeper cannot be nil"))
}

if maxCallbackGas == 0 {
panic(fmt.Errorf("maxCallbackGas cannot be zero"))
panic(errors.New("maxCallbackGas cannot be zero"))
}

return IBCMiddleware{
Expand Down
5 changes: 3 additions & 2 deletions modules/apps/callbacks/testing/simapp/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package simapp

import (
"encoding/json"
"errors"
"log"

storetypes "cosmossdk.io/store/types"
Expand Down Expand Up @@ -206,7 +207,7 @@ func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []
addr := sdk.ValAddress(stakingtypes.AddressFromValidatorsKey(iter.Key()))
validator, err := app.StakingKeeper.GetValidator(ctx, addr)
if err != nil {
panic("expected validator, not found")
panic(errors.New("expected validator, not found"))
}

validator.UnbondingHeight = 0
Expand All @@ -216,7 +217,7 @@ func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []

err = app.StakingKeeper.SetValidator(ctx, validator)
if err != nil {
panic("couldn't set validator")
panic(errors.New("couldn't set validator"))
}
counter++
}
Expand Down
2 changes: 1 addition & 1 deletion modules/apps/transfer/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, state types.GenesisState) {
// and claims the returned capability
err := k.BindPort(ctx, state.PortId)
if err != nil {
panic(fmt.Sprintf("could not claim port capability: %v", err))
panic(fmt.Errorf("could not claim port capability: %v", err))
}
}

Expand Down
9 changes: 5 additions & 4 deletions modules/apps/transfer/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -56,15 +57,15 @@ func NewKeeper(
) Keeper {
// ensure ibc transfer module account is set
if addr := authKeeper.GetModuleAddress(types.ModuleName); addr == nil {
panic("the IBC transfer module account has not been set")
panic(errors.New("the IBC transfer module account has not been set"))
}
// set KeyTable if it has not already been set
if !legacySubspace.HasKeyTable() {
legacySubspace = legacySubspace.WithKeyTable(types.ParamKeyTable())
}

if strings.TrimSpace(authority) == "" {
panic(fmt.Errorf("authority must be non-empty"))
panic(errors.New("authority must be non-empty"))
}

return Keeper{
Expand Down Expand Up @@ -128,7 +129,7 @@ func (k Keeper) GetParams(ctx sdk.Context) types.Params {
store := ctx.KVStore(k.storeKey)
bz := store.Get([]byte(types.ParamsKey))
if bz == nil { // only panic on unset params and not on empty params
panic("transfer params are not set in store")
panic(errors.New("transfer params are not set in store"))
}

var params types.Params
Expand Down Expand Up @@ -239,7 +240,7 @@ func (k Keeper) GetTotalEscrowForDenom(ctx sdk.Context, denom string) sdk.Coin {
// if the amount is negative.
func (k Keeper) SetTotalEscrowForDenom(ctx sdk.Context, coin sdk.Coin) {
if coin.Amount.IsNegative() {
panic(fmt.Sprintf("amount cannot be negative: %s", coin.Amount))
panic(fmt.Errorf("amount cannot be negative: %s", coin.Amount))
}

store := ctx.KVStore(k.storeKey)
Expand Down
Loading

0 comments on commit 65dd3ae

Please sign in to comment.