-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
54 lines (46 loc) · 1.43 KB
/
errors_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package base
import (
stderrors "errors"
"github.com/pkg/errors"
"testing"
)
var (
ErrCategory = stderrors.New("category")
)
func TestErrorCategories(t *testing.T) {
if !HasErrorCategory(ErrCategory, ErrCategory) {
t.Errorf("HasErrorCategory(x, x) unexpectedly returned false")
}
if cause := UnwrapCauseFromErrorCategory(ErrCategory, ErrCategory); nil != cause {
t.Errorf(
"mismatched UnwrapCauseFromErrorCategory(x, x) "+
"expected nil, got %s",
cause,
)
}
rootCauseError := errors.New("foo")
categorizedError := ErrorWithCategory(ErrCategory, rootCauseError)
if !HasErrorCategory(categorizedError, ErrCategory) {
t.Errorf("HasErrorCategory(ErrorWithCategory(x, y), x) unexpectedly returned false")
}
if cause := UnwrapCauseFromErrorCategory(categorizedError, ErrCategory); rootCauseError != cause {
t.Errorf(
"mismatched UnwrapCauseFromErrorCategory(ErrorWithCategory(x, y), x) "+
"expected %s, got %s",
rootCauseError,
cause,
)
}
wrappedError := errors.Wrap(categorizedError, "bar")
if !HasErrorCategory(wrappedError, ErrCategory) {
t.Errorf("HasErrorCategory(errors.Wrap(ErrorWithCategory(x, y), z), x) unexpectedly returned false")
}
if cause := UnwrapCauseFromErrorCategory(wrappedError, ErrCategory); rootCauseError != cause {
t.Errorf(
"mismatched UnwrapCauseFromErrorCategory(errors.Wrap(ErrorWithCategory(x, y), z), x) "+
"expected %s, got %s",
rootCauseError,
cause,
)
}
}