3x-ui/util/common/multi_error.go

34 lines
663 B
Go
Raw Normal View History

2023-02-09 19:18:06 +00:00
package common
import (
"strings"
)
2025-09-20 07:35:50 +00:00
// multiError represents a collection of errors.
2023-02-09 19:18:06 +00:00
type multiError []error
2025-09-20 07:35:50 +00:00
// Error returns a string representation of all errors joined with " | ".
2023-02-09 19:18:06 +00:00
func (e multiError) Error() string {
var r strings.Builder
r.WriteString("multierr: ")
for _, err := range e {
r.WriteString(err.Error())
r.WriteString(" | ")
}
return r.String()
}
2025-09-20 07:35:50 +00:00
// Combine combines multiple errors into a single error, filtering out nil errors.
2023-02-09 19:18:06 +00:00
func Combine(maybeError ...error) error {
var errs multiError
for _, err := range maybeError {
if err != nil {
errs = append(errs, err)
}
}
if len(errs) == 0 {
return nil
}
return errs
}