multi_error.go 663 B

123456789101112131415161718192021222324252627282930313233
  1. package common
  2. import (
  3. "strings"
  4. )
  5. // multiError represents a collection of errors.
  6. type multiError []error
  7. // Error returns a string representation of all errors joined with " | ".
  8. func (e multiError) Error() string {
  9. var r strings.Builder
  10. r.WriteString("multierr: ")
  11. for _, err := range e {
  12. r.WriteString(err.Error())
  13. r.WriteString(" | ")
  14. }
  15. return r.String()
  16. }
  17. // Combine combines multiple errors into a single error, filtering out nil errors.
  18. func Combine(maybeError ...error) error {
  19. var errs multiError
  20. for _, err := range maybeError {
  21. if err != nil {
  22. errs = append(errs, err)
  23. }
  24. }
  25. if len(errs) == 0 {
  26. return nil
  27. }
  28. return errs
  29. }