mirror of
https://github.com/Jguer/yay.git
synced 2024-11-07 01:27:21 +01:00
42 lines
799 B
Go
42 lines
799 B
Go
package multierror
|
|
|
|
import "sync"
|
|
|
|
// MultiError type handles error accumulation from goroutines.
|
|
type MultiError struct {
|
|
Errors []error
|
|
mux sync.Mutex
|
|
}
|
|
|
|
// Error turns the MultiError structure into a string.
|
|
func (err *MultiError) Error() string {
|
|
str := ""
|
|
|
|
for _, e := range err.Errors {
|
|
str += e.Error() + "\n"
|
|
}
|
|
|
|
return str[:len(str)-1]
|
|
}
|
|
|
|
// Add adds an error to the Multierror structure.
|
|
func (err *MultiError) Add(e error) {
|
|
if e == nil {
|
|
return
|
|
}
|
|
|
|
err.mux.Lock()
|
|
err.Errors = append(err.Errors, e)
|
|
err.mux.Unlock()
|
|
}
|
|
|
|
// Return is used as a wrapper on return on whether to return the
|
|
// MultiError Structure if errors exist or nil instead of delivering an empty structure.
|
|
func (err *MultiError) Return() error {
|
|
if len(err.Errors) > 0 {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|