1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-12-26 03:55:26 -05:00

errors.Combine

This commit is contained in:
Darien Raymond
2018-12-04 14:17:08 +01:00
parent d4613f156b
commit bea521537e
6 changed files with 52 additions and 62 deletions

View File

@@ -2,26 +2,12 @@ package task
import (
"context"
"strings"
"v2ray.com/core/common"
"v2ray.com/core/common/signal/semaphore"
)
type Task func() error
type MultiError []error
func (e MultiError) Error() string {
var r strings.Builder
common.Must2(r.WriteString("multierr: "))
for _, err := range e {
common.Must2(r.WriteString(err.Error()))
common.Must2(r.WriteString(" | "))
}
return r.String()
}
type executionContext struct {
ctx context.Context
tasks []Task
@@ -90,30 +76,6 @@ func Sequential(tasks ...Task) ExecutionOption {
}
}
func SequentialAll(tasks ...Task) ExecutionOption {
return func(c *executionContext) {
switch len(tasks) {
case 0:
return
case 1:
c.tasks = append(c.tasks, tasks[0])
default:
c.tasks = append(c.tasks, func() error {
var merr MultiError
for _, task := range tasks {
if err := task(); err != nil {
merr = append(merr, err)
}
}
if len(merr) == 0 {
return nil
}
return merr
})
}
}
}
func OnSuccess(task Task) ExecutionOption {
return func(c *executionContext) {
c.onSuccess = task