Skip to content

Commit 9753980

Browse files
committed
refactor: standardize error handling by replacing Run with RunE and introducing proper error returns in wasp-cli commands
1 parent 3dca5a5 commit 9753980

File tree

6 files changed

+75
-31
lines changed

6 files changed

+75
-31
lines changed

tools/wasp-cli/chain/deploy.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,21 @@ func initDeployMoveContractCmd() *cobra.Command {
4545
Use: "deploy-move-contract",
4646
Short: "Deploy a new move contract and save its package id",
4747
Args: cobra.NoArgs,
48-
Run: func(cmd *cobra.Command, args []string) {
48+
RunE: func(cmd *cobra.Command, args []string) error {
4949
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
5050
defer cancel()
5151

5252
l1Client := cliclients.L1Client()
5353
kp := wallet.Load()
5454
packageID, err := l1Client.DeployISCContracts(ctx, cryptolib.SignerToIotaSigner(kp))
55-
log.Check(err)
55+
if err != nil {
56+
return err
57+
}
5658

5759
config.SetPackageID(packageID)
5860

5961
log.Printf("Move contract deployed.\nPackageID: %v\n", packageID.String())
62+
return nil
6063
},
6164
}
6265

tools/wasp-cli/chain/rebuild-index.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"runtime"
55

66
"fortio.org/safecast"
7-
"github.com/samber/lo"
87
"github.com/spf13/cobra"
98

109
hivedb "github.com/iotaledger/hive.go/db"
@@ -22,25 +21,35 @@ func initBuildIndex() *cobra.Command {
2221
Use: "build-index <waspdb path> <indexdb destination path>",
2322
Short: "Builds a new EVM JSONRPC index db",
2423
Args: cobra.ExactArgs(2),
25-
Run: func(cmd *cobra.Command, args []string) {
24+
RunE: func(cmd *cobra.Command, args []string) error {
2625
logger := log.HiveLogger()
2726

2827
waspDBPath := args[0]
2928
db, err := database.NewDatabase(hivedb.EngineRocksDB, waspDBPath, false, database.CacheSizeDefault)
30-
log.Check(err)
29+
if err != nil {
30+
return err
31+
}
3132

32-
waspDBStore := indexedstore.New(lo.Must(state.NewStoreReadonly(db.KVStore())))
33+
storeRO, err := state.NewStoreReadonly(db.KVStore())
34+
if err != nil {
35+
return err
36+
}
37+
waspDBStore := indexedstore.New(storeRO)
3338

3439
latestIndex, err := waspDBStore.LatestBlockIndex()
35-
log.Check(err)
40+
if err != nil {
41+
return err
42+
}
3643

3744
logger.LogInfo("Creating index in parallel mode.")
3845
logger.LogInfof("Latest block index: %d\n", latestIndex)
3946

4047
index := jsonrpc.NewIndex(waspDBStore.StateByTrieRoot, hivedb.EngineRocksDB, args[1])
4148

4249
block, err := waspDBStore.StateByIndex(latestIndex)
43-
log.Check(err)
50+
if err != nil {
51+
return err
52+
}
4453

4554
logger.LogInfof("Indexing with %d cores.\n", workers)
4655

@@ -51,8 +60,10 @@ func initBuildIndex() *cobra.Command {
5160
return waspDBStore
5261
}
5362

54-
err = index.IndexAllBlocksInParallel(logger, storeProvider, block.TrieRoot(), workers)
55-
log.Check(err)
63+
if err := index.IndexAllBlocksInParallel(logger, storeProvider, block.TrieRoot(), workers); err != nil {
64+
return err
65+
}
66+
return nil
5667
},
5768
}
5869

tools/wasp-cli/codec/cmd.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,21 @@ func initDecodeMetadataCmd() *cobra.Command {
143143
Use: "metadata <0x...>",
144144
Short: "Translates metadata from Hex to a humanly-readable format",
145145
Args: cobra.ExactArgs(1),
146-
Run: func(cmd *cobra.Command, args []string) {
147-
metadata, err := isc.RequestMetadataFromBytes(hexutil.MustDecode(args[0]))
148-
log.Check(err)
146+
RunE: func(cmd *cobra.Command, args []string) error {
147+
bytes, err := hexutil.Decode(args[0])
148+
if err != nil {
149+
return err
150+
}
151+
metadata, err := isc.RequestMetadataFromBytes(bytes)
152+
if err != nil {
153+
return err
154+
}
149155
jsonBytes, err := json.MarshalIndent(metadata, "", " ")
150-
log.Check(err)
156+
if err != nil {
157+
return err
158+
}
151159
log.Printf("%s\n", jsonBytes)
160+
return nil
152161
},
153162
}
154163
}
@@ -158,10 +167,17 @@ func initDecodeGasFeePolicy() *cobra.Command {
158167
Use: "fee-policy <0x...>",
159168
Short: "Translates gas fee policy from Hex to a humanly-readable format",
160169
Args: cobra.ExactArgs(1),
161-
Run: func(cmd *cobra.Command, args []string) {
170+
RunE: func(cmd *cobra.Command, args []string) error {
162171
bytes, err := cryptolib.DecodeHex(args[0])
163-
log.Check(err)
164-
log.Printf("%v", gas.MustFeePolicyFromBytes(bytes).String())
172+
if err != nil {
173+
return err
174+
}
175+
fp, err := gas.FeePolicyFromBytes(bytes)
176+
if err != nil {
177+
return err
178+
}
179+
log.Printf("%v", fp.String())
180+
return nil
165181
},
166182
}
167183
}
@@ -177,18 +193,22 @@ func initEncodeGasFeePolicy() *cobra.Command {
177193
Use: "fee-policy",
178194
Short: "Translates metadata from Hex to a humanly-readable format",
179195
Args: cobra.NoArgs,
180-
Run: func(cmd *cobra.Command, args []string) {
196+
RunE: func(cmd *cobra.Command, args []string) error {
181197
feePolicy := gas.DefaultFeePolicy()
182198

183199
if gasPerToken != "" {
184200
ratio, err := wasputil.Ratio32FromString(gasPerToken)
185-
log.Check(err)
201+
if err != nil {
202+
return err
203+
}
186204
feePolicy.GasPerToken = ratio
187205
}
188206

189207
if evmGasRatio != "" {
190208
ratio, err := wasputil.Ratio32FromString(evmGasRatio)
191-
log.Check(err)
209+
if err != nil {
210+
return err
211+
}
192212
feePolicy.EVMGasRatio = ratio
193213
}
194214

@@ -197,6 +217,7 @@ func initEncodeGasFeePolicy() *cobra.Command {
197217
}
198218

199219
log.Printf("%s", cryptolib.EncodeHex(feePolicy.Bytes()))
220+
return nil
200221
},
201222
}
202223

tools/wasp-cli/wallet/request-funds.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package wallet
22

33
import (
4-
"context"
5-
64
"github.com/spf13/cobra"
75

86
"github.com/iotaledger/wasp/v2/tools/wasp-cli/util"
@@ -17,18 +15,21 @@ func initRequestFundsCmd() *cobra.Command {
1715
Use: "request-funds",
1816
Short: "Request funds from the faucet",
1917
Args: cobra.NoArgs,
20-
Run: func(cmd *cobra.Command, args []string) {
18+
RunE: func(cmd *cobra.Command, args []string) error {
2119
address := wallet.Load().Address()
22-
log.Check(cliclients.L1Client().RequestFunds(context.Background(), *address))
20+
if err := cliclients.L1Client().RequestFunds(cmd.Context(), *address); err != nil {
21+
return err
22+
}
2323

2424
model := &RequestFundsModel{
2525
Address: address.String(),
2626
Message: "success",
2727
}
2828

29-
util.TryManageCoinsAmount(context.Background())
29+
util.TryManageCoinsAmount(cmd.Context())
3030

3131
log.PrintCLIOutput(model)
32+
return nil
3233
},
3334
}
3435
}

tools/wasp-cli/wallet/wallet.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ func initInitCmd() *cobra.Command {
2121
Use: "init",
2222
Short: "Initialize a new wallet",
2323
Args: cobra.NoArgs,
24-
Run: func(cmd *cobra.Command, args []string) {
24+
RunE: func(cmd *cobra.Command, args []string) error {
2525
wallet.InitWallet(initOverwrite)
2626

2727
config.SetWalletProviderString(string(wallet.GetWalletProvider()))
28-
log.Check(config.WriteConfig())
28+
if err := config.WriteConfig(); err != nil {
29+
return err
30+
}
31+
return nil
2932
},
3033
}
3134
cmd.Flags().BoolVar(&initOverwrite, "overwrite", false, "allow overwriting existing seed")

tools/wasp-cli/wallet/wallet_provider.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@ func initWalletProviderCmd() *cobra.Command {
1212
Use: "provider (keychain, ledger)",
1313
Short: "Get or set wallet provider (keychain, ledger)",
1414
Args: cobra.MaximumNArgs(1),
15-
Run: func(cmd *cobra.Command, args []string) {
15+
RunE: func(cmd *cobra.Command, args []string) error {
1616
if len(args) == 0 {
1717
log.Printf("Wallet provider: %s\n", string(wallet.GetWalletProvider()))
18-
return
18+
return nil
1919
}
2020

21-
log.Check(wallet.SetWalletProvider(wallet.WalletProvider(args[0])))
22-
log.Check(config.WriteConfig())
21+
if err := wallet.SetWalletProvider(wallet.WalletProvider(args[0])); err != nil {
22+
return err
23+
}
24+
if err := config.WriteConfig(); err != nil {
25+
return err
26+
}
27+
return nil
2328
},
2429
}
2530
}

0 commit comments

Comments
 (0)