2017-12-17 10:55:22 +08:00
|
|
|
package util
|
|
|
|
|
|
|
|
import "bytes"
|
|
|
|
|
|
|
|
// MultiError pack multiple errors into one error.
|
|
|
|
type MultiError struct {
|
|
|
|
Errors []error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es MultiError) Error() string {
|
|
|
|
switch len(es.Errors) {
|
|
|
|
case 0:
|
|
|
|
return "no error"
|
|
|
|
case 1:
|
|
|
|
return es.Errors[0].Error()
|
|
|
|
default:
|
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.WriteString("multiple errors: ")
|
|
|
|
for i, e := range es.Errors {
|
|
|
|
if i > 0 {
|
|
|
|
buf.WriteString("; ")
|
|
|
|
}
|
|
|
|
buf.WriteString(e.Error())
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Errors concatenate multiple errors into one. If all errors are nil, it
|
2018-03-01 23:13:00 +08:00
|
|
|
// returns nil. If there is one non-nil error, it is returned. Otherwise the
|
|
|
|
// return value is a MultiError containing all the non-nil arguments. Arguments
|
|
|
|
// of the type MultiError are flattened.
|
2017-12-17 10:55:22 +08:00
|
|
|
func Errors(errs ...error) error {
|
|
|
|
var nonNil []error
|
|
|
|
for _, err := range errs {
|
|
|
|
if err != nil {
|
2018-01-21 19:16:40 +08:00
|
|
|
if multi, ok := err.(MultiError); ok {
|
|
|
|
nonNil = append(nonNil, multi.Errors...)
|
|
|
|
} else {
|
|
|
|
nonNil = append(nonNil, err)
|
|
|
|
}
|
2017-12-17 10:55:22 +08:00
|
|
|
}
|
|
|
|
}
|
2018-03-01 23:13:00 +08:00
|
|
|
switch len(nonNil) {
|
|
|
|
case 0:
|
2017-12-17 10:55:22 +08:00
|
|
|
return nil
|
2018-03-01 23:13:00 +08:00
|
|
|
case 1:
|
|
|
|
return nonNil[0]
|
|
|
|
default:
|
|
|
|
return MultiError{nonNil}
|
2017-12-17 10:55:22 +08:00
|
|
|
}
|
|
|
|
}
|