Skip to content

Commit

Permalink
fix(ecocredit): return gRPC errors for base queries (#1583)
Browse files Browse the repository at this point in the history
Co-authored-by: Ryan Christoffersen <12519942+ryanchristo@users.noreply.github.com>
  • Loading branch information
aleem1314 and ryanchristo authored Nov 2, 2022
1 parent f1cbc55 commit f1f02bf
Show file tree
Hide file tree
Showing 34 changed files with 121 additions and 100 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,5 @@ replace github.com/regen-network/regen-ledger/errors => ./errors
replace github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.7.0

replace github.com/confio/ics23/go => github.com/cosmos/cosmos-sdk/ics23/go v0.8.0

replace github.com/regen-network/regen-ledger/errors => ./errors
7 changes: 4 additions & 3 deletions x/ecocredit/base/keeper/query_all_balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/cosmos/cosmos-sdk/orm/model/ormlist"
sdk "github.com/cosmos/cosmos-sdk/types"
regenerrors "github.com/regen-network/regen-ledger/errors"

api "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1"
"github.com/regen-network/regen-ledger/types/ormutil"
Expand All @@ -16,7 +17,7 @@ import (
func (k Keeper) AllBalances(ctx context.Context, req *types.QueryAllBalancesRequest) (*types.QueryAllBalancesResponse, error) {
pg, err := ormutil.GogoPageReqToPulsarPageReq(req.Pagination)
if err != nil {
return nil, err
return nil, regenerrors.ErrInvalidArgument.Wrapf(err.Error())
}
it, err := k.stateStore.BatchBalanceTable().List(ctx, api.BatchBalancePrimaryKey{}, ormlist.Paginate(pg))
if err != nil {
Expand All @@ -33,7 +34,7 @@ func (k Keeper) AllBalances(ctx context.Context, req *types.QueryAllBalancesRequ

batch, err := k.stateStore.BatchTable().Get(ctx, balance.BatchKey)
if err != nil {
return nil, err
return nil, regenerrors.ErrNotFound.Wrapf("batch with key: %d", balance.BatchKey)
}

res.Balances = append(res.Balances, &types.BatchBalanceInfo{
Expand All @@ -46,7 +47,7 @@ func (k Keeper) AllBalances(ctx context.Context, req *types.QueryAllBalancesRequ
}
res.Pagination, err = ormutil.PulsarPageResToGogoPageRes(it.PageResponse())
if err != nil {
return nil, err
return nil, regenerrors.ErrInternal.Wrap(err.Error())
}
return &res, nil
}
5 changes: 3 additions & 2 deletions x/ecocredit/base/keeper/query_allowed_class_creators.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

baseapi "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1"
regenerrors "github.com/regen-network/regen-ledger/errors"
"github.com/regen-network/regen-ledger/types/ormutil"
types "github.com/regen-network/regen-ledger/x/ecocredit/base/types/v1"
)
Expand All @@ -15,7 +16,7 @@ import (
func (k Keeper) AllowedClassCreators(ctx context.Context, req *types.QueryAllowedClassCreatorsRequest) (*types.QueryAllowedClassCreatorsResponse, error) {
pg, err := ormutil.GogoPageReqToPulsarPageReq(req.Pagination)
if err != nil {
return nil, err
return nil, regenerrors.ErrInvalidArgument.Wrap(err.Error())
}

itr, err := k.stateStore.AllowedClassCreatorTable().List(ctx, baseapi.AllowedClassCreatorAddressIndexKey{}, ormlist.Paginate(pg))
Expand All @@ -36,7 +37,7 @@ func (k Keeper) AllowedClassCreators(ctx context.Context, req *types.QueryAllowe

pr, err := ormutil.PulsarPageResToGogoPageRes(itr.PageResponse())
if err != nil {
return nil, err
return nil, regenerrors.ErrInternal.Wrap(err.Error())
}

return &types.QueryAllowedClassCreatorsResponse{
Expand Down
8 changes: 4 additions & 4 deletions x/ecocredit/base/keeper/query_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

regenerrors "github.com/regen-network/regen-ledger/errors"
types "github.com/regen-network/regen-ledger/x/ecocredit/base/types/v1"
"github.com/regen-network/regen-ledger/x/ecocredit/server/utils"
)
Expand All @@ -15,17 +15,17 @@ import (
func (k Keeper) Balance(ctx context.Context, req *types.QueryBalanceRequest) (*types.QueryBalanceResponse, error) {
batch, err := k.stateStore.BatchTable().GetByDenom(ctx, req.BatchDenom)
if err != nil {
return nil, sdkerrors.ErrInvalidRequest.Wrapf("could not get batch with denom %s: %s", req.BatchDenom, err.Error())
return nil, regenerrors.ErrNotFound.Wrapf("could not get batch with denom %s: %s", req.BatchDenom, err.Error())
}

addr, err := sdk.AccAddressFromBech32(req.Address)
if err != nil {
return nil, err
return nil, regenerrors.ErrInvalidArgument.Wrap(err.Error())
}

balance, err := utils.GetBalance(ctx, k.stateStore.BatchBalanceTable(), addr, batch.Key)
if err != nil {
return nil, err
return nil, regenerrors.ErrNotFound.Wrapf("unable to get balance with batch key: %d", batch.Key)
}

info := types.BatchBalanceInfo{
Expand Down
8 changes: 4 additions & 4 deletions x/ecocredit/base/keeper/query_balance_by_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ import (

"github.com/cosmos/cosmos-sdk/orm/model/ormlist"
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/v1"
regenerrors "github.com/regen-network/regen-ledger/errors"
"github.com/regen-network/regen-ledger/types/ormutil"
types "github.com/regen-network/regen-ledger/x/ecocredit/base/types/v1"
)

func (k Keeper) BalancesByBatch(ctx context.Context, req *types.QueryBalancesByBatchRequest) (*types.QueryBalancesByBatchResponse, error) {
pg, err := ormutil.GogoPageReqToPulsarPageReq(req.Pagination)
if err != nil {
return nil, err
return nil, regenerrors.ErrInvalidArgument.Wrap(err.Error())
}
batch, err := k.stateStore.BatchTable().GetByDenom(ctx, req.BatchDenom)
if err != nil {
return nil, sdkerrors.ErrInvalidRequest.Wrapf("could not get batch with denom %s: %s", req.BatchDenom, err.Error())
return nil, regenerrors.ErrInvalidArgument.Wrapf("could not get batch with denom %s: %s", req.BatchDenom, err.Error())
}
it, err := k.stateStore.BatchBalanceTable().List(ctx, api.BatchBalanceBatchKeyAddressIndexKey{}.WithBatchKey(batch.Key), ormlist.Paginate(pg))
if err != nil {
Expand All @@ -43,7 +43,7 @@ func (k Keeper) BalancesByBatch(ctx context.Context, req *types.QueryBalancesByB
}
pr, err := ormutil.PulsarPageResToGogoPageRes(it.PageResponse())
if err != nil {
return nil, err
return nil, regenerrors.ErrInternal.Wrap(err.Error())
}
return &types.QueryBalancesByBatchResponse{
Balances: balances,
Expand Down
9 changes: 5 additions & 4 deletions x/ecocredit/base/keeper/query_balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

api "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1"
regenerrors "github.com/regen-network/regen-ledger/errors"
"github.com/regen-network/regen-ledger/types/ormutil"
types "github.com/regen-network/regen-ledger/x/ecocredit/base/types/v1"
)

func (k Keeper) Balances(ctx context.Context, req *types.QueryBalancesRequest) (*types.QueryBalancesResponse, error) {
addr, err := sdk.AccAddressFromBech32(req.Address)
if err != nil {
return nil, err
return nil, regenerrors.ErrInvalidArgument.Wrap(err.Error())
}

pg, err := ormutil.GogoPageReqToPulsarPageReq(req.Pagination)
if err != nil {
return nil, err
return nil, regenerrors.ErrInvalidArgument.Wrap(err.Error())
}

it, err := k.stateStore.BatchBalanceTable().List(ctx, api.BatchBalanceAddressBatchKeyIndexKey{}.WithAddress(addr), ormlist.Paginate(pg))
Expand All @@ -37,7 +38,7 @@ func (k Keeper) Balances(ctx context.Context, req *types.QueryBalancesRequest) (

batch, err := k.stateStore.BatchTable().Get(ctx, balance.BatchKey)
if err != nil {
return nil, err
return nil, regenerrors.ErrNotFound.Wrapf("balance with key: %d", balance.BatchKey)
}

info := types.BatchBalanceInfo{
Expand All @@ -53,7 +54,7 @@ func (k Keeper) Balances(ctx context.Context, req *types.QueryBalancesRequest) (

pr, err := ormutil.PulsarPageResToGogoPageRes(it.PageResponse())
if err != nil {
return nil, err
return nil, regenerrors.ErrInternal.Wrap(err.Error())
}

return &types.QueryBalancesResponse{Balances: balances, Pagination: pr}, nil
Expand Down
8 changes: 4 additions & 4 deletions x/ecocredit/base/keeper/query_batch_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

regenerrors "github.com/regen-network/regen-ledger/errors"
regentypes "github.com/regen-network/regen-ledger/types"
"github.com/regen-network/regen-ledger/x/ecocredit/base"
types "github.com/regen-network/regen-ledger/x/ecocredit/base/types/v1"
Expand All @@ -14,19 +14,19 @@ import (
// Batch queries for information on a credit batch.
func (k Keeper) Batch(ctx context.Context, request *types.QueryBatchRequest) (*types.QueryBatchResponse, error) {
if err := base.ValidateBatchDenom(request.BatchDenom); err != nil {
return nil, sdkerrors.ErrInvalidRequest.Wrapf("batch denom: %s", err)
return nil, regenerrors.ErrInvalidArgument.Wrapf("batch denom: %s", err)
}

batch, err := k.stateStore.BatchTable().GetByDenom(ctx, request.BatchDenom)
if err != nil {
return nil, sdkerrors.ErrInvalidRequest.Wrapf("could not get batch with denom %s: %s", request.BatchDenom, err.Error())
return nil, regenerrors.ErrNotFound.Wrapf("could not get batch with denom %s: %s", request.BatchDenom, err.Error())
}

issuer := sdk.AccAddress(batch.Issuer)

project, err := k.stateStore.ProjectTable().Get(ctx, batch.ProjectKey)
if err != nil {
return nil, err
return nil, regenerrors.ErrNotFound.Wrapf("could not get project with key %d", batch.ProjectKey)
}

info := types.BatchInfo{
Expand Down
7 changes: 4 additions & 3 deletions x/ecocredit/base/keeper/query_batches.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

api "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1"
regenerrors "github.com/regen-network/regen-ledger/errors"
regentypes "github.com/regen-network/regen-ledger/types"
"github.com/regen-network/regen-ledger/types/ormutil"
types "github.com/regen-network/regen-ledger/x/ecocredit/base/types/v1"
Expand All @@ -16,7 +17,7 @@ import (
func (k Keeper) Batches(ctx context.Context, request *types.QueryBatchesRequest) (*types.QueryBatchesResponse, error) {
pg, err := ormutil.GogoPageReqToPulsarPageReq(request.Pagination)
if err != nil {
return nil, err
return nil, regenerrors.ErrInvalidArgument.Wrap(err.Error())
}

it, err := k.stateStore.BatchTable().List(ctx, api.BatchPrimaryKey{}, ormlist.Paginate(pg))
Expand All @@ -36,7 +37,7 @@ func (k Keeper) Batches(ctx context.Context, request *types.QueryBatchesRequest)

project, err := k.stateStore.ProjectTable().Get(ctx, batch.ProjectKey)
if err != nil {
return nil, err
return nil, regenerrors.ErrNotFound.Wrapf("failed to get project by key: %d: %s", batch.ProjectKey, err.Error())
}

info := types.BatchInfo{
Expand All @@ -55,7 +56,7 @@ func (k Keeper) Batches(ctx context.Context, request *types.QueryBatchesRequest)

pr, err := ormutil.PulsarPageResToGogoPageRes(it.PageResponse())
if err != nil {
return nil, err
return nil, regenerrors.ErrInternal.Wrap(err.Error())
}

return &types.QueryBatchesResponse{
Expand Down
10 changes: 5 additions & 5 deletions x/ecocredit/base/keeper/query_batches_by_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (

"github.com/cosmos/cosmos-sdk/orm/model/ormlist"
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/v1"
regenerrors "github.com/regen-network/regen-ledger/errors"
regentypes "github.com/regen-network/regen-ledger/types"
"github.com/regen-network/regen-ledger/types/ormutil"
types "github.com/regen-network/regen-ledger/x/ecocredit/base/types/v1"
Expand All @@ -17,12 +17,12 @@ import (
func (k Keeper) BatchesByClass(ctx context.Context, request *types.QueryBatchesByClassRequest) (*types.QueryBatchesByClassResponse, error) {
pg, err := ormutil.GogoPageReqToPulsarPageReq(request.Pagination)
if err != nil {
return nil, err
return nil, regenerrors.ErrInvalidArgument.Wrap(err.Error())
}

class, err := k.stateStore.ClassTable().GetById(ctx, request.ClassId)
if err != nil {
return nil, sdkerrors.ErrInvalidRequest.Wrapf("could not get class with id %s: %s", request.ClassId, err.Error())
return nil, regenerrors.ErrNotFound.Wrapf("could not get class with id %s: %s", request.ClassId, err.Error())
}

// we put a "-" after the class name to avoid including class names outside of the query (i.e. a query for C01 could technically include C011 otherwise).
Expand All @@ -43,7 +43,7 @@ func (k Keeper) BatchesByClass(ctx context.Context, request *types.QueryBatchesB

project, err := k.stateStore.ProjectTable().Get(ctx, batch.ProjectKey)
if err != nil {
return nil, err
return nil, regenerrors.ErrNotFound.Wrapf("unable to get project with key: %d", batch.ProjectKey)
}

info := types.BatchInfo{
Expand All @@ -62,7 +62,7 @@ func (k Keeper) BatchesByClass(ctx context.Context, request *types.QueryBatchesB

pr, err := ormutil.PulsarPageResToGogoPageRes(it.PageResponse())
if err != nil {
return nil, err
return nil, regenerrors.ErrInternal.Wrap(err.Error())
}

return &types.QueryBatchesByClassResponse{
Expand Down
10 changes: 5 additions & 5 deletions x/ecocredit/base/keeper/query_batches_by_issuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (

"github.com/cosmos/cosmos-sdk/orm/model/ormlist"
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/v1"
regenerrors "github.com/regen-network/regen-ledger/errors"
regentypes "github.com/regen-network/regen-ledger/types"
"github.com/regen-network/regen-ledger/types/ormutil"
types "github.com/regen-network/regen-ledger/x/ecocredit/base/types/v1"
Expand All @@ -17,12 +17,12 @@ import (
func (k Keeper) BatchesByIssuer(ctx context.Context, req *types.QueryBatchesByIssuerRequest) (*types.QueryBatchesByIssuerResponse, error) {
issuer, err := sdk.AccAddressFromBech32(req.Issuer)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrap(err.Error())
return nil, regenerrors.ErrInvalidArgument.Wrapf("issuer: %s", err.Error())
}

pg, err := ormutil.GogoPageReqToPulsarPageReq(req.Pagination)
if err != nil {
return nil, err
return nil, regenerrors.ErrInvalidArgument.Wrap(err.Error())
}

it, err := k.stateStore.BatchTable().List(ctx, api.BatchIssuerIndexKey{}.WithIssuer(issuer), ormlist.Paginate(pg))
Expand All @@ -41,7 +41,7 @@ func (k Keeper) BatchesByIssuer(ctx context.Context, req *types.QueryBatchesByIs

project, err := k.stateStore.ProjectTable().Get(ctx, batch.ProjectKey)
if err != nil {
return nil, err
return nil, regenerrors.ErrNotFound.Wrapf("unable to get project by key: %d", batch.ProjectKey)
}

info := types.BatchInfo{
Expand All @@ -60,7 +60,7 @@ func (k Keeper) BatchesByIssuer(ctx context.Context, req *types.QueryBatchesByIs

pr, err := ormutil.PulsarPageResToGogoPageRes(it.PageResponse())
if err != nil {
return nil, err
return nil, regenerrors.ErrInternal.Wrap(err.Error())
}

return &types.QueryBatchesByIssuerResponse{Batches: batches, Pagination: pr}, nil
Expand Down
3 changes: 1 addition & 2 deletions x/ecocredit/base/keeper/query_batches_by_issuer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"gotest.tools/v3/assert"

"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"

api "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1"
Expand Down Expand Up @@ -70,5 +69,5 @@ func TestQueryBatchesByIssuer(t *testing.T) {

// query batches by an invalid address
_, err = s.k.BatchesByIssuer(s.ctx, &types.QueryBatchesByIssuerRequest{Issuer: "foobar"})
assert.ErrorContains(t, err, sdkerrors.ErrInvalidAddress.Error())
assert.ErrorContains(t, err, "decoding bech32 failed: invalid bech32 string length 6: invalid argument")
}
8 changes: 4 additions & 4 deletions x/ecocredit/base/keeper/query_batches_by_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (

"github.com/cosmos/cosmos-sdk/orm/model/ormlist"
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/v1"
regenerrors "github.com/regen-network/regen-ledger/errors"
regentypes "github.com/regen-network/regen-ledger/types"
"github.com/regen-network/regen-ledger/types/ormutil"
types "github.com/regen-network/regen-ledger/x/ecocredit/base/types/v1"
Expand All @@ -17,12 +17,12 @@ import (
func (k Keeper) BatchesByProject(ctx context.Context, request *types.QueryBatchesByProjectRequest) (*types.QueryBatchesByProjectResponse, error) {
pg, err := ormutil.GogoPageReqToPulsarPageReq(request.Pagination)
if err != nil {
return nil, err
return nil, regenerrors.ErrInvalidArgument.Wrap(err.Error())
}

project, err := k.stateStore.ProjectTable().GetById(ctx, request.ProjectId)
if err != nil {
return nil, sdkerrors.ErrInvalidRequest.Wrapf("could not get project with id %s: %s", request.ProjectId, err.Error())
return nil, regenerrors.ErrNotFound.Wrapf("could not get project with id %s: %s", request.ProjectId, err.Error())
}

it, err := k.stateStore.BatchTable().List(ctx, api.BatchProjectKeyIndexKey{}.WithProjectKey(project.Key), ormlist.Paginate(pg))
Expand Down Expand Up @@ -56,7 +56,7 @@ func (k Keeper) BatchesByProject(ctx context.Context, request *types.QueryBatche

pr, err := ormutil.PulsarPageResToGogoPageRes(it.PageResponse())
if err != nil {
return nil, err
return nil, regenerrors.ErrInternal.Wrap(err.Error())
}

return &types.QueryBatchesByProjectResponse{
Expand Down
3 changes: 2 additions & 1 deletion x/ecocredit/base/keeper/query_class_creator_allowlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package keeper
import (
"context"

regenerrors "github.com/regen-network/regen-ledger/errors"
types "github.com/regen-network/regen-ledger/x/ecocredit/base/types/v1"
)

// ClassCreatorAllowlist queries credit class allowlist setting.
func (k Keeper) ClassCreatorAllowlist(ctx context.Context, request *types.QueryClassCreatorAllowlistRequest) (*types.QueryClassCreatorAllowlistResponse, error) {
result, err := k.stateStore.ClassCreatorAllowlistTable().Get(ctx)
if err != nil {
return nil, err
return nil, regenerrors.ErrInternal.Wrapf("failed to get class creator allowlist: %s", err.Error())
}

return &types.QueryClassCreatorAllowlistResponse{
Expand Down
Loading

0 comments on commit f1f02bf

Please sign in to comment.