-
Notifications
You must be signed in to change notification settings - Fork 3
/
errors.go
76 lines (63 loc) · 1.12 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package util
import (
"context"
"fmt"
"time"
"github.com/pkg/errors"
)
var (
IgnoreError = NewError("ignore")
NotFoundError = NewError("not found")
FoundError = NewError("found")
DuplicatedError = NewError("duplicated error")
WrongTypeError = NewError("wrong type")
EmptyError = NewError("empty")
)
type DataContainerError struct {
d interface{}
}
func NewDataContainerError(d interface{}) DataContainerError {
return DataContainerError{d: d}
}
func (er DataContainerError) Error() string {
return fmt.Sprintf("data container error of %T", er.d)
}
func (er DataContainerError) Data() interface{} {
return er.d
}
func EnsureErrors(
ctx context.Context,
dur time.Duration,
f func() error,
errs ...error,
) error {
var wait func()
if dur > 0 {
wait = func() {
<-time.After(dur)
}
}
var err error
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
err = f()
if err == nil {
return nil
}
var found bool
for i := range errs {
if errors.Is(err, errs[i]) {
found = true
break
}
}
if !found {
return err
}
}
wait()
}
}