From 7fad21d800c79000dd102d50cef8e51dfffa67fa Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Fri, 20 Oct 2017 19:51:01 +0200 Subject: [PATCH] Use own ErrNoData, not from light-client --- client/errors.go | 22 ++++++++++++++++++++++ client/errors_test.go | 18 ++++++++++++++++++ client/query.go | 4 ++-- client/query_test.go | 6 +++--- modules/coin/commands/query.go | 5 ++--- modules/coin/rest/handlers.go | 4 ++-- modules/nonce/commands/query.go | 5 ++--- modules/nonce/rest/handlers.go | 4 ++-- 8 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 client/errors.go create mode 100644 client/errors_test.go diff --git a/client/errors.go b/client/errors.go new file mode 100644 index 000000000..9f69f5e12 --- /dev/null +++ b/client/errors.go @@ -0,0 +1,22 @@ +package client + +import ( + "fmt" + + "github.com/pkg/errors" +) + +//-------------------------------------------- + +var errNoData = fmt.Errorf("No data returned for query") + +// IsNoDataErr checks whether an error is due to a query returning empty data +func IsNoDataErr(err error) bool { + return errors.Cause(err) == errNoData +} + +func ErrNoData() error { + return errors.WithStack(errNoData) +} + +//-------------------------------------------- diff --git a/client/errors_test.go b/client/errors_test.go new file mode 100644 index 000000000..c561c35b7 --- /dev/null +++ b/client/errors_test.go @@ -0,0 +1,18 @@ +package client + +import ( + "errors" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestErrorNoData(t *testing.T) { + e1 := ErrNoData() + e1.Error() + assert.True(t, IsNoDataErr(e1)) + + e2 := errors.New("foobar") + assert.False(t, IsNoDataErr(e2)) + assert.False(t, IsNoDataErr(nil)) +} diff --git a/client/query.go b/client/query.go index c6c058943..96da9bc22 100644 --- a/client/query.go +++ b/client/query.go @@ -38,7 +38,7 @@ func GetWithProof(key []byte, reqHeight int, node client.Client, return } if len(resp.Key) == 0 || len(resp.Proof) == 0 { - err = lc.ErrNoData() + err = ErrNoData() return } if resp.Height == 0 { @@ -84,7 +84,7 @@ func GetWithProof(key []byte, reqHeight int, node client.Client, err = errors.Wrap(err, "Couldn't verify proof") return } - err = lc.ErrNoData() + err = ErrNoData() proof = aproof } diff --git a/client/query_test.go b/client/query_test.go index 8faff5599..a3b945ba4 100644 --- a/client/query_test.go +++ b/client/query_test.go @@ -9,14 +9,14 @@ import ( "github.com/stretchr/testify/require" "github.com/tendermint/go-wire" - lc "github.com/tendermint/light-client" "github.com/tendermint/light-client/certifiers" certclient "github.com/tendermint/light-client/certifiers/client" + "github.com/tendermint/tmlibs/log" + nm "github.com/tendermint/tendermint/node" "github.com/tendermint/tendermint/rpc/client" rpctest "github.com/tendermint/tendermint/rpc/test" "github.com/tendermint/tendermint/types" - "github.com/tendermint/tmlibs/log" sdkapp "github.com/cosmos/cosmos-sdk/app" "github.com/cosmos/cosmos-sdk/modules/eyes" @@ -98,7 +98,7 @@ func TestAppProofs(t *testing.T) { // Test non-existing key. missing := []byte("my-missing-key") bs, _, proof, err = GetWithProof(missing, 0, cl, cert) - require.True(lc.IsNoDataErr(err)) + require.True(IsNoDataErr(err)) require.Nil(bs) require.NotNil(proof) err = proof.Verify(missing, nil, rootHash) diff --git a/modules/coin/commands/query.go b/modules/coin/commands/query.go index c1e0d9f55..1b13bdd2a 100644 --- a/modules/coin/commands/query.go +++ b/modules/coin/commands/query.go @@ -5,8 +5,7 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" - lc "github.com/tendermint/light-client" - + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/commands" "github.com/cosmos/cosmos-sdk/client/commands/query" "github.com/cosmos/cosmos-sdk/modules/coin" @@ -35,7 +34,7 @@ func accountQueryCmd(cmd *cobra.Command, args []string) error { acc := coin.Account{} prove := !viper.GetBool(commands.FlagTrustNode) height, err := query.GetParsed(key, &acc, query.GetHeight(), prove) - if lc.IsNoDataErr(err) { + if client.IsNoDataErr(err) { return errors.Errorf("Account bytes are empty for address %s ", addr) } else if err != nil { return err diff --git a/modules/coin/rest/handlers.go b/modules/coin/rest/handlers.go index 6deaf4494..a7362c706 100644 --- a/modules/coin/rest/handlers.go +++ b/modules/coin/rest/handlers.go @@ -10,6 +10,7 @@ import ( "github.com/spf13/viper" sdk "github.com/cosmos/cosmos-sdk" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/commands" "github.com/cosmos/cosmos-sdk/client/commands/query" "github.com/cosmos/cosmos-sdk/modules/auth" @@ -18,7 +19,6 @@ import ( "github.com/cosmos/cosmos-sdk/modules/fee" "github.com/cosmos/cosmos-sdk/modules/nonce" "github.com/cosmos/cosmos-sdk/stack" - lightclient "github.com/tendermint/light-client" "github.com/tendermint/tmlibs/common" ) @@ -62,7 +62,7 @@ func doQueryAccount(w http.ResponseWriter, r *http.Request) { account := new(coin.Account) prove := !viper.GetBool(commands.FlagTrustNode) height, err := query.GetParsed(key, account, h, prove) - if lightclient.IsNoDataErr(err) { + if client.IsNoDataErr(err) { err := fmt.Errorf("account bytes are empty for address: %q", signature) common.WriteError(w, err) return diff --git a/modules/nonce/commands/query.go b/modules/nonce/commands/query.go index 860f7b3aa..7021c8ca5 100644 --- a/modules/nonce/commands/query.go +++ b/modules/nonce/commands/query.go @@ -7,9 +7,8 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" - lc "github.com/tendermint/light-client" - sdk "github.com/cosmos/cosmos-sdk" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/commands" "github.com/cosmos/cosmos-sdk/client/commands/query" "github.com/cosmos/cosmos-sdk/modules/nonce" @@ -46,7 +45,7 @@ func doNonceQuery(signers []sdk.Actor) (sequence uint32, height uint64, err erro key := stack.PrefixedKey(nonce.NameNonce, nonce.GetSeqKey(signers)) prove := !viper.GetBool(commands.FlagTrustNode) height, err = query.GetParsed(key, &sequence, query.GetHeight(), prove) - if lc.IsNoDataErr(err) { + if client.IsNoDataErr(err) { // no data, return sequence 0 return 0, 0, nil } diff --git a/modules/nonce/rest/handlers.go b/modules/nonce/rest/handlers.go index f22601951..05c0b2cd8 100644 --- a/modules/nonce/rest/handlers.go +++ b/modules/nonce/rest/handlers.go @@ -9,6 +9,7 @@ import ( "github.com/spf13/viper" sdk "github.com/cosmos/cosmos-sdk" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/commands" "github.com/cosmos/cosmos-sdk/client/commands/query" "github.com/cosmos/cosmos-sdk/errors" @@ -16,7 +17,6 @@ import ( "github.com/cosmos/cosmos-sdk/modules/nonce" "github.com/cosmos/cosmos-sdk/stack" wire "github.com/tendermint/go-wire" - lightclient "github.com/tendermint/light-client" "github.com/tendermint/tmlibs/common" ) @@ -48,7 +48,7 @@ func doQueryNonce(w http.ResponseWriter, r *http.Request) { // query sequence number data, height, err := query.Get(key, h, prove) - if lightclient.IsNoDataErr(err) { + if client.IsNoDataErr(err) { err = fmt.Errorf("nonce empty for address: %q", signature) common.WriteError(w, err) return