Skip to content

Commit

Permalink
format recovered error depends on the error type
Browse files Browse the repository at this point in the history
  • Loading branch information
yun-yeo committed Nov 17, 2020
1 parent 7a2a827 commit 32bfb4d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
24 changes: 23 additions & 1 deletion x/wasm/internal/keeper/querier.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package keeper

import (
"fmt"
"runtime/debug"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -116,7 +119,26 @@ func queryContractStore(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) (
// recover from out-of-gas panic
defer func() {
if r := recover(); r != nil {
err = sdkerrors.ErrOutOfGas
switch rType := r.(type) {
// TODO: Use ErrOutOfGas instead of ErrorOutOfGas which would allow us
// to keep the stracktrace.
case sdk.ErrorOutOfGas:
err = sdkerrors.Wrap(
sdkerrors.ErrOutOfGas, fmt.Sprintf(
"out of gas in location: %v; gasWanted: %d, gasUsed: %d",
rType.Descriptor, ctx.GasMeter().Limit(), ctx.GasMeter().GasConsumed(),
),
)

default:
err = sdkerrors.Wrap(
sdkerrors.ErrPanic, fmt.Sprintf(
"recovered: %v\nstack:\n%v", r, string(debug.Stack()),
),
)
}

bz = nil
}
}()

Expand Down
2 changes: 1 addition & 1 deletion x/wasm/internal/keeper/recursive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func TestGasOnExternalQuery(t *testing.T) {
if tc.expectPanic {
_, err = querier(ctx, []string{types.QueryContractStore}, abci.RequestQuery{Data: []byte(bz)})
require.Error(t, err)
require.Equal(t, err, sdkerror.ErrOutOfGas)
require.Contains(t, err.Error(), sdkerror.ErrOutOfGas.Error())
} else {
// otherwise, make sure we get a good success
_, err = querier(ctx, []string{types.QueryContractStore}, abci.RequestQuery{Data: []byte(bz)})
Expand Down

0 comments on commit 32bfb4d

Please sign in to comment.