-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
66 lines (55 loc) · 1.07 KB
/
context.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
package godispatch
import (
"bytes"
"fmt"
"math"
"github.com/streadway/amqp"
)
const (
AbortIndex = math.MaxInt8 / 2
)
type errorMsg struct {
Err string `json:"error"`
Meta interface{} `json:"meta"`
}
type errorMsgs []errorMsg
func (a errorMsgs) String() string {
if len(a) == 0 {
return ""
}
var buffer bytes.Buffer
for i, msg := range a {
text := fmt.Sprintf("Error #%02d: %s \n Meta: %v\n", (i + 1), msg.Err, msg.Meta)
buffer.WriteString(text)
}
return buffer.String()
}
type Context struct {
Delivery *amqp.Delivery
Message interface{}
Log Logger
Keys map[string]interface{}
Errors errorMsgs
handlers []HandlerFunc
index int8
}
func (c *Context) Next() {
c.index++
s := int8(len(c.handlers))
for ; c.index < s; c.index++ {
c.handlers[c.index](c)
}
}
func (c *Context) Abort() {
c.index = AbortIndex
}
func (c *Context) Fail(err error) {
c.Error(err, "Operation aborted")
c.Abort()
}
func (c *Context) Error(err error, meta interface{}) {
c.Errors = append(c.Errors, errorMsg{
Err: err.Error(),
Meta: meta,
})
}