2014-07-03 19:14:17 +02:00
|
|
|
package multierror
|
2014-07-03 06:00:06 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-07-03 19:14:17 +02:00
|
|
|
// Error is an error type to track multiple errors. This is used to
|
2014-07-03 06:00:06 +02:00
|
|
|
// accumulate errors in cases such as configuration parsing, and returning
|
|
|
|
// them as a single error.
|
2014-07-03 19:14:17 +02:00
|
|
|
type Error struct {
|
2014-07-03 06:00:06 +02:00
|
|
|
Errors []error
|
|
|
|
}
|
|
|
|
|
2014-07-03 19:14:17 +02:00
|
|
|
func (e *Error) Error() string {
|
2014-07-03 06:00:06 +02:00
|
|
|
points := make([]string, len(e.Errors))
|
|
|
|
for i, err := range e.Errors {
|
|
|
|
points[i] = fmt.Sprintf("* %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"%d error(s) occurred:\n\n%s",
|
|
|
|
len(e.Errors), strings.Join(points, "\n"))
|
|
|
|
}
|
|
|
|
|
2014-07-03 19:14:17 +02:00
|
|
|
// ErrorAppend is a helper function that will append more errors
|
|
|
|
// onto a Error in order to create a larger multi-error. If the
|
|
|
|
// original error is not a Error, it will be turned into one.
|
|
|
|
func ErrorAppend(err error, errs ...error) *Error {
|
2014-07-03 06:00:06 +02:00
|
|
|
if err == nil {
|
2014-07-03 19:14:17 +02:00
|
|
|
err = new(Error)
|
2014-07-03 06:00:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch err := err.(type) {
|
2014-07-03 19:14:17 +02:00
|
|
|
case *Error:
|
2014-07-03 06:00:06 +02:00
|
|
|
if err == nil {
|
2014-07-03 19:14:17 +02:00
|
|
|
err = new(Error)
|
2014-07-03 06:00:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err.Errors = append(err.Errors, errs...)
|
|
|
|
return err
|
|
|
|
default:
|
|
|
|
newErrs := make([]error, len(errs)+1)
|
|
|
|
newErrs[0] = err
|
|
|
|
copy(newErrs[1:], errs)
|
2014-07-03 19:14:17 +02:00
|
|
|
return &Error{
|
2014-07-03 06:00:06 +02:00
|
|
|
Errors: newErrs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|