-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcause_test.go
50 lines (42 loc) · 1.07 KB
/
cause_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
package errorutil
import (
"errors"
"testing"
)
var _ error = (*errorWithCause)(nil)
// errorWithCause is simple custom error which includes causer
type errorWithCause struct {
error
*causer
}
func (e errorWithCause) Error() string {
return e.error.Error()
}
func (e errorWithCause) Is(err error) bool {
return errors.Is(e.error, err)
}
func TestNestedError(t *testing.T) {
errRoot := errors.New("this is root error")
errChild := errors.New("this is child error")
errGrandChild := errors.New("this is grand child error")
childErr := errorWithCause{
error: errChild,
causer: &causer{cause: errGrandChild},
}
rootErr := errorWithCause{
error: errRoot,
causer: &causer{cause: childErr},
}
// Ensure child has grandchild
if valid := errors.Is(childErr, errGrandChild); !valid {
t.Error("child don't have grandchild")
}
// Ensure root has child
if valid := errors.Is(rootErr, errChild); !valid {
t.Error("root don't have child")
}
// Ensure root has grandchild
if valid := errors.Is(rootErr, errGrandChild); !valid {
t.Error("root don't have grandchild")
}
}