2018-01-13 16:14:54 +00:00
package ut
import (
"bytes"
"fmt"
)
2021-08-13 08:11:13 +00:00
var _ error = & Errors { }
2018-01-13 16:14:54 +00:00
// Errors exposes slice of errors.
2021-08-13 08:11:13 +00:00
type Errors struct {
Errs [ ] error
2018-01-13 16:14:54 +00:00
}
2021-08-13 08:11:13 +00:00
func ( e Errors ) Error ( ) string {
2018-01-13 16:14:54 +00:00
var msg = & bytes . Buffer { }
fmt . Fprint ( msg , "errors: " )
2021-08-13 08:11:13 +00:00
for i , err := range e . Errs {
2018-01-13 16:14:54 +00:00
if 0 < i {
fmt . Fprint ( msg , "; " )
}
fmt . Fprint ( msg , err )
}
return msg . String ( )
}
// ErrCombine combines one or more errors, nil entries are omitted and nil is returned if all given errors are nil. The first argument is expanded if it satisfies the Errors interface. In case of aggregation of errors the return error will satisfy the Errors interface.
func ErrCombine ( errs ... error ) error {
2021-08-13 08:11:13 +00:00
if len ( errs ) > 0 {
if e , ok := errs [ 0 ] . ( * Errors ) ; ok {
errs = append ( e . Errs , errs [ 1 : ] ... )
2018-01-13 16:14:54 +00:00
}
}
for i := 0 ; i < len ( errs ) ; i ++ {
if nil == errs [ i ] {
errs = append ( errs [ : i ] , errs [ i + 1 : ] ... )
} else {
i ++
}
}
if 0 == len ( errs ) {
return nil
} else if 1 == len ( errs ) {
return errs [ 0 ]
}
2021-08-13 08:11:13 +00:00
return & Errors {
Errs : errs ,
}
2018-01-13 16:14:54 +00:00
}