mirror of
https://github.com/Jguer/yay
synced 2024-10-31 04:12:51 +00:00
dacd77012c
types is pretty generic as a module name, this should be split into seperate modules.
41 lines
795 B
Go
41 lines
795 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
|
|
}
|