7 "github.com/onsi/gomega/types"
10 // A stateful matcher that nests other matchers within it and preserves the error types of the
11 // nested matcher failures.
12 type NestingMatcher interface {
15 // Returns the failures of nested matchers.
19 // An error type for labeling errors on deeply nested matchers.
20 type NestedError struct {
25 func (e *NestedError) Error() string {
27 indented := strings.Replace(e.Err.Error(), "\n", "\n\t", -1)
28 return fmt.Sprintf("%s:\n\t%v", e.Path, indented)
31 // Create a NestedError with the given path.
32 // If err is a NestedError, prepend the path to it.
33 // If err is an AggregateError, recursively Nest each error.
34 func Nest(path string, err error) error {
35 if ag, ok := err.(AggregateError); ok {
36 var errs AggregateError
37 for _, e := range ag {
38 errs = append(errs, Nest(path, e))
42 if ne, ok := err.(*NestedError); ok {
54 // An error type for treating multiple errors as a single error.
55 type AggregateError []error
57 // Error is part of the error interface.
58 func (err AggregateError) Error() string {
60 // This should never happen, really.
66 result := fmt.Sprintf("[%s", err[0].Error())
67 for i := 1; i < len(err); i++ {
68 result += fmt.Sprintf(", %s", err[i].Error())