1package mapstructure 2 3import ( 4 "errors" 5 "fmt" 6 "sort" 7 "strings" 8) 9 10// Error implements the error interface and can represents multiple 11// errors that occur in the course of a single decode. 12type Error struct { 13 Errors []string 14} 15 16func (e *Error) Error() string { 17 points := make([]string, len(e.Errors)) 18 for i, err := range e.Errors { 19 points[i] = fmt.Sprintf("* %s", err) 20 } 21 22 sort.Strings(points) 23 return fmt.Sprintf( 24 "%d error(s) decoding:\n\n%s", 25 len(e.Errors), strings.Join(points, "\n")) 26} 27 28// WrappedErrors implements the errwrap.Wrapper interface to make this 29// return value more useful with the errwrap and go-multierror libraries. 30func (e *Error) WrappedErrors() []error { 31 if e == nil { 32 return nil 33 } 34 35 result := make([]error, len(e.Errors)) 36 for i, e := range e.Errors { 37 result[i] = errors.New(e) 38 } 39 40 return result 41} 42 43func appendErrors(errors []string, err error) []string { 44 switch e := err.(type) { 45 case *Error: 46 return append(errors, e.Errors...) 47 default: 48 return append(errors, e.Error()) 49 } 50} 51