initial commit
[govpp.git] / vendor / github.com / onsi / gomega / gstruct / errors / nested_types.go
1 package errors
2
3 import (
4         "fmt"
5         "strings"
6
7         "github.com/onsi/gomega/types"
8 )
9
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 {
13         types.GomegaMatcher
14
15         // Returns the failures of nested matchers.
16         Failures() []error
17 }
18
19 // An error type for labeling errors on deeply nested matchers.
20 type NestedError struct {
21         Path string
22         Err  error
23 }
24
25 func (e *NestedError) Error() string {
26         // Indent Errors.
27         indented := strings.Replace(e.Err.Error(), "\n", "\n\t", -1)
28         return fmt.Sprintf("%s:\n\t%v", e.Path, indented)
29 }
30
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))
39                 }
40                 return errs
41         }
42         if ne, ok := err.(*NestedError); ok {
43                 return &NestedError{
44                         Path: path + ne.Path,
45                         Err:  ne.Err,
46                 }
47         }
48         return &NestedError{
49                 Path: path,
50                 Err:  err,
51         }
52 }
53
54 // An error type for treating multiple errors as a single error.
55 type AggregateError []error
56
57 // Error is part of the error interface.
58 func (err AggregateError) Error() string {
59         if len(err) == 0 {
60                 // This should never happen, really.
61                 return ""
62         }
63         if len(err) == 1 {
64                 return err[0].Error()
65         }
66         result := fmt.Sprintf("[%s", err[0].Error())
67         for i := 1; i < len(err); i++ {
68                 result += fmt.Sprintf(", %s", err[i].Error())
69         }
70         result += "]"
71         return result
72 }