-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_error.go
35 lines (27 loc) · 865 Bytes
/
multi_error.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
package errors
import "fmt"
// NewMulti returns a new errors.MultiError with the provided errs.
func NewMulti(errs ...error) error {
return &MultiError{
Errors: errs,
}
}
// AppendMulti appends err to multi.Errors and returns an error if multi is not of the type errors.MultiError.
func AppendMulti(multi error, err error) error {
m, ok := multi.(*MultiError)
if !ok {
return New("the multi parameter is supposed to be given an errors.MultiError value")
}
m.Errors = append(m.Errors, err)
return nil
}
// MultiError is the error struct for multiple errors used internally by the package. This type should be only be used for type assertions.
type MultiError struct {
Errors []error
}
func (m MultiError) Error() string {
if len(m.Errors) == 0 {
return ""
}
return fmt.Sprintf("first of %d errors: %s", len(m.Errors), m.Errors[0].Error())
}