Improve multi error

This commit is contained in:
世界 2022-07-29 19:50:27 +08:00
parent 2469ae4c9a
commit ad86aea967
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
2 changed files with 33 additions and 15 deletions

View file

@ -20,6 +20,39 @@ func (e *multiError) UnwrapMulti() []error {
return e.errors
}
func Errors(errors ...error) error {
errors = common.FilterNotNil(errors)
errors = ExpandAll(errors)
errors = common.UniqBy(errors, error.Error)
switch len(errors) {
case 0:
return nil
case 1:
return errors[0]
}
return &multiError{
errors: errors,
}
}
func Expand(err error) []error {
if multiErr, isMultiErr := err.(MultiError); isMultiErr {
return ExpandAll(multiErr.UnwrapMulti())
}
return []error{err}
}
func ExpandAll(errs []error) []error {
return common.FlatMap(errs, Expand)
}
func Append(err error, other error, block func(error) error) error {
if other == nil {
return err
}
return Errors(err, block(err))
}
func IsMulti(err error, targetList ...error) bool {
for _, target := range targetList {
if errors.Is(err, target) {