Removed dependency of errors on basecoin main
This commit is contained in:
parent
6983f61961
commit
768427dcc0
|
@ -3,10 +3,10 @@ package errors
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
abci "github.com/tendermint/abci/types"
|
||||
"github.com/tendermint/basecoin"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -23,8 +23,21 @@ var (
|
|||
errUnknownModule = fmt.Errorf("Unknown module")
|
||||
)
|
||||
|
||||
func ErrUnknownTxType(tx basecoin.Tx) TMError {
|
||||
msg := fmt.Sprintf("%T", tx.Unwrap())
|
||||
// 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))
|
||||
w := errors.Wrap(errUnknownTxType, msg)
|
||||
return WithCode(w, abci.CodeType_UnknownRequest)
|
||||
}
|
||||
|
@ -32,8 +45,8 @@ func IsUnknownTxTypeErr(err error) bool {
|
|||
return IsSameError(errUnknownTxType, err)
|
||||
}
|
||||
|
||||
func ErrInvalidFormat(tx basecoin.Tx) TMError {
|
||||
msg := fmt.Sprintf("%T", tx.Unwrap())
|
||||
func ErrInvalidFormat(tx interface{}) TMError {
|
||||
msg := fmt.Sprintf("%T", unwrap(tx))
|
||||
w := errors.Wrap(errInvalidFormat, msg)
|
||||
return WithCode(w, abci.CodeType_UnknownRequest)
|
||||
}
|
||||
|
|
|
@ -5,15 +5,26 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/tendermint/basecoin"
|
||||
)
|
||||
|
||||
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() basecoin.Tx {
|
||||
return basecoin.Tx{t}
|
||||
func (t DemoTx) Wrap() holder {
|
||||
return holder{t}
|
||||
}
|
||||
|
||||
func (t DemoTx) ValidateBasic() error {
|
||||
|
@ -32,7 +43,8 @@ func TestErrorMatches(t *testing.T) {
|
|||
{errMissingSignature, ErrUnauthorized(), false},
|
||||
{errMissingSignature, ErrMissingSignature(), true},
|
||||
{errWrongChain, ErrWrongChain("hakz"), true},
|
||||
{errUnknownTxType, ErrUnknownTxType(basecoin.Tx{}), true},
|
||||
{errUnknownTxType, ErrUnknownTxType(holder{}), true},
|
||||
{errUnknownTxType, ErrUnknownTxType("some text here..."), true},
|
||||
{errUnknownTxType, ErrUnknownTxType(DemoTx{5}.Wrap()), true},
|
||||
}
|
||||
|
||||
|
|
8
tx.go
8
tx.go
|
@ -1,8 +1,9 @@
|
|||
package basecoin
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"github.com/tendermint/go-wire/data"
|
||||
|
||||
"github.com/tendermint/basecoin/errors"
|
||||
)
|
||||
|
||||
const maxTxSize = 10240
|
||||
|
@ -23,12 +24,9 @@ type TxInner interface {
|
|||
}
|
||||
|
||||
// LoadTx parses a tx from data
|
||||
//
|
||||
// TODO: label both errors with abci.CodeType_EncodingError
|
||||
// need to move errors to avoid import cycle
|
||||
func LoadTx(bin []byte) (tx Tx, err error) {
|
||||
if len(bin) > maxTxSize {
|
||||
return tx, errors.New("Tx size exceeds maximum")
|
||||
return tx, errors.ErrTooLarge()
|
||||
}
|
||||
|
||||
// Decode tx
|
||||
|
|
Loading…
Reference in New Issue