2020-10-08 18:28:21 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2020-10-09 12:00:33 +00:00
|
|
|
// Package multierror provides a type that can translate multiple errors into a Go error interface.
|
|
|
|
//
|
|
|
|
// A common use of this package is as follows.
|
|
|
|
// errs := multierror.New()
|
|
|
|
// for i := range stuff {
|
|
|
|
// err := doThing(i)
|
|
|
|
// errs.Add(err)
|
|
|
|
// }
|
|
|
|
// return errs.ErrOrNil()
|
2020-10-08 18:28:21 +00:00
|
|
|
package multierror
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-10-09 14:39:17 +00:00
|
|
|
// formatFunc is a function used to format the string representing of a MultiError. It is used in the
|
|
|
|
// Error() function.
|
|
|
|
//
|
|
|
|
// It is marked out here to indicate how we could potentially extend MultiError in the future to
|
|
|
|
// support more styles of converting from a list of error's to a string.
|
|
|
|
//nolint: gochecknoglobals
|
|
|
|
var formatFunc func(errs MultiError, sb *strings.Builder) = defaultFormat
|
|
|
|
|
2020-10-09 12:00:33 +00:00
|
|
|
// MultiError holds a list of error's, that could potentially be empty.
|
|
|
|
//
|
|
|
|
// Use New() to create a MultiError.
|
|
|
|
type MultiError []error
|
2020-10-08 18:28:21 +00:00
|
|
|
|
2020-10-09 12:00:33 +00:00
|
|
|
// New returns an empty MultiError.
|
|
|
|
func New() MultiError {
|
2020-10-08 18:28:21 +00:00
|
|
|
return make([]error, 0)
|
|
|
|
}
|
|
|
|
|
2020-10-09 12:00:33 +00:00
|
|
|
// Add adds an error to the MultiError. The provided err must not be nil.
|
|
|
|
func (m *MultiError) Add(err error) {
|
2020-10-08 18:28:21 +00:00
|
|
|
*m = append(*m, err)
|
|
|
|
}
|
|
|
|
|
2020-10-09 12:00:33 +00:00
|
|
|
// Error implements the error.Error() interface method.
|
|
|
|
func (m MultiError) Error() string {
|
2020-10-08 18:28:21 +00:00
|
|
|
sb := strings.Builder{}
|
2020-10-09 14:39:17 +00:00
|
|
|
formatFunc(m, &sb)
|
2020-10-08 18:28:21 +00:00
|
|
|
return sb.String()
|
|
|
|
}
|
|
|
|
|
2020-10-09 12:00:33 +00:00
|
|
|
// ErrOrNil returns either nil, if there are no errors in this MultiError, or an error, otherwise.
|
|
|
|
func (m MultiError) ErrOrNil() error {
|
|
|
|
if len(m) > 0 {
|
2020-10-08 18:28:21 +00:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-09 14:39:17 +00:00
|
|
|
|
|
|
|
func defaultFormat(errs MultiError, sb *strings.Builder) {
|
|
|
|
_, _ = fmt.Fprintf(sb, "%d error(s):", len(errs))
|
|
|
|
for _, err := range errs {
|
|
|
|
_, _ = fmt.Fprintf(sb, "\n- %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|