-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrorGroup.go
More file actions
61 lines (52 loc) · 1.75 KB
/
errorGroup.go
File metadata and controls
61 lines (52 loc) · 1.75 KB
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
package background
import "fmt"
// ErrTail detaches after error group Background initialization.
// The tail is supposed to stay in a background job associated with
// created Background and used to assign error to it.
type ErrTail interface {
// Error assigns err to associated background.
// If the background already has an error - does nothing.
Error(err error)
// Errorf formats according to a format specifier and assigns
// the string to associated background as a value that satisfies error.
// If the background already has an error - does nothing.
Errorf(format string, a ...interface{})
}
type errGroupBackground struct {
*errBackground
}
// WithErrorGroup returns new background with merged children that can
// store an error.
//
// The returned ErrTail is used to assign error to the background.
func WithErrorGroup(children ...Background) (Background, ErrTail) {
b := withErrorGroup(children...)
return b, b
}
func withErrorGroup(children ...Background) *errGroupBackground {
return &errGroupBackground{errBackground: withError(nil, children...)}
}
// Error assigns err to the Background.
//
// If the Background already has an error - does nothing.
func (e *errGroupBackground) Error(err error) {
if err != nil {
e.Lock()
if e.err == nil {
e.err = err
}
e.Unlock()
}
}
// Errorf formats according to a format specifier and assigns
// the string to the Background as a value that satisfies error.
//
// If the Background already has an error - does nothing.
//
// Uses fmt.Errorf thus supports error wrapping with %w verb.
func (e *errGroupBackground) Errorf(format string, a ...interface{}) {
e.Error(fmt.Errorf(format, a...))
}
func (e *errGroupBackground) DependsOn(children ...Background) Background {
return withDependency(e, children...)
}