github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/helper/multierror/error.go (about) 1 package multierror 2 3 import ( 4 "fmt" 5 "strings" 6 ) 7 8 // Error is an error type to track multiple errors. This is used to 9 // accumulate errors in cases such as configuration parsing, and returning 10 // them as a single error. 11 type Error struct { 12 Errors []error 13 } 14 15 func (e *Error) Error() string { 16 points := make([]string, len(e.Errors)) 17 for i, err := range e.Errors { 18 points[i] = fmt.Sprintf("* %s", err) 19 } 20 21 return fmt.Sprintf( 22 "%d error(s) occurred:\n\n%s", 23 len(e.Errors), strings.Join(points, "\n")) 24 } 25 26 func (e *Error) GoString() string { 27 return fmt.Sprintf("*%#v", *e) 28 } 29 30 // ErrorAppend is a helper function that will append more errors 31 // onto an Error in order to create a larger multi-error. If the 32 // original error is not an Error, it will be turned into one. 33 func ErrorAppend(err error, errs ...error) *Error { 34 if err == nil { 35 err = new(Error) 36 } 37 38 switch err := err.(type) { 39 case *Error: 40 if err == nil { 41 err = new(Error) 42 } 43 44 err.Errors = append(err.Errors, errs...) 45 return err 46 default: 47 newErrs := make([]error, len(errs)+1) 48 newErrs[0] = err 49 copy(newErrs[1:], errs) 50 return &Error{ 51 Errors: newErrs, 52 } 53 } 54 }