multi_error.go 457 B

123456789101112131415161718192021222324252627282930
  1. package common
  2. import (
  3. "strings"
  4. )
  5. type multiError []error
  6. func (e multiError) Error() string {
  7. var r strings.Builder
  8. r.WriteString("multierr: ")
  9. for _, err := range e {
  10. r.WriteString(err.Error())
  11. r.WriteString(" | ")
  12. }
  13. return r.String()
  14. }
  15. func Combine(maybeError ...error) error {
  16. var errs multiError
  17. for _, err := range maybeError {
  18. if err != nil {
  19. errs = append(errs, err)
  20. }
  21. }
  22. if len(errs) == 0 {
  23. return nil
  24. }
  25. return errs
  26. }