You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
624 B
43 lines
624 B
1 year ago
|
package errors
|
||
1 year ago
|
|
||
|
import "bytes"
|
||
|
|
||
|
type (
|
||
|
BatchError struct {
|
||
|
errs errorArray
|
||
|
}
|
||
|
errorArray []error
|
||
|
)
|
||
|
|
||
|
func (be *BatchError) Add(errs ...error) {
|
||
|
for _, err := range errs {
|
||
|
if err != nil {
|
||
|
be.errs = append(be.errs, err)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (be *BatchError) Err() error {
|
||
|
switch len(be.errs) {
|
||
|
case 0:
|
||
|
return nil
|
||
|
case 1:
|
||
|
return be.errs[0]
|
||
|
default:
|
||
|
return be.errs
|
||
|
}
|
||
|
}
|
||
|
func (be *BatchError) NotNil() bool {
|
||
|
return len(be.errs) > 0
|
||
|
}
|
||
|
func (ea errorArray) Error() string {
|
||
|
var buf bytes.Buffer
|
||
|
for i := range ea {
|
||
|
if i > 0 {
|
||
|
buf.WriteByte('\n')
|
||
|
}
|
||
|
buf.WriteString(ea[i].Error())
|
||
|
}
|
||
|
return buf.String()
|
||
|
}
|