-
Notifications
You must be signed in to change notification settings - Fork 4
/
multierror.go
47 lines (39 loc) · 914 Bytes
/
multierror.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package multierror
import (
"errors"
"strings"
"sync"
)
// MultiError implements error interface.
// An instance of MultiError has zero or more errors.
type MultiError struct {
mutex *sync.Mutex
errs []error
}
// NewMultiError: returns a thread safe instance of multierror
func NewMultiError() *MultiError {
return &MultiError{
mutex: &sync.Mutex{},
}
}
// Push adds an error to MultiError.
func (m *MultiError) Push(errString string) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.errs = append(m.errs, errors.New(errString))
}
// HasError checks if MultiError has any error.
func (m *MultiError) HasError() error {
if len(m.errs) == 0 {
return nil
}
return m
}
// Error implements error interface.
func (m *MultiError) Error() string {
formattedError := make([]string, len(m.errs))
for i, e := range m.errs {
formattedError[i] = e.Error()
}
return strings.Join(formattedError, ", ")
}