Complete error package overhaul

This commit is contained in:
Ethan Frey
2017-07-03 14:50:33 +02:00
parent 5fa77bf647
commit 995452ea02
16 changed files with 184 additions and 107 deletions
+2 -2
View File
@@ -42,10 +42,10 @@ func (c Chain) DeliverTx(ctx basecoin.Context, store types.KVStore, tx basecoin.
func (c Chain) checkChain(tx basecoin.Tx) (basecoin.Tx, error) {
ctx, ok := tx.Unwrap().(*txs.Chain)
if !ok {
return tx, errors.NoChain()
return tx, errors.ErrNoChain()
}
if ctx.ChainID != c.ChainID {
return tx, errors.WrongChain(ctx.ChainID)
return tx, errors.ErrWrongChain(ctx.ChainID)
}
return ctx.Tx, nil
}
+3 -3
View File
@@ -25,7 +25,7 @@ func TestChain(t *testing.T) {
errorMsg string
}{
{txs.NewChain(chainID, raw).Wrap(), true, ""},
{txs.NewChain("someone-else", raw).Wrap(), false, "Tx belongs to different chain - someone-else"},
{txs.NewChain("someone-else", raw).Wrap(), false, "someone-else"},
{raw, false, "No chain id provided"},
}
@@ -47,7 +47,7 @@ func TestChain(t *testing.T) {
assert.Equal(msg, res.Log, i)
} else {
if assert.NotNil(err, i) {
assert.Equal(tc.errorMsg, err.Error(), i)
assert.Contains(err.Error(), tc.errorMsg, i)
}
}
@@ -58,7 +58,7 @@ func TestChain(t *testing.T) {
assert.Equal(msg, res.Log, i)
} else {
if assert.NotNil(err, i) {
assert.Equal(tc.errorMsg, err.Error(), i)
assert.Contains(err.Error(), tc.errorMsg, i)
}
}
}
+2 -2
View File
@@ -25,14 +25,14 @@ func (_ CheckMiddleware) Name() string {
func (p CheckMiddleware) CheckTx(ctx basecoin.Context, store types.KVStore, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) {
if !ctx.HasPermission(p.Required) {
return res, errors.Unauthorized()
return res, errors.ErrUnauthorized()
}
return next.CheckTx(ctx, store, tx)
}
func (p CheckMiddleware) DeliverTx(ctx basecoin.Context, store types.KVStore, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) {
if !ctx.HasPermission(p.Required) {
return res, errors.Unauthorized()
return res, errors.ErrUnauthorized()
}
return next.DeliverTx(ctx, store, tx)
}
+9 -11
View File
@@ -31,12 +31,12 @@ func TestPermissionSandbox(t *testing.T) {
grant basecoin.Actor
require basecoin.Actor
expectedRes data.Bytes
expectedErr error
expected func(error) bool
}{
{grantee, grantee, rawBytes, nil},
{grantee, grantee2, nil, errors.Unauthorized()},
{grantee, signer, nil, errors.Unauthorized()},
{signer, signer, nil, errors.InternalError("panic")},
{grantee, grantee2, nil, errors.IsUnauthorizedErr},
{grantee, signer, nil, errors.IsUnauthorizedErr},
{signer, signer, nil, errors.IsInternalErr},
}
for i, tc := range cases {
@@ -47,24 +47,22 @@ func TestPermissionSandbox(t *testing.T) {
).Use(EchoHandler{})
res, err := app.CheckTx(ctx, store, raw)
checkPerm(t, i, tc.expectedRes, tc.expectedErr, res, err)
checkPerm(t, i, tc.expectedRes, tc.expected, res, err)
res, err = app.DeliverTx(ctx, store, raw)
checkPerm(t, i, tc.expectedRes, tc.expectedErr, res, err)
checkPerm(t, i, tc.expectedRes, tc.expected, res, err)
}
}
func checkPerm(t *testing.T, idx int, data []byte, expected error, res basecoin.Result, err error) {
func checkPerm(t *testing.T, idx int, data []byte, check func(error) bool, res basecoin.Result, err error) {
assert := assert.New(t)
if expected == nil {
if len(data) > 0 {
assert.Nil(err, "%d: %+v", idx, err)
assert.EqualValues(data, res.Data)
} else {
assert.NotNil(err, "%d", idx)
// check error code!
shouldCode := errors.Wrap(expected).ErrorCode()
isCode := errors.Wrap(err).ErrorCode()
assert.Equal(shouldCode, isCode, "%d: %+v", idx, err)
assert.True(check(err), "%d: %+v", idx, err)
}
}
+1 -1
View File
@@ -45,5 +45,5 @@ func normalizePanic(p interface{}) error {
return errors.Wrap(err)
}
msg := fmt.Sprintf("%v", p)
return errors.InternalError(msg)
return errors.ErrInternal(msg)
}
+1 -1
View File
@@ -61,7 +61,7 @@ func addSigners(ctx basecoin.Context, sigs []crypto.PubKey) basecoin.Context {
func getSigners(tx basecoin.Tx) ([]crypto.PubKey, basecoin.Tx, error) {
stx, ok := tx.Unwrap().(Signed)
if !ok {
return nil, basecoin.Tx{}, errors.Unauthorized()
return nil, basecoin.Tx{}, errors.ErrUnauthorized()
}
sig, err := stx.Signers()
return sig, stx.Next(), err