From d97c5769e78db727b6b20e80281da17a4ce9fe50 Mon Sep 17 00:00:00 2001 From: MD Aleem <72057206+aleem1314@users.noreply.github.com> Date: Sat, 23 Jul 2022 05:41:50 +0530 Subject: [PATCH] fix(x/ecocredit): improved error for basket queries (#1290) * fix: update query response errors * chore: add changelog * chore: fix failing test * Update x/ecocredit/server/basket/query_basket_test.go Co-authored-by: Ryan Christoffersen <12519942+ryanchristo@users.noreply.github.com> * chore: review changes Co-authored-by: Ryan Christoffersen <12519942+ryanchristo@users.noreply.github.com> --- CHANGELOG.md | 2 ++ x/ecocredit/server/basket/query_balance.go | 21 ++++++++++++++-- .../server/basket/query_balance_test.go | 25 ++++++++++++++++++- x/ecocredit/server/basket/query_balances.go | 7 +++++- .../server/basket/query_balances_test.go | 2 +- x/ecocredit/server/basket/query_basket.go | 8 +++++- .../server/basket/query_basket_test.go | 9 ++++++- x/ecocredit/server/testsuite/suite.go | 6 ++--- 8 files changed, 70 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b5070d1b00..9baceeb402 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [#1278](https://github.com/regen-network/regen-ledger/pull/1278) Fix `open` not set in `Msg/CreateBatch` - [#1281](https://github.com/regen-network/regen-ledger/pull/1281) Fix curator address in state migrations - [#1284](https://github.com/regen-network/regen-ledger/pull/1284) Fix basket state migration +- [#1290](https://github.com/regen-network/regen-ledger/pull/1290) Fix error message when basket not found. +- [#1290](https://github.com/regen-network/regen-ledger/pull/1290) Return empty balance when basket balance not found. ## [v4.0.0-rc1](https://github.com/regen-network/regen-ledger/releases/tag/v4.0.0-rc1) - 2022-07-15 diff --git a/x/ecocredit/server/basket/query_balance.go b/x/ecocredit/server/basket/query_balance.go index 062ad3fa1e..12bb85192c 100644 --- a/x/ecocredit/server/basket/query_balance.go +++ b/x/ecocredit/server/basket/query_balance.go @@ -6,6 +6,9 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + "github.com/cosmos/cosmos-sdk/orm/types/ormerrors" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + baskettypes "github.com/regen-network/regen-ledger/x/ecocredit/basket" ) @@ -16,12 +19,26 @@ func (k Keeper) BasketBalance(ctx context.Context, request *baskettypes.QueryBas basket, err := k.stateStore.BasketTable().GetByBasketDenom(ctx, request.BasketDenom) if err != nil { - return nil, err + if ormerrors.IsNotFound(err) { + return nil, sdkerrors.Wrapf(err, "basket %s not found", request.BasketDenom) + } + return nil, sdkerrors.Wrapf(err, "failed to get basket %s", request.BasketDenom) + } + + found, err := k.coreStore.BatchTable().HasByDenom(ctx, request.BatchDenom) + if err != nil { + return nil, sdkerrors.Wrapf(err, "failed to get credit batch %s", request.BatchDenom) + } + if !found { + return nil, ormerrors.NotFound.Wrapf("credit batch %s not found", request.BatchDenom) } balance, err := k.stateStore.BasketBalanceTable().Get(ctx, basket.Id, request.BatchDenom) if err != nil { - return nil, err + if ormerrors.IsNotFound(err) { + return &baskettypes.QueryBasketBalanceResponse{Balance: "0"}, nil + } + return nil, sdkerrors.Wrapf(err, "failed to get basket balance for %s", request.BasketDenom) } return &baskettypes.QueryBasketBalanceResponse{Balance: balance.Balance}, nil diff --git a/x/ecocredit/server/basket/query_balance_test.go b/x/ecocredit/server/basket/query_balance_test.go index 8af5a7c681..b619611b89 100644 --- a/x/ecocredit/server/basket/query_balance_test.go +++ b/x/ecocredit/server/basket/query_balance_test.go @@ -6,6 +6,7 @@ import ( "github.com/stretchr/testify/require" api "github.com/regen-network/regen-ledger/api/regen/ecocredit/basket/v1" + coreapi "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1" baskettypes "github.com/regen-network/regen-ledger/x/ecocredit/basket" ) @@ -22,6 +23,11 @@ func TestKeeper_BasketBalance(t *testing.T) { }) require.NoError(t, err) + err = s.coreStore.BatchTable().Insert(s.ctx, &coreapi.Batch{ + Denom: batchDenom, + }) + require.NoError(t, err) + // add a balance require.NoError(t, s.stateStore.BasketBalanceTable().Insert(s.ctx, &api.BasketBalance{ BasketId: id, @@ -38,9 +44,26 @@ func TestKeeper_BasketBalance(t *testing.T) { require.Equal(t, balance, res.Balance) // bad query - res, err = s.k.BasketBalance(s.ctx, &baskettypes.QueryBasketBalanceRequest{ + _, err = s.k.BasketBalance(s.ctx, &baskettypes.QueryBasketBalanceRequest{ BasketDenom: batchDenom, BatchDenom: basketDenom, }) require.Error(t, err) + + // add another basket + basketDenom = "foo1" + basketName := "foo1.bar" + err = s.stateStore.BasketTable().Insert(s.ctx, &api.Basket{ + BasketDenom: basketDenom, + Name: basketName, + }) + require.NoError(t, err) + + // expect empty basket balance + res, err = s.k.BasketBalance(s.ctx, &baskettypes.QueryBasketBalanceRequest{ + BasketDenom: basketDenom, + BatchDenom: batchDenom, + }) + require.NoError(t, err) + require.Equal(t, res.Balance, "0") } diff --git a/x/ecocredit/server/basket/query_balances.go b/x/ecocredit/server/basket/query_balances.go index 9d6ec631b5..9e9555193f 100644 --- a/x/ecocredit/server/basket/query_balances.go +++ b/x/ecocredit/server/basket/query_balances.go @@ -7,6 +7,8 @@ import ( "google.golang.org/grpc/status" "github.com/cosmos/cosmos-sdk/orm/model/ormlist" + "github.com/cosmos/cosmos-sdk/orm/types/ormerrors" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" api "github.com/regen-network/regen-ledger/api/regen/ecocredit/basket/v1" "github.com/regen-network/regen-ledger/types/ormutil" @@ -20,7 +22,10 @@ func (k Keeper) BasketBalances(ctx context.Context, request *baskettypes.QueryBa basket, err := k.stateStore.BasketTable().GetByBasketDenom(ctx, request.BasketDenom) if err != nil { - return nil, err + if ormerrors.IsNotFound(err) { + return nil, sdkerrors.Wrapf(err, "basket %s not found", request.BasketDenom) + } + return nil, sdkerrors.Wrapf(err, "failed to get basket %s", request.BasketDenom) } pulsarPageReq, err := ormutil.GogoPageReqToPulsarPageReq(request.Pagination) diff --git a/x/ecocredit/server/basket/query_balances_test.go b/x/ecocredit/server/basket/query_balances_test.go index fadada78ed..9a2abedc51 100644 --- a/x/ecocredit/server/basket/query_balances_test.go +++ b/x/ecocredit/server/basket/query_balances_test.go @@ -66,7 +66,7 @@ func TestQueryBalances(t *testing.T) { require.Equal(t, "4.20", res.Balances[1].Balance) // bad query - res, err = s.k.BasketBalances(s.ctx, &baskettypes.QueryBasketBalancesRequest{BasketDenom: "nope"}) + _, err = s.k.BasketBalances(s.ctx, &baskettypes.QueryBasketBalancesRequest{BasketDenom: "nope"}) require.Error(t, err) require.Contains(t, err.Error(), "not found") } diff --git a/x/ecocredit/server/basket/query_basket.go b/x/ecocredit/server/basket/query_basket.go index 1b4bb7bf12..f473986323 100644 --- a/x/ecocredit/server/basket/query_basket.go +++ b/x/ecocredit/server/basket/query_basket.go @@ -6,7 +6,10 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + "github.com/cosmos/cosmos-sdk/orm/types/ormerrors" sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + api "github.com/regen-network/regen-ledger/api/regen/ecocredit/basket/v1" "github.com/regen-network/regen-ledger/types/ormutil" baskettypes "github.com/regen-network/regen-ledger/x/ecocredit/basket" @@ -19,7 +22,10 @@ func (k Keeper) Basket(ctx context.Context, request *baskettypes.QueryBasketRequ basket, err := k.stateStore.BasketTable().GetByBasketDenom(ctx, request.BasketDenom) if err != nil { - return nil, err + if ormerrors.IsNotFound(err) { + return nil, sdkerrors.Wrapf(err, "basket %s not found", request.BasketDenom) + } + return nil, sdkerrors.Wrapf(err, "failed to get basket %s", request.BasketDenom) } basketGogo := &baskettypes.Basket{} diff --git a/x/ecocredit/server/basket/query_basket_test.go b/x/ecocredit/server/basket/query_basket_test.go index 9fe56bc249..506ca0ce7b 100644 --- a/x/ecocredit/server/basket/query_basket_test.go +++ b/x/ecocredit/server/basket/query_basket_test.go @@ -29,7 +29,7 @@ func TestKeeper_Basket(t *testing.T) { require.Equal(t, basketDenom, res.Basket.BasketDenom) // bad query - res, err = s.k.Basket(s.ctx, &baskettypes.QueryBasketRequest{ + _, err = s.k.Basket(s.ctx, &baskettypes.QueryBasketRequest{ BasketDenom: batchDenom, }) require.Error(t, err) @@ -61,4 +61,11 @@ func TestKeeper_BasketClasses(t *testing.T) { require.NoError(t, err) require.Equal(t, basketDenom, res.Basket.BasketDenom) require.Equal(t, []string{classId}, res.Classes) + + // query unknown basket + _, err = s.k.Basket(s.ctx, &baskettypes.QueryBasketRequest{ + BasketDenom: "unknown", + }) + require.Error(t, err) + require.ErrorContains(t, err, "basket unknown not found") } diff --git a/x/ecocredit/server/testsuite/suite.go b/x/ecocredit/server/testsuite/suite.go index fe80e436b5..c6f136a97f 100644 --- a/x/ecocredit/server/testsuite/suite.go +++ b/x/ecocredit/server/testsuite/suite.go @@ -277,9 +277,9 @@ func (s *IntegrationTestSuite) TestBasketScenario() { BasketDenom: basketDenom, BatchDenom: batchDenom, }) - require.Error(err) - require.Contains(err.Error(), "not found") - require.Nil(bRes) + require.NoError(err) + require.NotNil(bRes) + require.Equal(bRes.Balance, "0") // basket token balance of user2 should be empty now endBal := s.getUserBalance(user2, basketDenom)