|
| 1 | +package multierror |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + |
| 6 | + "github.com/hashicorp/go-multierror" |
| 7 | +) |
| 8 | + |
| 9 | +// Error wraps a multierror.Error and defines a default |
| 10 | +// formatting function that fits cli needs |
| 11 | +type Error struct { |
| 12 | + err *multierror.Error |
| 13 | +} |
| 14 | + |
| 15 | +func (e *Error) Error() string { |
| 16 | + if e == nil || e.err == nil { |
| 17 | + return "" |
| 18 | + } |
| 19 | + e.err.ErrorFormat = listErrorFunc |
| 20 | + return e.err.Error() |
| 21 | +} |
| 22 | + |
| 23 | +// WrappedErrors returns the list of errors that this Error is wrapping. |
| 24 | +// It is an implementation of the errwrap.Wrapper interface so that |
| 25 | +// multierror.Error can be used with that library. |
| 26 | +// |
| 27 | +// This method is not safe to be called concurrently and is no different |
| 28 | +// than accessing the Errors field directly. It is implemented only to |
| 29 | +// satisfy the errwrap.Wrapper interface. |
| 30 | +func (e *Error) WrappedErrors() []error { |
| 31 | + return e.err.WrappedErrors() |
| 32 | +} |
| 33 | + |
| 34 | +// Unwrap returns an error from Error (or nil if there are no errors) |
| 35 | +func (e *Error) Unwrap() error { |
| 36 | + if e == nil || e.err == nil { |
| 37 | + return nil |
| 38 | + } |
| 39 | + return e.err.Unwrap() |
| 40 | +} |
| 41 | + |
| 42 | +// ErrorOrNil returns an error interface if this Error represents |
| 43 | +// a list of errors, or returns nil if the list of errors is empty. This |
| 44 | +// function is useful at the end of accumulation to make sure that the value |
| 45 | +// returned represents the existence of errors. |
| 46 | +func (e *Error) ErrorOrNil() error { |
| 47 | + if e == nil || e.err == nil { |
| 48 | + return nil |
| 49 | + } |
| 50 | + if len(e.err.Errors) == 0 { |
| 51 | + return nil |
| 52 | + } |
| 53 | + |
| 54 | + return e |
| 55 | +} |
| 56 | + |
| 57 | +// Append adds an error to a multierror, if err is |
| 58 | +// not a multierror it will be converted to one |
| 59 | +func Append(err error, errs ...error) *Error { |
| 60 | + switch err := err.(type) { |
| 61 | + case *Error: |
| 62 | + if err == nil { |
| 63 | + err = new(Error) |
| 64 | + } |
| 65 | + for _, e := range errs { |
| 66 | + err.err = multierror.Append(err.err, e) |
| 67 | + } |
| 68 | + return err |
| 69 | + default: |
| 70 | + newErrs := make([]error, 0, len(errs)+1) |
| 71 | + if err != nil { |
| 72 | + newErrs = append(newErrs, err) |
| 73 | + } |
| 74 | + newErrs = append(newErrs, errs...) |
| 75 | + |
| 76 | + return Append(&Error{}, newErrs...) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func listErrorFunc(errs []error) string { |
| 81 | + if len(errs) == 1 { |
| 82 | + return errs[0].Error() |
| 83 | + } |
| 84 | + |
| 85 | + messages := make([]string, len(errs)) |
| 86 | + |
| 87 | + for i, err := range errs { |
| 88 | + messages[i] = "Error: " + err.Error() |
| 89 | + } |
| 90 | + |
| 91 | + return strings.Join(messages, "\n") |
| 92 | +} |
0 commit comments