terraform/helper/multierror/error.go

55 lines
1.1 KiB
Go
Raw Normal View History

2014-07-03 19:14:17 +02:00
package multierror
import (
"fmt"
"strings"
)
2014-07-03 19:14:17 +02:00
// Error is an error type to track multiple errors. This is used to
// 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 {
Errors []error
}
2014-07-03 19:14:17 +02:00
func (e *Error) Error() string {
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-22 17:27:16 +02:00
func (e *Error) GoString() string {
return fmt.Sprintf("*%#v", *e)
}
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 {
if err == nil {
2014-07-03 19:14:17 +02:00
err = new(Error)
}
switch err := err.(type) {
2014-07-03 19:14:17 +02:00
case *Error:
if err == nil {
2014-07-03 19:14:17 +02:00
err = new(Error)
}
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{
Errors: newErrs,
}
}
}