Skip to content

Commit 8a0877e

Browse files
committed
refactor: handle wallet balance errors consistently with a detailed output format
1 parent 83db1b7 commit 8a0877e

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed

tools/wasp-cli/authentication/login.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ func initSetTokenCmd() *cobra.Command {
4040
err := format.PrintOutput(authOutput)
4141

4242
log.Check(err, "error formatting output")
43-
4443
},
4544
}
4645
waspcmd.WithWaspNodeFlag(cmd, &node)

tools/wasp-cli/format/wallet_balance_output.go

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,23 @@ type WalletBalanceData struct {
1414
Balances []*iotajsonrpc.Balance `json:"balances"`
1515
}
1616

17+
// WalletBalanceErrorData represents the data structure for wallet balance error cases
18+
type WalletBalanceErrorData struct {
19+
AddressIndex uint32 `json:"addressIndex"`
20+
Address string `json:"address"`
21+
Error string `json:"error"`
22+
}
23+
1724
// WalletBalanceOutput represents the output of wallet balance commands
1825
type WalletBalanceOutput struct {
1926
BaseOutput[WalletBalanceData]
2027
}
2128

29+
// WalletBalanceErrorOutput represents the output for wallet balance errors
30+
type WalletBalanceErrorOutput struct {
31+
BaseOutput[WalletBalanceErrorData]
32+
}
33+
2234
// NewWalletBalanceOutput creates a new wallet balance output
2335
func NewWalletBalanceOutput(addressIndex uint32, address string, balances []*iotajsonrpc.Balance, success bool) *WalletBalanceOutput {
2436
status := "success"
@@ -43,14 +55,14 @@ func NewWalletBalanceSuccess(addressIndex uint32, address string, balances []*io
4355
}
4456

4557
// NewWalletBalanceError creates an error wallet balance output
46-
func NewWalletBalanceError(addressIndex uint32, address string) *WalletBalanceOutput {
47-
data := WalletBalanceData{
58+
func NewWalletBalanceError(addressIndex uint32, address string, errorMsg string) *WalletBalanceErrorOutput {
59+
data := WalletBalanceErrorData{
4860
AddressIndex: addressIndex,
4961
Address: address,
50-
Balances: []*iotajsonrpc.Balance{},
62+
Error: errorMsg,
5163
}
5264

53-
return &WalletBalanceOutput{
65+
return &WalletBalanceErrorOutput{
5466
BaseOutput: NewBaseOutput("wallet_balance", "error", data),
5567
}
5668
}
@@ -125,3 +137,58 @@ func (wbo *WalletBalanceOutput) GetBalanceForCoinType(coinType string) (string,
125137
func (wbo *WalletBalanceOutput) IsSuccess() bool {
126138
return wbo.Status == "success"
127139
}
140+
141+
// ToTable returns the wallet balance error output as table rows
142+
func (wbeo *WalletBalanceErrorOutput) ToTable() [][]string {
143+
rows := [][]string{
144+
{"Address Index", "Address"},
145+
{strconv.FormatUint(uint64(wbeo.Data.AddressIndex), 10), wbeo.Data.Address},
146+
}
147+
148+
// Add empty row for spacing
149+
rows = append(rows, []string{"", ""})
150+
151+
// Add error information
152+
rows = append(rows, []string{"Error", wbeo.Data.Error})
153+
154+
return rows
155+
}
156+
157+
// Validate validates the wallet balance error output
158+
func (wbeo *WalletBalanceErrorOutput) Validate() error {
159+
// Validate base output
160+
if err := wbeo.BaseOutput.Validate(); err != nil {
161+
return err
162+
}
163+
164+
// Validate wallet balance error-specific fields
165+
if wbeo.Data.Address == "" {
166+
return fmt.Errorf("address cannot be empty for wallet balance error output")
167+
}
168+
169+
if wbeo.Data.Error == "" {
170+
return fmt.Errorf("error message cannot be empty for wallet balance error output")
171+
}
172+
173+
return nil
174+
}
175+
176+
// GetAddress returns the wallet address
177+
func (wbeo *WalletBalanceErrorOutput) GetAddress() string {
178+
return wbeo.Data.Address
179+
}
180+
181+
// GetAddressIndex returns the address index
182+
func (wbeo *WalletBalanceErrorOutput) GetAddressIndex() uint32 {
183+
return wbeo.Data.AddressIndex
184+
}
185+
186+
// GetError returns the error message
187+
func (wbeo *WalletBalanceErrorOutput) GetError() string {
188+
return wbeo.Data.Error
189+
}
190+
191+
// IsSuccess returns false since this is an error output
192+
func (wbeo *WalletBalanceErrorOutput) IsSuccess() bool {
193+
return false
194+
}

tools/wasp-cli/wallet/info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func initBalanceCmd() *cobra.Command {
3939
address := myWallet.Address()
4040
balance, err := cliclients.L1Client().GetAllBalances(context.Background(), address.AsIotaAddress())
4141
if err != nil {
42-
balanceOutput := format.NewWalletBalanceError(myWallet.AddressIndex(), address.String())
42+
balanceOutput := format.NewWalletBalanceError(myWallet.AddressIndex(), address.String(), err.Error())
4343
formatErr := format.PrintOutput(balanceOutput)
4444
if formatErr != nil {
4545
log.Printf("Error formatting output: %v", formatErr)

0 commit comments

Comments
 (0)