-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
83 lines (69 loc) · 1.95 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package events_test
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"golang.org/x/xerrors"
. "github.com/bancek/events/v2"
)
type pointerError struct {
nonComparable []byte
}
func (e pointerError) Error() string {
return "pointer error"
}
type nonPointerError struct {
nonComparable []byte
}
func (e nonPointerError) Error() string {
return "non pointer error"
}
var _ = Describe("Errors", func() {
Describe("UnwrapAll", func() {
It("should return the same error", func() {
err := nonPointerError{}
unwrapped := UnwrapAll(err)
Expect(unwrapped).To(Equal(err))
})
It("should unwrap a pointer error", func() {
err := &pointerError{}
wrapped := xerrors.Errorf("wrap 2: %w", xerrors.Errorf("wrap 1: %w", err))
unwrapped := UnwrapAll(wrapped)
Expect(unwrapped).To(Equal(err))
})
It("should unwrap a non-pointer error", func() {
err := nonPointerError{}
wrapped := xerrors.Errorf("wrap 2: %w", xerrors.Errorf("wrap 1: %w", err))
unwrapped := UnwrapAll(wrapped)
Expect(unwrapped).To(Equal(err))
})
It("should return nil", func() {
unwrapped := UnwrapAll(nil)
Expect(unwrapped).To(BeNil())
})
})
Describe("GetCause", func() {
It("should return false if the error is not wrapped", func() {
err := nonPointerError{}
_, ok := GetCause(err)
Expect(ok).To(BeFalse())
})
It("should unwrap a pointer error", func() {
err := &pointerError{}
wrapped := xerrors.Errorf("wrap 2: %w", xerrors.Errorf("wrap 1: %w", err))
cause, ok := GetCause(wrapped)
Expect(ok).To(BeTrue())
Expect(cause).To(Equal(err))
})
It("should unwrap a non-pointer error", func() {
err := nonPointerError{}
wrapped := xerrors.Errorf("wrap 2: %w", xerrors.Errorf("wrap 1: %w", err))
cause, ok := GetCause(wrapped)
Expect(ok).To(BeTrue())
Expect(cause).To(Equal(err))
})
It("should return false if for nil error", func() {
_, ok := GetCause(nil)
Expect(ok).To(BeFalse())
})
})
})