Skip to content

Commit

Permalink
Fix xerror is category (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
w41ter authored Dec 19, 2024
1 parent e22cc80 commit 1810e50
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
11 changes: 8 additions & 3 deletions pkg/xerror/xerror.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package xerror

import (
"errors"
stderrors "errors"
"fmt"
)
Expand Down Expand Up @@ -237,9 +238,13 @@ func IsCategory(err error, category ErrorCategory) bool {
return false
}

if xerr, ok := err.(*XError); ok {
return xerr.category == category
var xerr *XError
if !errors.As(err, &xerr) {
return false
}
if xerr.IsPanic() {
return false
}

return false
return xerr.Category() == category
}
17 changes: 17 additions & 0 deletions pkg/xerror/xerror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,20 @@ func TestPanicf(t *testing.T) {
assert.Equal(t, xerr.Category(), Normal)
assert.Equal(t, xerr.err.Error(), errMsg)
}

func TestIsCategory(t *testing.T) {
errMsg := "test error"
err := Errorf(Normal, errMsg)
assert.NotNil(t, err)

assert.True(t, IsCategory(err, Normal))
assert.False(t, IsCategory(err, DB))

err = Wrap(err, Meta, "wrapped error")
assert.True(t, IsCategory(err, Meta))
assert.False(t, IsCategory(err, Normal))

err = fmt.Errorf("invalid error")
assert.False(t, IsCategory(err, Meta))
assert.False(t, IsCategory(err, Normal))
}

0 comments on commit 1810e50

Please sign in to comment.