remove errors module
This commit is contained in:
parent
8d305039c8
commit
561d77cae0
109
errors/common.go
109
errors/common.go
|
@ -1,109 +0,0 @@
|
|||
//nolint
|
||||
package errors
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
abci "github.com/tendermint/abci/types"
|
||||
)
|
||||
|
||||
var (
|
||||
errDecoding = fmt.Errorf("Error decoding input")
|
||||
errUnauthorized = fmt.Errorf("Unauthorized")
|
||||
errTooLarge = fmt.Errorf("Input size too large")
|
||||
errMissingSignature = fmt.Errorf("Signature missing")
|
||||
errUnknownTxType = fmt.Errorf("Tx type unknown")
|
||||
errInvalidFormat = fmt.Errorf("Invalid format")
|
||||
errUnknownModule = fmt.Errorf("Unknown module")
|
||||
errUnknownKey = fmt.Errorf("Unknown key")
|
||||
|
||||
internalErr = abci.CodeType_InternalError
|
||||
encodingErr = abci.CodeType_EncodingError
|
||||
unauthorized = abci.CodeType_Unauthorized
|
||||
unknownRequest = abci.CodeType_UnknownRequest
|
||||
unknownAddress = abci.CodeType_BaseUnknownAddress
|
||||
)
|
||||
|
||||
// some crazy reflection to unwrap any generated struct.
|
||||
func unwrap(i interface{}) interface{} {
|
||||
v := reflect.ValueOf(i)
|
||||
m := v.MethodByName("Unwrap")
|
||||
if m.IsValid() {
|
||||
out := m.Call(nil)
|
||||
if len(out) == 1 {
|
||||
return out[0].Interface()
|
||||
}
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
func ErrUnknownTxType(tx interface{}) TMError {
|
||||
msg := fmt.Sprintf("%T", unwrap(tx))
|
||||
return WithMessage(msg, errUnknownTxType, unknownRequest)
|
||||
}
|
||||
func IsUnknownTxTypeErr(err error) bool {
|
||||
return IsSameError(errUnknownTxType, err)
|
||||
}
|
||||
|
||||
func ErrInvalidFormat(expected string, tx interface{}) TMError {
|
||||
msg := fmt.Sprintf("%T not %s", unwrap(tx), expected)
|
||||
return WithMessage(msg, errInvalidFormat, unknownRequest)
|
||||
}
|
||||
func IsInvalidFormatErr(err error) bool {
|
||||
return IsSameError(errInvalidFormat, err)
|
||||
}
|
||||
|
||||
func ErrUnknownModule(mod string) TMError {
|
||||
return WithMessage(mod, errUnknownModule, unknownRequest)
|
||||
}
|
||||
func IsUnknownModuleErr(err error) bool {
|
||||
return IsSameError(errUnknownModule, err)
|
||||
}
|
||||
|
||||
func ErrUnknownKey(mod string) TMError {
|
||||
return WithMessage(mod, errUnknownKey, unknownRequest)
|
||||
}
|
||||
func IsUnknownKeyErr(err error) bool {
|
||||
return IsSameError(errUnknownKey, err)
|
||||
}
|
||||
|
||||
func ErrInternal(msg string) TMError {
|
||||
return New(msg, internalErr)
|
||||
}
|
||||
|
||||
// IsInternalErr matches any error that is not classified
|
||||
func IsInternalErr(err error) bool {
|
||||
return HasErrorCode(err, internalErr)
|
||||
}
|
||||
|
||||
func ErrDecoding() TMError {
|
||||
return WithCode(errDecoding, encodingErr)
|
||||
}
|
||||
func IsDecodingErr(err error) bool {
|
||||
return IsSameError(errDecoding, err)
|
||||
}
|
||||
|
||||
func ErrUnauthorized() TMError {
|
||||
return WithCode(errUnauthorized, unauthorized)
|
||||
}
|
||||
|
||||
// IsUnauthorizedErr is generic helper for any unauthorized errors,
|
||||
// also specific sub-types
|
||||
func IsUnauthorizedErr(err error) bool {
|
||||
return HasErrorCode(err, unauthorized)
|
||||
}
|
||||
|
||||
func ErrMissingSignature() TMError {
|
||||
return WithCode(errMissingSignature, unauthorized)
|
||||
}
|
||||
func IsMissingSignatureErr(err error) bool {
|
||||
return IsSameError(errMissingSignature, err)
|
||||
}
|
||||
|
||||
func ErrTooLarge() TMError {
|
||||
return WithCode(errTooLarge, encodingErr)
|
||||
}
|
||||
func IsTooLargeErr(err error) bool {
|
||||
return IsSameError(errTooLarge, err)
|
||||
}
|
|
@ -1,78 +0,0 @@
|
|||
package errors
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type validate interface {
|
||||
ValidateBasic() error
|
||||
}
|
||||
|
||||
type holder struct {
|
||||
validate
|
||||
}
|
||||
|
||||
func (h holder) Unwrap() validate {
|
||||
return h.validate
|
||||
}
|
||||
|
||||
type demoTx struct {
|
||||
Age int
|
||||
}
|
||||
|
||||
func (t demoTx) Wrap() holder {
|
||||
return holder{t}
|
||||
}
|
||||
|
||||
func (t demoTx) ValidateBasic() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestErrorMatches(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
cases := []struct {
|
||||
pattern, err error
|
||||
match bool
|
||||
}{
|
||||
{errDecoding, ErrDecoding(), true},
|
||||
{errUnauthorized, ErrUnauthorized(), true},
|
||||
{errMissingSignature, ErrUnauthorized(), false},
|
||||
{errMissingSignature, ErrMissingSignature(), true},
|
||||
{errUnknownTxType, ErrUnknownTxType(holder{}), true},
|
||||
{errUnknownTxType, ErrUnknownTxType("some text here..."), true},
|
||||
{errUnknownTxType, ErrUnknownTxType(demoTx{5}.Wrap()), true},
|
||||
}
|
||||
|
||||
for i, tc := range cases {
|
||||
same := IsSameError(tc.pattern, tc.err)
|
||||
assert.Equal(tc.match, same, "%d: %#v / %#v", i, tc.pattern, tc.err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChecks(t *testing.T) {
|
||||
// TODO: make sure the Is and Err methods match
|
||||
assert := assert.New(t)
|
||||
|
||||
cases := []struct {
|
||||
err error
|
||||
check func(error) bool
|
||||
match bool
|
||||
}{
|
||||
{ErrDecoding(), IsDecodingErr, true},
|
||||
{ErrUnauthorized(), IsDecodingErr, false},
|
||||
{ErrUnauthorized(), IsUnauthorizedErr, true},
|
||||
// make sure lots of things match InternalErr, but not everything
|
||||
{ErrInternal("bad db connection"), IsInternalErr, true},
|
||||
{Wrap(errors.New("wrapped")), IsInternalErr, true},
|
||||
{ErrUnauthorized(), IsInternalErr, false},
|
||||
}
|
||||
|
||||
for i, tc := range cases {
|
||||
match := tc.check(tc.err)
|
||||
assert.Equal(tc.match, match, "%d", i)
|
||||
}
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
package errors
|
||||
|
||||
// CheckErr is the type of all the check functions here
|
||||
type CheckErr func(error) bool
|
||||
|
||||
// NoErr is useful for test cases when you want to fulfil the CheckErr type
|
||||
func NoErr(err error) bool {
|
||||
return err == nil
|
||||
}
|
139
errors/main.go
139
errors/main.go
|
@ -1,139 +0,0 @@
|
|||
package errors
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
abci "github.com/tendermint/abci/types"
|
||||
)
|
||||
|
||||
const defaultErrCode = abci.CodeType_InternalError
|
||||
|
||||
type stackTracer interface {
|
||||
error
|
||||
StackTrace() errors.StackTrace
|
||||
}
|
||||
|
||||
type causer interface {
|
||||
Cause() error
|
||||
}
|
||||
|
||||
// TMError is the tendermint abci return type with stack trace
|
||||
type TMError interface {
|
||||
stackTracer
|
||||
ErrorCode() abci.CodeType
|
||||
Message() string
|
||||
}
|
||||
|
||||
type tmerror struct {
|
||||
stackTracer
|
||||
code abci.CodeType
|
||||
msg string
|
||||
}
|
||||
|
||||
var (
|
||||
_ causer = tmerror{}
|
||||
_ error = tmerror{}
|
||||
)
|
||||
|
||||
func (t tmerror) ErrorCode() abci.CodeType {
|
||||
return t.code
|
||||
}
|
||||
|
||||
func (t tmerror) Message() string {
|
||||
return t.msg
|
||||
}
|
||||
|
||||
func (t tmerror) Cause() error {
|
||||
if c, ok := t.stackTracer.(causer); ok {
|
||||
return c.Cause()
|
||||
}
|
||||
return t.stackTracer
|
||||
}
|
||||
|
||||
// Format handles "%+v" to expose the full stack trace
|
||||
// concept from pkg/errors
|
||||
func (t tmerror) Format(s fmt.State, verb rune) {
|
||||
// special case also show all info
|
||||
if verb == 'v' && s.Flag('+') {
|
||||
fmt.Fprintf(s, "%+v\n", t.stackTracer)
|
||||
}
|
||||
// always print the normal error
|
||||
fmt.Fprintf(s, "(%d) %s\n", t.code, t.msg)
|
||||
}
|
||||
|
||||
// Result converts any error into a abci.Result, preserving as much info
|
||||
// as possible if it was already a TMError
|
||||
func Result(err error) abci.Result {
|
||||
tm := Wrap(err)
|
||||
return abci.Result{
|
||||
Code: tm.ErrorCode(),
|
||||
Log: tm.Message(),
|
||||
}
|
||||
}
|
||||
|
||||
// Wrap safely takes any error and promotes it to a TMError
|
||||
func Wrap(err error) TMError {
|
||||
// nil or TMError are no-ops
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
// and check for noop
|
||||
tm, ok := err.(TMError)
|
||||
if ok {
|
||||
return tm
|
||||
}
|
||||
|
||||
return WithCode(err, defaultErrCode)
|
||||
}
|
||||
|
||||
// WithCode adds a stacktrace if necessary and sets the code and msg,
|
||||
// overriding the state if err was already TMError
|
||||
func WithCode(err error, code abci.CodeType) TMError {
|
||||
// add a stack only if not present
|
||||
st, ok := err.(stackTracer)
|
||||
if !ok {
|
||||
st = errors.WithStack(err).(stackTracer)
|
||||
}
|
||||
// and then wrap it with TMError info
|
||||
return tmerror{
|
||||
stackTracer: st,
|
||||
code: code,
|
||||
msg: err.Error(),
|
||||
}
|
||||
}
|
||||
|
||||
// WithMessage prepends some text to the error, then calls WithCode
|
||||
// It wraps the original error, so IsSameError will still match on err
|
||||
func WithMessage(prefix string, err error, code abci.CodeType) TMError {
|
||||
e2 := errors.WithMessage(err, prefix)
|
||||
return WithCode(e2, code)
|
||||
}
|
||||
|
||||
// New adds a stacktrace if necessary and sets the code and msg,
|
||||
// overriding the state if err was already TMError
|
||||
func New(msg string, code abci.CodeType) TMError {
|
||||
// create a new error with stack trace and attach a code
|
||||
st := errors.New(msg).(stackTracer)
|
||||
return tmerror{
|
||||
stackTracer: st,
|
||||
code: code,
|
||||
msg: msg,
|
||||
}
|
||||
}
|
||||
|
||||
// IsSameError returns true if these errors have the same root cause.
|
||||
// pattern is the expected error type and should always be non-nil
|
||||
// err may be anything and returns true if it is a wrapped version of pattern
|
||||
func IsSameError(pattern error, err error) bool {
|
||||
return err != nil && (errors.Cause(err) == errors.Cause(pattern))
|
||||
}
|
||||
|
||||
// HasErrorCode checks if this error would return the named error code
|
||||
func HasErrorCode(err error, code abci.CodeType) bool {
|
||||
if tm, ok := err.(TMError); ok {
|
||||
return tm.ErrorCode() == code
|
||||
}
|
||||
return code == defaultErrCode
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
package errors
|
||||
|
||||
import (
|
||||
stderr "errors"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
pkerr "github.com/pkg/errors"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
abci "github.com/tendermint/abci/types"
|
||||
)
|
||||
|
||||
func TestCreateResult(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
cases := []struct {
|
||||
err error
|
||||
msg string
|
||||
code abci.CodeType
|
||||
}{
|
||||
{stderr.New("base"), "base", defaultErrCode},
|
||||
{pkerr.New("dave"), "dave", defaultErrCode},
|
||||
{New("nonce", abci.CodeType_BadNonce), "nonce", abci.CodeType_BadNonce},
|
||||
{Wrap(stderr.New("wrap")), "wrap", defaultErrCode},
|
||||
{WithCode(stderr.New("coded"), abci.CodeType_BaseInvalidInput), "coded", abci.CodeType_BaseInvalidInput},
|
||||
{ErrDecoding(), errDecoding.Error(), abci.CodeType_EncodingError},
|
||||
{ErrUnauthorized(), errUnauthorized.Error(), abci.CodeType_Unauthorized},
|
||||
}
|
||||
|
||||
for idx, tc := range cases {
|
||||
i := strconv.Itoa(idx)
|
||||
|
||||
res := Result(tc.err)
|
||||
assert.True(res.IsErr(), i)
|
||||
assert.Equal(tc.msg, res.Log, i)
|
||||
assert.Equal(tc.code, res.Code, i)
|
||||
}
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"github.com/spf13/pflag"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk"
|
||||
txcmd "github.com/cosmos/cosmos-sdk/client/commands/txs"
|
||||
"github.com/cosmos/cosmos-sdk/modules/auth"
|
||||
)
|
||||
|
||||
//nolint
|
||||
const (
|
||||
FlagMulti = "multi"
|
||||
)
|
||||
|
||||
// SigWrapper wraps a tx with a signature layer to hold pubkey sigs
|
||||
type SigWrapper struct{}
|
||||
|
||||
var _ txcmd.Wrapper = SigWrapper{}
|
||||
|
||||
// Wrap will wrap the tx with OneSig or MultiSig depending on flags
|
||||
func (SigWrapper) Wrap(tx sdk.Tx) (res sdk.Tx, err error) {
|
||||
if !viper.GetBool(FlagMulti) {
|
||||
res = auth.NewSig(tx).Wrap()
|
||||
} else {
|
||||
res = auth.NewMulti(tx).Wrap()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Register adds the sequence flags to the cli
|
||||
func (SigWrapper) Register(fs *pflag.FlagSet) {
|
||||
fs.Bool(FlagMulti, false, "Prepare the tx for multisig")
|
||||
}
|
Loading…
Reference in New Issue