-
Notifications
You must be signed in to change notification settings - Fork 19
/
errors.go
45 lines (37 loc) · 1.4 KB
/
errors.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
package testutils
import (
"fmt"
gomegatypes "github.com/onsi/gomega/types"
"github.com/rotisserie/eris"
)
// Relies on eris (https://github.com/rotisserie/eris)'s concept of error identity, specifically:
//
// "eris.Is returns true if a particular error appears anywhere in the error chain...
// eris.Is works simply by comparing error messages with each other. If an error contains a
// particular message anywhere in its chain (e.g. "not found"), it's defined to be that error
// type (i.e. eris.Is will return true)."
//
// Example usage:
// Expect(wrapperError1).To(HaveInErrorChain(baseError), "Chaining should work")
func HaveInErrorChain(err error) gomegatypes.GomegaMatcher {
return &errorChainMatcher{expected: err}
}
type errorChainMatcher struct {
expected error
}
func (e *errorChainMatcher) Match(actual interface{}) (success bool, err error) {
if actual == nil {
return false, nil
}
actualError, ok := actual.(error)
if !ok {
return false, eris.New("could not convert actual value to error type")
}
return eris.Is(actualError, e.expected), nil
}
func (e *errorChainMatcher) FailureMessage(actual interface{}) (message string) {
return fmt.Sprintf("Received actual: %+v, but expected error %+v", actual, e.expected)
}
func (e *errorChainMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return fmt.Sprintf("Expected actual: %+v not to equal error +%v", actual, e.expected)
}