64de635626
As we have a hard time figuring out what moby/moby#46099 should look like, this drop-in replacement will solve the initial formatting problem we have. It's made internal such that we can remove it whenever we want and unlike moby/moby#46099 doesn't require thoughtful API changes. Signed-off-by: Albin Kerouanton <albinker@gmail.com> Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
46 lines
818 B
Go
46 lines
818 B
Go
package multierror
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
// Join is a drop-in replacement for errors.Join with better formatting.
|
|
func Join(errs ...error) error {
|
|
n := 0
|
|
for _, err := range errs {
|
|
if err != nil {
|
|
n++
|
|
}
|
|
}
|
|
if n == 0 {
|
|
return nil
|
|
}
|
|
e := &joinError{
|
|
errs: make([]error, 0, n),
|
|
}
|
|
for _, err := range errs {
|
|
if err != nil {
|
|
e.errs = append(e.errs, err)
|
|
}
|
|
}
|
|
return e
|
|
}
|
|
|
|
type joinError struct {
|
|
errs []error
|
|
}
|
|
|
|
func (e *joinError) Error() string {
|
|
if len(e.errs) == 1 {
|
|
return strings.TrimSpace(e.errs[0].Error())
|
|
}
|
|
stringErrs := make([]string, 0, len(e.errs))
|
|
for _, subErr := range e.errs {
|
|
stringErrs = append(stringErrs, strings.Replace(subErr.Error(), "\n", "\n\t", -1))
|
|
}
|
|
return "* " + strings.Join(stringErrs, "\n* ")
|
|
}
|
|
|
|
func (e *joinError) Unwrap() []error {
|
|
return e.errs
|
|
}
|