1package matchers
2
3import (
4	"fmt"
5	"reflect"
6
7	"github.com/onsi/gomega/format"
8)
9
10type MatchErrorMatcher struct {
11	Expected interface{}
12}
13
14func (matcher *MatchErrorMatcher) Match(actual interface{}) (success bool, err error) {
15	if isNil(actual) {
16		return false, fmt.Errorf("Expected an error, got nil")
17	}
18
19	if !isError(actual) {
20		return false, fmt.Errorf("Expected an error.  Got:\n%s", format.Object(actual, 1))
21	}
22
23	actualErr := actual.(error)
24
25	if isError(matcher.Expected) {
26		return reflect.DeepEqual(actualErr, matcher.Expected), nil
27	}
28
29	if isString(matcher.Expected) {
30		return actualErr.Error() == matcher.Expected, nil
31	}
32
33	var subMatcher omegaMatcher
34	var hasSubMatcher bool
35	if matcher.Expected != nil {
36		subMatcher, hasSubMatcher = (matcher.Expected).(omegaMatcher)
37		if hasSubMatcher {
38			return subMatcher.Match(actualErr.Error())
39		}
40	}
41
42	return false, fmt.Errorf("MatchError must be passed an error, string, or Matcher that can match on strings.  Got:\n%s", format.Object(matcher.Expected, 1))
43}
44
45func (matcher *MatchErrorMatcher) FailureMessage(actual interface{}) (message string) {
46	return format.Message(actual, "to match error", matcher.Expected)
47}
48
49func (matcher *MatchErrorMatcher) NegatedFailureMessage(actual interface{}) (message string) {
50	return format.Message(actual, "not to match error", matcher.Expected)
51}
52