multierror.go 799 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package multierror
  2. import "sync"
  3. // MultiError type handles error accumulation from goroutines.
  4. type MultiError struct {
  5. Errors []error
  6. mux sync.Mutex
  7. }
  8. // Error turns the MultiError structure into a string.
  9. func (err *MultiError) Error() string {
  10. str := ""
  11. for _, e := range err.Errors {
  12. str += e.Error() + "\n"
  13. }
  14. return str[:len(str)-1]
  15. }
  16. // Add adds an error to the Multierror structure.
  17. func (err *MultiError) Add(e error) {
  18. if e == nil {
  19. return
  20. }
  21. err.mux.Lock()
  22. err.Errors = append(err.Errors, e)
  23. err.mux.Unlock()
  24. }
  25. // Return is used as a wrapper on return on whether to return the
  26. // MultiError Structure if errors exist or nil instead of delivering an empty structure.
  27. func (err *MultiError) Return() error {
  28. if len(err.Errors) > 0 {
  29. return err
  30. }
  31. return nil
  32. }