forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/mitchellh/mapstructure/error.go

77 lines
1.0 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package mapstructure
import (
"errors"
"fmt"
"sort"
"strings"
)
// Error implements the error interface and can represents multiple
2024-02-18 10:42:21 +00:00
// errors that occur in the course of a single decode.
2024-02-18 10:42:21 +00:00
type Error struct {
Errors []string
}
func (e *Error) Error() string {
2024-02-18 10:42:21 +00:00
points := make([]string, len(e.Errors))
2024-02-18 10:42:21 +00:00
for i, err := range e.Errors {
2024-02-18 10:42:21 +00:00
points[i] = fmt.Sprintf("* %s", err)
2024-02-18 10:42:21 +00:00
}
sort.Strings(points)
2024-02-18 10:42:21 +00:00
return fmt.Sprintf(
2024-02-18 10:42:21 +00:00
"%d error(s) decoding:\n\n%s",
2024-02-18 10:42:21 +00:00
len(e.Errors), strings.Join(points, "\n"))
2024-02-18 10:42:21 +00:00
}
// WrappedErrors implements the errwrap.Wrapper interface to make this
2024-02-18 10:42:21 +00:00
// return value more useful with the errwrap and go-multierror libraries.
2024-02-18 10:42:21 +00:00
func (e *Error) WrappedErrors() []error {
2024-02-18 10:42:21 +00:00
if e == nil {
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
result := make([]error, len(e.Errors))
2024-02-18 10:42:21 +00:00
for i, e := range e.Errors {
2024-02-18 10:42:21 +00:00
result[i] = errors.New(e)
2024-02-18 10:42:21 +00:00
}
return result
2024-02-18 10:42:21 +00:00
}
func appendErrors(errors []string, err error) []string {
2024-02-18 10:42:21 +00:00
switch e := err.(type) {
2024-02-18 10:42:21 +00:00
case *Error:
2024-02-18 10:42:21 +00:00
return append(errors, e.Errors...)
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return append(errors, e.Error())
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}