2021-12-18 22:33:50 -05:00
|
|
|
package gokwallet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
NewErrors returns a new MultiError based on a slice of error.Error (errs).
|
|
|
|
Any nil errors are trimmed. If there are no actual errors after trimming, err will be nil.
|
|
|
|
*/
|
|
|
|
func NewErrors(errs ...error) (err error) {
|
|
|
|
|
|
|
|
if errs == nil || len(errs) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var realErrs []error = make([]error, 0)
|
|
|
|
|
|
|
|
for _, e := range errs {
|
|
|
|
if e == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
realErrs = append(realErrs, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(realErrs) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = &MultiError{
|
|
|
|
Errors: realErrs,
|
|
|
|
ErrorSep: "\n",
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-22 03:20:08 -05:00
|
|
|
// Error returns a string representation of a MultiError (to conform with the error interface).
|
2021-12-18 22:33:50 -05:00
|
|
|
func (e *MultiError) Error() (errStr string) {
|
|
|
|
|
|
|
|
var numErrs int
|
|
|
|
|
|
|
|
if e == nil || len(e.Errors) == 0 {
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
numErrs = len(e.Errors)
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx, err := range e.Errors {
|
|
|
|
if (idx + 1) < numErrs {
|
|
|
|
errStr += fmt.Sprintf(err.Error(), e.ErrorSep)
|
|
|
|
} else {
|
|
|
|
errStr += err.Error()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|