Skip to content

Commit

Permalink
Merge pull request #4 from go-pogo/develop
Browse files Browse the repository at this point in the history
v0.7.1
  • Loading branch information
roeldev authored Jan 30, 2022
2 parents ae22e3b + bd1169c commit 29d0a74
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
9 changes: 9 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,17 @@ func withPossibleCause(ce *commonError) *commonError {

func (ce *commonError) StackTrace() *StackTrace { return ce.stack }

// Unwrap returns the next error in the error chain. It returns nil if there
// is not a next error.
func (ce *commonError) Unwrap() error { return ce.cause }

func (ce *commonError) Is(target error) bool {
if m, ok := ce.error.(Msg); ok {
return m.Is(target)
}
return false
}

func (ce *commonError) As(target interface{}) bool {
if t, ok := target.(*commonError); ok {
*t = *ce
Expand Down
9 changes: 5 additions & 4 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,16 @@ func TestMsg(t *testing.T) {

func TestMsg_Is(t *testing.T) {
t.Run("true", func(t *testing.T) {
msg := Msg("some err")
m := Msg("some err")
tests := map[string]error{
"Msg": Msg("some err"),
"*Msg": &msg,
"*Msg": &m,
}
for a, err := range tests {
for a, msg := range tests {
for b, target := range tests {
t.Run(a+"/"+b, func(t *testing.T) {
assert.ErrorIs(t, err, target)
assert.ErrorIs(t, msg, target)
assert.ErrorIs(t, New(msg), target)
})
}
}
Expand Down
6 changes: 6 additions & 0 deletions kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ const UnknownKind Kind = ""
// It is recommended to define each Kind as a constant.
type Kind string

// Kindf formats according to a format specifier using fmt.Sprintf, and returns
// the resulting string as Kind.
func Kindf(format string, args ...interface{}) Kind {
return Kind(fmt.Sprintf(format, args...))
}

func (k Kind) Is(target error) bool {
switch t := target.(type) {
case Kind:
Expand Down
13 changes: 13 additions & 0 deletions kind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ func TestKind(t *testing.T) {
assert.Equal(t, kind.String(), kind.Error())
}

func TestKindf(t *testing.T) {
tests := map[string][]interface{}{
"no args": nil,
"some %s": {"string"},
"%s %s": {"foo", "bar"},
}
for f, a := range tests {
t.Run(f, func(t *testing.T) {
assert.Equal(t, Kind(fmt.Sprintf(f, a...)), Kindf(f, a...))
})
}
}

func TestKind_Is(t *testing.T) {
t.Run("true", func(t *testing.T) {
kind := Kind("foobar")
Expand Down

0 comments on commit 29d0a74

Please sign in to comment.