-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patherrors.go
67 lines (58 loc) · 1.24 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
package appctl
import (
"context"
"fmt"
"sync"
)
type appError string
const (
ErrWrongState appError = "wrong application state"
ErrMainOmitted appError = "main function is omitted"
ErrShutdown appError = "application is in shutdown state"
ErrTermTimeout appError = "termination timeout"
)
func (e appError) Error() string {
return string(e)
}
type arrError []error
func (e arrError) Error() string {
if len(e) == 0 {
return "something went wrong"
}
var s = "the following errors occurred:"
for i := range e {
s += "\n" + e[i].Error()
}
return s
}
type parallelRun struct {
mux sync.Mutex
wg sync.WaitGroup
err arrError
}
func (p *parallelRun) do(ctx context.Context, ident string, f func(context.Context) error) {
p.wg.Add(1)
go func() {
defer func() {
r := recover()
if r != nil {
p.mux.Lock()
p.err = append(p.err, fmt.Errorf("unhandled error has occurred in the '%s' service: %v", ident, r))
p.mux.Unlock()
}
p.wg.Done()
}()
if err := f(ctx); err != nil {
p.mux.Lock()
p.err = append(p.err, fmt.Errorf("error has occurred in the '%s' service: %w", ident, err))
p.mux.Unlock()
}
}()
}
func (p *parallelRun) wait() error {
p.wg.Wait()
if len(p.err) > 0 {
return p.err
}
return nil
}