Parsed Error msgs and added tests

This commit is contained in:
Federico Kunze 2018-08-22 16:15:45 +02:00
parent d8f01be211
commit 092a178c12
2 changed files with 41 additions and 8 deletions

View File

@ -1,7 +1,9 @@
package types
import (
"encoding/json"
"fmt"
"strings"
cmn "github.com/tendermint/tendermint/libs/common"
@ -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,15 @@ 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)
_ = err.Error()
parsedErrMsg := parseCmnError(err.cmnError.Error())
jsonErr := newHumanReadableError(err.codespace, err.code, err.ABCICode(), parsedErrMsg)
bz, er := json.Marshal(jsonErr)
if er != nil {
panic(er)
}
stringifiedJSON := string(bz)
return stringifiedJSON
}
func (err *sdkError) Result() Result {
@ -268,3 +276,25 @@ func (err *sdkError) QueryResult() abci.ResponseQuery {
Log: err.ABCILog(),
}
}
func parseCmnError(err string) string {
errArray := strings.Split(err, "{")
return errArray[1][:len(errArray[1])-1]
}
// nolint
type HumanReadableError struct {
Codespace CodespaceType `json:"codespace"`
Code CodeType `json:"code"`
ABCICode ABCICodeType `json:"abci_code"`
Message string `json:"message"`
}
func newHumanReadableError(codespace CodespaceType, code CodeType, ABCICode ABCICodeType, msg string) HumanReadableError {
return HumanReadableError{
Codespace: codespace,
Code: code,
ABCICode: ABCICode,
Message: msg,
}
}

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))