Skip to content

Commit

Permalink
Merge pull request #478 from sttts/sttts-multi-error
Browse files Browse the repository at this point in the history
  • Loading branch information
phisco authored Jul 14, 2023
2 parents 27a547d + 1fadab9 commit 5246a84
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions pkg/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package errors
import (
"errors"
"fmt"

kerrors "k8s.io/apimachinery/pkg/util/errors"
)

// New returns an error that formats as the given text. Each call to New returns
Expand Down Expand Up @@ -124,3 +126,37 @@ func Cause(err error) error {

return err
}

// MultiError is an error that wraps multiple errors.
type MultiError interface {
error
Unwrap() []error
}

// Join returns an error that wraps the given errors. Any nil error values are
// discarded. Join returns nil if errs contains no non-nil values. The error
// formats as the concatenation of the strings obtained by calling the Error
// method of each element of errs and formatting like:
//
// [first error, second error, third error]
//
// Note: aggregating errors should not be the default. Usually, return only the
// first error, and only aggregate if there is clear value to the user.
func Join(errs ...error) MultiError {
err := kerrors.NewAggregate(errs)
if err == nil {
return nil
}
return multiError{aggregate: err}
}

type multiError struct {
aggregate kerrors.Aggregate
}

func (m multiError) Error() string {
return m.aggregate.Error()
}
func (m multiError) Unwrap() []error {
return m.aggregate.Errors()
}

0 comments on commit 5246a84

Please sign in to comment.