From d1305ea98639d004436df7fe0961c47bc0628b65 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Wed, 14 Aug 2024 01:13:23 +0200 Subject: [PATCH] feat(template): only panics the module in the most top function level (#4300) ## Description Avoid using a lot of panic calls into the `InitGenesis` and `ExportGenesis` methods and panic in the parent function. This also make the code more testable. --- changelog.md | 1 + .../base/x/{{moduleName}}/module/genesis.go.plush | 12 +++++------- .../x/{{moduleName}}/module/genesis_test.go.plush | 6 ++++-- .../base/x/{{moduleName}}/module/module.go.plush | 9 +++++++-- ignite/templates/module/create/ibc.go | 13 +++++++++++-- ignite/templates/typed/list/genesis.go | 8 ++++---- ignite/templates/typed/map/map.go | 4 ++-- ignite/templates/typed/singleton/singleton.go | 7 ++++--- 8 files changed, 38 insertions(+), 22 deletions(-) diff --git a/changelog.md b/changelog.md index 3c3343b345..8ccd41def0 100644 --- a/changelog.md +++ b/changelog.md @@ -16,6 +16,7 @@ - [#4111](https://github.com/ignite/cli/pull/4111) Remove vuex generation - [#4113](https://github.com/ignite/cli/pull/4113) Generate chain config documentation automatically - [#4131](https://github.com/ignite/cli/pull/4131) Support `bytes` as data type in the `scaffold` commands +- [#4300](https://github.com/ignite/cli/pull/4300) Only panics the module in the most top function level ### Changes diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/module/genesis.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/module/genesis.go.plush index ec2b160c25..0017540910 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/module/genesis.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/module/genesis.go.plush @@ -8,24 +8,22 @@ import ( ) // InitGenesis initializes the module's state from a provided genesis state. -func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) { +func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) error { // this line is used by starport scaffolding # genesis/module/init - if err := k.Params.Set(ctx, genState.Params); err != nil { - panic(err) - } + return k.Params.Set(ctx, genState.Params) } // ExportGenesis returns the module's exported genesis. -func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState { +func ExportGenesis(ctx sdk.Context, k keeper.Keeper) (*types.GenesisState, error) { var err error genesis := types.DefaultGenesis() genesis.Params, err = k.Params.Get(ctx) if err != nil { - panic(err) + return nil, err } // this line is used by starport scaffolding # genesis/module/export - return genesis + return genesis, nil } diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/module/genesis_test.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/module/genesis_test.go.plush index a305e68e7f..67d42f972a 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/module/genesis_test.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/module/genesis_test.go.plush @@ -18,8 +18,10 @@ func TestGenesis(t *testing.T) { } k, ctx, _ := keepertest.<%= title(moduleName) %>Keeper(t) - <%= moduleName %>.InitGenesis(ctx, k, genesisState) - got := <%= moduleName %>.ExportGenesis(ctx, k) + err := <%= moduleName %>.InitGenesis(ctx, k, genesisState) + require.NoError(t, err) + got, err := <%= moduleName %>.ExportGenesis(ctx, k) + require.NoError(t, err) require.NotNil(t, got) nullify.Fill(&genesisState) diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/module/module.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/module/module.go.plush index 2b8dc966d7..ee5f808ebe 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/module/module.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/module/module.go.plush @@ -144,12 +144,17 @@ func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, gs json.Ra // Initialize global index to index in genesis state cdc.MustUnmarshalJSON(gs, &genState) - InitGenesis(ctx, am.keeper, genState) + if err := InitGenesis(ctx, am.keeper, genState); err != nil { + panic(err) + } } // ExportGenesis returns the module's exported genesis state as raw JSON bytes. func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage { - genState := ExportGenesis(ctx, am.keeper) + genState, err := ExportGenesis(ctx, am.keeper) + if err != nil { + panic(err) + } return cdc.MustMarshalJSON(genState) } diff --git a/ignite/templates/module/create/ibc.go b/ignite/templates/module/create/ibc.go index 2f3a42e44a..a7eefb2671 100644 --- a/ignite/templates/module/create/ibc.go +++ b/ignite/templates/module/create/ibc.go @@ -64,6 +64,15 @@ func genesisModify(replacer placeholder.Replacer, opts *CreateOptions) genny.Run return err } + // Import + content, err := xast.AppendImports( + f.String(), + xast.WithLastImport("cosmossdk.io/errors"), + ) + if err != nil { + return err + } + // Genesis init templateInit := `%s k.SetPort(ctx, genState.PortId) @@ -74,11 +83,11 @@ if k.ShouldBound(ctx, genState.PortId) { // and claims the returned capability err := k.BindPort(ctx, genState.PortId) if err != nil { - panic("could not claim port capability: " + err.Error()) + return errors.Wrap(err, "could not claim port capability") } }` replacementInit := fmt.Sprintf(templateInit, typed.PlaceholderGenesisModuleInit) - content := replacer.Replace(f.String(), typed.PlaceholderGenesisModuleInit, replacementInit) + content = replacer.Replace(content, typed.PlaceholderGenesisModuleInit, replacementInit) // Genesis export templateExport := `genesis.PortId = k.GetPort(ctx) diff --git a/ignite/templates/typed/list/genesis.go b/ignite/templates/typed/list/genesis.go index 9612ffffd7..5999446885 100644 --- a/ignite/templates/typed/list/genesis.go +++ b/ignite/templates/typed/list/genesis.go @@ -126,13 +126,13 @@ func genesisModuleModify(replacer placeholder.Replacer, opts *typed.Options) gen templateModuleInit := `// Set all the %[2]v for _, elem := range genState.%[3]vList { if err := k.%[3]v.Set(ctx, elem.Id, elem); err != nil { - panic(err) + return err } } // Set %[2]v count if err := k.%[3]vSeq.Set(ctx, genState.%[3]vCount); err != nil { - panic(err) + return err } %[1]v` replacementModuleInit := fmt.Sprintf( @@ -149,12 +149,12 @@ err = k.%[2]v.Walk(ctx, nil, func(key uint64, elem types.%[2]v) (bool, error) { return false, nil }) if err != nil { - panic(err) + return nil, err } genesis.%[2]vCount, err = k.%[2]vSeq.Peek(ctx) if err != nil { - panic(err) + return nil, err } %[1]v` diff --git a/ignite/templates/typed/map/map.go b/ignite/templates/typed/map/map.go index c6219f33f4..29e1dfd252 100644 --- a/ignite/templates/typed/map/map.go +++ b/ignite/templates/typed/map/map.go @@ -403,7 +403,7 @@ func genesisModuleModify(replacer placeholder.Replacer, opts *typed.Options) gen templateModuleInit := `// Set all the %[2]v for _, elem := range genState.%[3]vList { if err := k.%[3]v.Set(ctx, elem.%[4]v, elem); err != nil { - panic(err) + return err } } %[1]v` @@ -420,7 +420,7 @@ for _, elem := range genState.%[3]vList { genesis.%[2]vList = append(genesis.%[2]vList, val) return false, nil }); err != nil { - panic(err) + return nil, err } %[1]v` replacementModuleExport := fmt.Sprintf( diff --git a/ignite/templates/typed/singleton/singleton.go b/ignite/templates/typed/singleton/singleton.go index c285309c67..3c1819f7f9 100644 --- a/ignite/templates/typed/singleton/singleton.go +++ b/ignite/templates/typed/singleton/singleton.go @@ -375,7 +375,7 @@ func genesisModuleModify(replacer placeholder.Replacer, opts *typed.Options) gen templateModuleInit := `// Set if defined if genState.%[3]v != nil { if err := k.%[3]v.Set(ctx, *genState.%[3]v); err != nil { - panic(err) + return err } } %[1]v` @@ -389,9 +389,10 @@ if genState.%[3]v != nil { templateModuleExport := `// Get all %[2]v %[2]v, err := k.%[3]v.Get(ctx) -if err == nil { - genesis.%[3]v = &%[2]v +if err != nil { + return nil, err } +genesis.%[3]v = &%[2]v %[1]v` replacementModuleExport := fmt.Sprintf( templateModuleExport,