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 * SDK
* [core] \#1807 Switch from use of rational to decimal * [core] \#1807 Switch from use of rational to decimal
* [types] \#1901 Validator interface's GetOwner() renamed to GetOperator() * [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 * Tendermint

View File

@ -2,9 +2,11 @@ package types
import ( import (
"fmt" "fmt"
"strings"
cmn "github.com/tendermint/tendermint/libs/common" cmn "github.com/tendermint/tendermint/libs/common"
"github.com/cosmos/cosmos-sdk/wire"
abci "github.com/tendermint/tendermint/abci/types" abci "github.com/tendermint/tendermint/abci/types"
) )
@ -225,7 +227,11 @@ func (err *sdkError) TraceSDK(format string, args ...interface{}) Error {
// Implements ABCIError. // Implements ABCIError.
// Overrides err.Error.Error(). // Overrides err.Error.Error().
func (err *sdkError) Error() string { 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. // Implements ABCIError.
@ -245,13 +251,20 @@ func (err *sdkError) Code() CodeType {
// Implements ABCIError. // Implements ABCIError.
func (err *sdkError) ABCILog() string { func (err *sdkError) ABCILog() string {
return fmt.Sprintf(`=== ABCI Log === cdc := wire.NewCodec()
Codespace: %v parsedErrMsg := parseCmnError(err.cmnError.Error())
Code: %v jsonErr := humanReadableError{
ABCICode: %v Codespace: err.codespace,
Error: %#v Code: err.code,
=== /ABCI Log === ABCICode: err.ABCICode(),
`, err.codespace, err.code, err.ABCICode(), err.cmnError) Message: parsedErrMsg,
}
bz, er := cdc.MarshalJSON(jsonErr)
if er != nil {
panic(er)
}
stringifiedJSON := string(bz)
return stringifiedJSON
} }
func (err *sdkError) Result() Result { func (err *sdkError) Result() Result {
@ -268,3 +281,18 @@ func (err *sdkError) QueryResult() abci.ResponseQuery {
Log: err.ABCILog(), 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("") err := errFn("")
codeType := codeTypes[i] codeType := codeTypes[i]
require.Equal(t, err.Code(), codeType, "Err function expected to return proper code. tc #%d", 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.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)) require.Equal(t, ABCICodeOK, ToABCICode(CodespaceRoot, CodeOK))