-
Notifications
You must be signed in to change notification settings - Fork 6
/
handler.go
53 lines (47 loc) · 979 Bytes
/
handler.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
package grace
type Handlers struct {
err error
}
type HandlerFunc func(error) error
func Handler(e error) *Handlers {
h := Handlers{e}
if e == nil {
h.err = &grace{}
}
return &h
}
// Loop a list of handlers on an error or multiple error.
func (h *Handlers) Loop() error {
switch err := h.err.(type) {
case *grace:
for index := range err.errors {
for _, elmH := range err.handlers {
err.errors[index] = elmH(err.errors[index])
}
}
}
return h.err
}
// Get a list of existing handler func
func (h *Handlers) Get() []HandlerFunc {
switch err := h.err.(type) {
case *grace:
return err.handlers
}
return nil
}
// Add a list of handlers to a new error.
func (h *Handlers) Add(f ...HandlerFunc) error {
switch err := h.err.(type) {
case *grace:
err.handlers = append(err.handlers, f...)
return h.err
default:
g := grace{}
if h.err != nil {
g.errors = append(g.errors, h.err)
}
g.handlers = append(g.handlers, f...)
return &g
}
}