Add waddrmgr.IsError function.

This commit is contained in:
Josh Rickmar 2015-05-27 16:50:05 -04:00
parent 9ee887823d
commit 39cab6087a
2 changed files with 52 additions and 0 deletions

View file

@ -204,3 +204,10 @@ func managerError(c ErrorCode, desc string, err error) ManagerError {
// Break is a global err used to signal a break from the callback
// function by returning an error with the code ErrCallBackBreak
var Break = managerError(ErrCallBackBreak, "callback break", nil)
// IsError returns whether the error is a MangerError with a matching error
// code.
func IsError(err error, code ErrorCode) bool {
e, ok := err.(ManagerError)
return ok && e.ErrorCode == code
}

View file

@ -17,6 +17,7 @@
package waddrmgr_test
import (
"errors"
"fmt"
"testing"
@ -120,3 +121,47 @@ func TestManagerError(t *testing.T) {
}
}
}
// TestIsError tests the IsError func.
func TestIsError(t *testing.T) {
tests := []struct {
err error
code waddrmgr.ErrorCode
exp bool
}{
{
err: waddrmgr.ManagerError{
ErrorCode: waddrmgr.ErrDatabase,
},
code: waddrmgr.ErrDatabase,
exp: true,
},
{
// package should never return *ManagerError
err: &waddrmgr.ManagerError{
ErrorCode: waddrmgr.ErrDatabase,
},
code: waddrmgr.ErrDatabase,
exp: false,
},
{
err: waddrmgr.ManagerError{
ErrorCode: waddrmgr.ErrCrypto,
},
code: waddrmgr.ErrDatabase,
exp: false,
},
{
err: errors.New("not a ManagerError"),
code: waddrmgr.ErrDatabase,
exp: false,
},
}
for i, test := range tests {
got := waddrmgr.IsError(test.err, test.code)
if got != test.exp {
t.Errorf("Test %d: got %v expected %v", i, got, test.exp)
}
}
}