]>
Commit | Line | Data |
---|---|---|
bae9f6d2 JC |
1 | package multierror |
2 | ||
3 | import ( | |
4 | "fmt" | |
5 | ) | |
6 | ||
7 | // Error is an error type to track multiple errors. This is used to | |
8 | // accumulate errors in cases and return them as a single "error". | |
9 | type Error struct { | |
10 | Errors []error | |
11 | ErrorFormat ErrorFormatFunc | |
12 | } | |
13 | ||
14 | func (e *Error) Error() string { | |
15 | fn := e.ErrorFormat | |
16 | if fn == nil { | |
17 | fn = ListFormatFunc | |
18 | } | |
19 | ||
20 | return fn(e.Errors) | |
21 | } | |
22 | ||
23 | // ErrorOrNil returns an error interface if this Error represents | |
24 | // a list of errors, or returns nil if the list of errors is empty. This | |
25 | // function is useful at the end of accumulation to make sure that the value | |
26 | // returned represents the existence of errors. | |
27 | func (e *Error) ErrorOrNil() error { | |
28 | if e == nil { | |
29 | return nil | |
30 | } | |
31 | if len(e.Errors) == 0 { | |
32 | return nil | |
33 | } | |
34 | ||
35 | return e | |
36 | } | |
37 | ||
38 | func (e *Error) GoString() string { | |
39 | return fmt.Sprintf("*%#v", *e) | |
40 | } | |
41 | ||
42 | // WrappedErrors returns the list of errors that this Error is wrapping. | |
43 | // It is an implementatin of the errwrap.Wrapper interface so that | |
44 | // multierror.Error can be used with that library. | |
45 | // | |
46 | // This method is not safe to be called concurrently and is no different | |
47 | // than accessing the Errors field directly. It is implementd only to | |
48 | // satisfy the errwrap.Wrapper interface. | |
49 | func (e *Error) WrappedErrors() []error { | |
50 | return e.Errors | |
51 | } |