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

Create sdk.Result from any error type #4849

Merged
merged 3 commits into from
Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 28 additions & 4 deletions types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"strings"

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

abci "github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// CodeType - ABCI code identifier within codespace
Expand Down Expand Up @@ -248,10 +250,14 @@ func (err *sdkError) Code() CodeType {
// Implements ABCIError.
func (err *sdkError) ABCILog() string {
errMsg := err.cmnError.Error()
return encodeErrorLog(err.codespace, err.code, errMsg)
}

func encodeErrorLog(codespace CodespaceType, code CodeType, msg string) string {
jsonErr := humanReadableError{
Codespace: err.codespace,
Code: err.code,
Message: errMsg,
Codespace: codespace,
Code: code,
Message: msg,
}

var buff bytes.Buffer
Expand Down Expand Up @@ -282,6 +288,24 @@ func (err *sdkError) QueryResult() abci.ResponseQuery {
}
}

// ResultFromError will return err.Result() if it implements sdk.Error
// Otherwise, it will use the reflecton from types/error to determine
// the code, codespace, and log.
//
// This is intended to provide a bridge to allow both error types
// to live side-by-side.
func ResultFromError(err error) Result {
if sdk, ok := err.(Error); ok {
return sdk.Result()
}
space, code, log := sdkerrors.ABCIInfo(err, false)
return Result{
Codespace: CodespaceType(space),
Code: CodeType(code),
Log: encodeErrorLog(CodespaceType(space), CodeType(code), log),
}
}

//----------------------------------------
// REST error utilities

Expand Down
42 changes: 42 additions & 0 deletions types/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"testing"

"github.com/stretchr/testify/require"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

var codeTypes = []CodeType{
Expand Down Expand Up @@ -93,3 +95,43 @@ func TestAppendMsgToErr(t *testing.T) {
fmt.Sprintf("Should have formatted the error message of ABCI Log. tc #%d", i))
}
}

func TestResultFromError(t *testing.T) {
cases := map[string]struct {
err error
expect Result
}{
"sdk.Error": {
err: ErrUnauthorized("not owner"),
expect: Result{
Codespace: CodespaceRoot,
Code: CodeUnauthorized,
Log: `{"codespace":"sdk","code":4,"message":"not owner"}`,
},
},
"types/errors": {
err: sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "not owner"),
expect: Result{
Codespace: CodespaceRoot,
Code: CodeUnauthorized,
Log: `{"codespace":"sdk","code":4,"message":"not owner: unauthorized"}`,
},
},
"stdlib errors": {
err: fmt.Errorf("not owner"),
expect: Result{
Codespace: CodespaceType("undefined"),
Code: CodeInternal,
// note that we redact the internal errors in the new package to not leak eg. panics
Log: `{"codespace":"undefined","code":1,"message":"internal error"}`,
},
},
}

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
res := ResultFromError(tc.err)
require.Equal(t, tc.expect, res)
})
}
}