Merge PR #2119: Parsed Err msgs

This commit is contained in:
Christopher Goes 2018-08-23 13:55:05 +02:00 committed by GitHub
commit aa82f6fb38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 9 deletions

View File

@ -21,6 +21,7 @@ BREAKING CHANGES
* SDK
* [core] \#1807 Switch from use of rational to decimal
* [types] \#1901 Validator interface's GetOwner() renamed to GetOperator()
* [types] \#2119 Parsed error messages and ABCI log errors to make them more human readable.
* Tendermint
@ -51,7 +52,7 @@ IMPROVEMENTS
* [cli] #2060 removed `--select` from `block` command
* Gaia
* [x/stake] [#2023](https://github.com/cosmos/cosmos-sdk/pull/2023) Terminate iteration loop in `UpdateBondedValidators` and `UpdateBondedValidatorsFull` when the first revoked validator is encountered and perform a sanity check.
* [x/stake] [#2023](https://github.com/cosmos/cosmos-sdk/pull/2023) Terminate iteration loop in `UpdateBondedValidators` and `UpdateBondedValidatorsFull` when the first revoked validator is encountered and perform a sanity check.
* [x/auth] Signature verification's gas cost now accounts for pubkey type. [#2046](https://github.com/tendermint/tendermint/pull/2046)
* SDK

View File

@ -2,9 +2,11 @@ package types
import (
"fmt"
"strings"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/cosmos/cosmos-sdk/wire"
abci "github.com/tendermint/tendermint/abci/types"
)
@ -225,7 +227,11 @@ func (err *sdkError) TraceSDK(format string, args ...interface{}) Error {
// Implements ABCIError.
// Overrides err.Error.Error().
func (err *sdkError) Error() string {
return fmt.Sprintf("Error{%d:%d,%#v}", err.codespace, err.code, err.cmnError)
return fmt.Sprintf(`ERROR:
Codespace: %d
Code: %d
Message: %#v
`, err.codespace, err.code, parseCmnError(err.cmnError.Error()))
}
// Implements ABCIError.
@ -245,13 +251,20 @@ func (err *sdkError) Code() CodeType {
// Implements ABCIError.
func (err *sdkError) ABCILog() string {
return fmt.Sprintf(`=== ABCI Log ===
Codespace: %v
Code: %v
ABCICode: %v
Error: %#v
=== /ABCI Log ===
`, err.codespace, err.code, err.ABCICode(), err.cmnError)
cdc := wire.NewCodec()
parsedErrMsg := parseCmnError(err.cmnError.Error())
jsonErr := humanReadableError{
Codespace: err.codespace,
Code: err.code,
ABCICode: err.ABCICode(),
Message: parsedErrMsg,
}
bz, er := cdc.MarshalJSON(jsonErr)
if er != nil {
panic(er)
}
stringifiedJSON := string(bz)
return stringifiedJSON
}
func (err *sdkError) Result() Result {
@ -268,3 +281,18 @@ func (err *sdkError) QueryResult() abci.ResponseQuery {
Log: err.ABCILog(),
}
}
func parseCmnError(err string) string {
if idx := strings.Index(err, "{"); idx != -1 {
err = err[idx+1 : len(err)-1]
}
return err
}
// nolint
type humanReadableError struct {
Codespace CodespaceType `json:"codespace"`
Code CodeType `json:"code"`
ABCICode ABCICodeType `json:"abci_code"`
Message string `json:"message"`
}

View File

@ -57,7 +57,10 @@ func TestErrFn(t *testing.T) {
err := errFn("")
codeType := codeTypes[i]
require.Equal(t, err.Code(), codeType, "Err function expected to return proper code. tc #%d", i)
require.Equal(t, err.Codespace(), CodespaceRoot, "Err function expected to return proper codespace. tc #%d", i)
require.Equal(t, err.Result().Code, ToABCICode(CodespaceRoot, codeType), "Err function expected to return proper ABCICode. tc #%d")
require.Equal(t, err.QueryResult().Code, uint32(err.ABCICode()), "Err function expected to return proper ABCICode from QueryResult. tc #%d")
require.Equal(t, err.QueryResult().Log, err.ABCILog(), "Err function expected to return proper ABCILog from QueryResult. tc #%d")
}
require.Equal(t, ABCICodeOK, ToABCICode(CodespaceRoot, CodeOK))