Skip to content

Commit

Permalink
fix: better format error of gnokey / client using Errorf instead of wrap
Browse files Browse the repository at this point in the history
Signed-off-by: gfanton <[email protected]>
  • Loading branch information
gfanton committed Nov 9, 2023
1 parent da05213 commit ce31780
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 21 deletions.
12 changes: 7 additions & 5 deletions tm2/pkg/crypto/keys/client/addpkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func signAndBroadcast(
}
qres, err := queryHandler(qopts)
if err != nil {
return errors.Wrap(err, "query account")
return fmt.Errorf("unable to query account: %w", err)
}
var qret struct{ BaseAccount std.BaseAccount }
err = amino.UnmarshalJSON(qres.Response.Data, &qret)
Expand Down Expand Up @@ -195,7 +195,7 @@ func signAndBroadcast(

signedTx, err := SignHandler(sopts)
if err != nil {
return errors.Wrap(err, "sign tx")
return fmt.Errorf("unable to sign tx: %w", err)
}

// broadcast signed tx
Expand All @@ -205,13 +205,15 @@ func signAndBroadcast(
}
bres, err := broadcastHandler(bopts)
if err != nil {
return errors.Wrap(err, "broadcast tx")
return fmt.Errorf("unable to broadcast tx: %w", err)
}
if bres.CheckTx.IsErr() {
return errors.Wrap(bres.CheckTx.Error, "check transaction failed: log:%s", bres.CheckTx.Log)
io.ErrPrintfln("response log: %s", bres.CheckTx.Log)
return fmt.Errorf("unable to check transaction: %w", bres.CheckTx.Error)
}
if bres.DeliverTx.IsErr() {
return errors.Wrap(bres.DeliverTx.Error, "deliver transaction failed: log:%s", bres.DeliverTx.Log)
io.ErrPrintfln("response log: %s", bres.DeliverTx.Log)
return fmt.Errorf("unable to deliver transaction: %w", bres.DeliverTx.Error)
}
io.Println(string(bres.DeliverTx.Data))
io.Println("OK!")
Expand Down
19 changes: 11 additions & 8 deletions tm2/pkg/crypto/keys/client/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client
import (
"context"
"flag"
"fmt"
"os"

"github.com/gnolang/gno/tm2/pkg/amino"
Expand Down Expand Up @@ -58,12 +59,12 @@ func execBroadcast(cfg *broadcastCfg, args []string, io *commands.IO) error {

jsonbz, err := os.ReadFile(filename)
if err != nil {
return errors.Wrap(err, "reading tx document file "+filename)
return fmt.Errorf("unable to read tx document file %q: %w", filename, err)
}
var tx std.Tx
err = amino.UnmarshalJSON(jsonbz, &tx)
if err != nil {
return errors.Wrap(err, "unmarshaling tx json bytes")
return fmt.Errorf("unable tounmarshal tx json: %w", err)
}
cfg.tx = &tx

Expand All @@ -73,9 +74,11 @@ func execBroadcast(cfg *broadcastCfg, args []string, io *commands.IO) error {
}

if res.CheckTx.IsErr() {
return errors.New("transaction failed %#v\nlog %s", res, res.CheckTx.Log)
io.ErrPrintln(res.CheckTx.Log)
return fmt.Errorf("unable to check transaction: %w", res.CheckTx.Error)
} else if res.DeliverTx.IsErr() {
return errors.New("transaction failed %#v\nlog %s", res, res.DeliverTx.Log)
io.ErrPrintln(res.DeliverTx.Log)
return fmt.Errorf("unable to deliver transaction: %w", res.DeliverTx.Error)
} else {
io.Println(string(res.DeliverTx.Data))
io.Println("OK!")
Expand All @@ -97,7 +100,7 @@ func broadcastHandler(cfg *broadcastCfg) (*ctypes.ResultBroadcastTxCommit, error

bz, err := amino.Marshal(cfg.tx)
if err != nil {
return nil, errors.Wrap(err, "remarshaling tx binary bytes")
return nil, fmt.Errorf("unable to remarshaling tx binary: %w", err)
}

cli := client.NewHTTP(remote, "/websocket")
Expand All @@ -108,7 +111,7 @@ func broadcastHandler(cfg *broadcastCfg) (*ctypes.ResultBroadcastTxCommit, error

bres, err := cli.BroadcastTxCommit(bz)
if err != nil {
return nil, errors.Wrap(err, "broadcasting bytes")
return nil, fmt.Errorf("unable to broadcast: %w", err)
}

return bres, nil
Expand All @@ -117,13 +120,13 @@ func broadcastHandler(cfg *broadcastCfg) (*ctypes.ResultBroadcastTxCommit, error
func simulateTx(cli client.ABCIClient, tx []byte) (*ctypes.ResultBroadcastTxCommit, error) {
bres, err := cli.ABCIQuery(".app/simulate", tx)
if err != nil {
return nil, errors.Wrap(err, "simulate tx")
return nil, fmt.Errorf("unable to simulate tx: %w", err)
}

var result abci.ResponseDeliverTx
err = amino.Unmarshal(bres.Response.Value, &result)
if err != nil {
return nil, errors.Wrap(err, "unmarshaling simulate result")
return nil, fmt.Errorf("unable to unmarshal simulate result: %w", err)
}

return &ctypes.ResultBroadcastTxCommit{
Expand Down
4 changes: 2 additions & 2 deletions tm2/pkg/crypto/keys/client/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ func execCall(cfg *callCfg, args []string, io *commands.IO) error {
// Parse send amount.
send, err := std.ParseCoins(cfg.send)
if err != nil {
return errors.Wrap(err, "parsing send coins")
return fmt.Errorf("unable to parse send coins: %w", err)
}

// parse gas wanted & fee.
gaswanted := cfg.rootCfg.gasWanted
gasfee, err := std.ParseCoin(cfg.rootCfg.gasFee)
if err != nil {
return errors.Wrap(err, "parsing gas fee coin")
return fmt.Errorf("unable to parse gas fee coin: %w", err)
}

// construct msg & tx and marshal.
Expand Down
2 changes: 1 addition & 1 deletion tm2/pkg/crypto/keys/client/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func execGenerate(cfg *generateCfg, args []string, io *commands.IO) error {
return err
}
if len(inputEntropy) < 43 {
return fmt.Errorf("256-bits is 43 characters in Base-64, and 100 in Base-6. You entered %v, and probably want more", len(inputEntropy))
return fmt.Errorf("256-bits is 43 characters in Base-64, and 100 in Base-6. You entered %d, and probably want more", len(inputEntropy))
}
conf, err := io.GetConfirmation(
fmt.Sprintf("Input length: %d", len(inputEntropy)),
Expand Down
6 changes: 3 additions & 3 deletions tm2/pkg/crypto/keys/client/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client
import (
"context"
"flag"
"fmt"

"github.com/gnolang/gno/tm2/pkg/bft/rpc/client"
ctypes "github.com/gnolang/gno/tm2/pkg/bft/rpc/core/types"
Expand Down Expand Up @@ -75,8 +76,7 @@ func execQuery(cfg *queryCfg, args []string, io *commands.IO) error {
}

if qres.Response.Error != nil {
io.Printf("Log: %s\n",
qres.Response.Log)
io.ErrPrintfln("response log: %s", qres.Response.Log)
return qres.Response.Error
}

Expand Down Expand Up @@ -105,7 +105,7 @@ func queryHandler(cfg *queryCfg) (*ctypes.ResultABCIQuery, error) {
qres, err := cli.ABCIQueryWithOptions(
cfg.path, data, opts2)
if err != nil {
return nil, errors.Wrap(err, "querying")
return nil, fmt.Errorf("unable to query: %w", err)
}

return qres, nil
Expand Down
4 changes: 2 additions & 2 deletions tm2/pkg/crypto/keys/client/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ func execSend(cfg *sendCfg, args []string, io *commands.IO) error {
// Parse send amount.
send, err := std.ParseCoins(cfg.send)
if err != nil {
return errors.Wrap(err, "parsing send coins")
return fmt.Errorf("unable to parse send coin: %w", err)
}

// parse gas wanted & fee.
gaswanted := cfg.rootCfg.gasWanted
gasfee, err := std.ParseCoin(cfg.rootCfg.gasFee)
if err != nil {
return errors.Wrap(err, "parsing gas fee coin")
return fmt.Errorf("unable to parse gas fee coin: %w", err)
}

// construct msg & tx and marshal.
Expand Down

0 comments on commit ce31780

Please sign in to comment.