Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R: Improve SDK Error Messages & Allow Unicode #3604

Merged
merged 6 commits into from
Feb 11, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ IMPROVEMENTS
* Gaia

* SDK
* [\#3601] Improve SDK funds related error messages and allow for unicode in
JSON ABCI log.

* Tendermint

Expand Down
20 changes: 12 additions & 8 deletions types/errors.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package types

import (
"bytes"
"encoding/json"
"fmt"
"strings"

"github.com/pkg/errors"
cmn "github.com/tendermint/tendermint/libs/common"

"github.com/cosmos/cosmos-sdk/codec"

abci "github.com/tendermint/tendermint/abci/types"
)

Expand Down Expand Up @@ -246,19 +247,22 @@ func (err *sdkError) Code() CodeType {

// Implements ABCIError.
func (err *sdkError) ABCILog() string {
cdc := codec.New()
errMsg := err.cmnError.Error()
jsonErr := humanReadableError{
Codespace: err.codespace,
Code: err.code,
Message: errMsg,
}
bz, er := cdc.MarshalJSON(jsonErr)
if er != nil {
panic(er)

var buff bytes.Buffer
enc := json.NewEncoder(&buff)
enc.SetEscapeHTML(false)

if err := enc.Encode(jsonErr); err != nil {
panic(errors.Wrap(err, "failed to encode ABCI error log"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++

}
stringifiedJSON := string(bz)
return stringifiedJSON

return buff.String()
}

func (err *sdkError) Result() Result {
Expand Down
12 changes: 7 additions & 5 deletions x/auth/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,20 +306,22 @@ func DeductFees(blockTime time.Time, acc Account, fee StdFee) (Account, sdk.Resu
// get the resulting coins deducting the fees
newCoins, ok := coins.SafeMinus(feeAmount)
if ok {
errMsg := fmt.Sprintf("%s < %s", coins, feeAmount)
return nil, sdk.ErrInsufficientFunds(errMsg).Result()
return nil, sdk.ErrInsufficientFunds(
fmt.Sprintf("insufficient funds to pay for fees; %s < %s", coins, feeAmount),
).Result()
}

// Validate the account has enough "spendable" coins as this will cover cases
// such as vesting accounts.
spendableCoins := acc.SpendableCoins(blockTime)
if _, hasNeg := spendableCoins.SafeMinus(feeAmount); hasNeg {
return nil, sdk.ErrInsufficientFunds(fmt.Sprintf("%s < %s", spendableCoins, feeAmount)).Result()
return nil, sdk.ErrInsufficientFunds(
fmt.Sprintf("insufficient funds to pay for fees; %s < %s", spendableCoins, feeAmount),
).Result()
}

if err := acc.SetCoins(newCoins); err != nil {
// Handle w/ #870
panic(err)
return nil, sdk.ErrInternal(err.Error()).Result()
}

return acc, sdk.Result{}
Expand Down
12 changes: 9 additions & 3 deletions x/bank/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ func subtractCoins(ctx sdk.Context, ak auth.AccountKeeper, addr sdk.AccAddress,
// So the check here is sufficient instead of subtracting from oldCoins.
_, hasNeg := spendableCoins.SafeMinus(amt)
if hasNeg {
return amt, nil, sdk.ErrInsufficientCoins(fmt.Sprintf("%s < %s", spendableCoins, amt))
return amt, nil, sdk.ErrInsufficientCoins(
fmt.Sprintf("insufficient account funds; %s < %s", spendableCoins, amt),
)
}

newCoins := oldCoins.Minus(amt) // should not panic as spendable coins was already checked
Expand All @@ -246,7 +248,9 @@ func addCoins(ctx sdk.Context, am auth.AccountKeeper, addr sdk.AccAddress, amt s
newCoins := oldCoins.Plus(amt)

if newCoins.IsAnyNegative() {
return amt, nil, sdk.ErrInsufficientCoins(fmt.Sprintf("%s < %s", oldCoins, amt))
return amt, nil, sdk.ErrInsufficientCoins(
fmt.Sprintf("insufficient account funds; %s < %s", oldCoins, amt),
)
}

err := setCoins(ctx, am, addr, newCoins)
Expand Down Expand Up @@ -319,7 +323,9 @@ func delegateCoins(

_, hasNeg := oldCoins.SafeMinus(amt)
if hasNeg {
return nil, sdk.ErrInsufficientCoins(fmt.Sprintf("%s < %s", oldCoins, amt))
return nil, sdk.ErrInsufficientCoins(
fmt.Sprintf("insufficient account funds; %s < %s", oldCoins, amt),
)
}

if err := trackDelegation(acc, ctx.BlockHeader().Time, amt); err != nil {
Expand Down