-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmatch_wrapped_error_matcher.go
More file actions
79 lines (61 loc) · 1.93 KB
/
match_wrapped_error_matcher.go
File metadata and controls
79 lines (61 loc) · 1.93 KB
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
package gerrors
import (
"github.com/onsi/gomega/types"
"reflect"
"github.com/onsi/gomega/format"
"github.com/onsi/gomega/matchers"
)
func MatchWrappedError(expected interface{}) types.GomegaMatcher {
return &MatchWrappedErrorMatcher{
Expected: expected,
}
}
type MatchWrappedErrorMatcher struct {
Expected interface{}
}
type Causer interface {
Cause() error
}
func (matcher *MatchWrappedErrorMatcher) Match(actual interface{}) (bool, error) {
success, err := matchError(matcher.Expected, actual)
// Matched or it's an incompatible use of the matcher
if success || err != nil {
return success, err
}
// Did not match, but it's a correct usage of the matcher
// Time to check to see if it's actually a valid wrapped error
actualErr, aok := actual.(error)
expectedErr, eok := matcher.Expected.(error)
// One of them is not actually an error, default to what MatchError says
if !aok || !eok {
return success, err
}
_, expectedCauserOk := matcher.Expected.(Causer)
_, actualCauserOK := actual.(Causer)
// XOR, one of them is a wrapped error and the other isn't
if expectedCauserOk != actualCauserOK {
underlyingExpected := unwindError(expectedErr)
underlyingActual := unwindError(actualErr)
return matchError(underlyingExpected, underlyingActual)
}
// Both wrapped errors
return reflect.DeepEqual(matcher.Expected, actual), nil
}
func (matcher *MatchWrappedErrorMatcher) FailureMessage(actual interface{}) string {
return format.Message(actual, "to match wrapped error", matcher.Expected)
}
func (matcher *MatchWrappedErrorMatcher) NegatedFailureMessage(actual interface{}) string {
return format.Message(actual, "not to match wrapped error", matcher.Expected)
}
func matchError(expected, actual interface{}) (bool, error) {
return (&matchers.MatchErrorMatcher{
Expected: expected,
}).Match(actual)
}
func unwindError(e error) error {
causer, ok := e.(Causer)
if !ok {
return e
}
return unwindError(causer.Cause())
}