@@ -22,6 +22,8 @@ package errors
2222import (
2323 "errors"
2424 "fmt"
25+
26+ kerrors "k8s.io/apimachinery/pkg/util/errors"
2527)
2628
2729// New returns an error that formats as the given text. Each call to New returns
@@ -124,3 +126,37 @@ func Cause(err error) error {
124126
125127 return err
126128}
129+
130+ // MultiError is an error that wraps multiple errors.
131+ type MultiError interface {
132+ error
133+ Unwrap () []error
134+ }
135+
136+ // Join returns an error that wraps the given errors. Any nil error values are
137+ // discarded. Join returns nil if errs contains no non-nil values. The error
138+ // formats as the concatenation of the strings obtained by calling the Error
139+ // method of each element of errs and formatting like:
140+ //
141+ // [first error, second error, third error]
142+ //
143+ // Note: aggregating errors should not be the default. Usually, return only the
144+ // first error, and only aggregate if there is clear value to the user.
145+ func Join (errs ... error ) MultiError {
146+ err := kerrors .NewAggregate (errs )
147+ if err == nil {
148+ return nil
149+ }
150+ return multiError {aggregate : err }
151+ }
152+
153+ type multiError struct {
154+ aggregate kerrors.Aggregate
155+ }
156+
157+ func (m multiError ) Error () string {
158+ return m .aggregate .Error ()
159+ }
160+ func (m multiError ) Unwrap () []error {
161+ return m .aggregate .Errors ()
162+ }
0 commit comments