-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogResult.go
73 lines (60 loc) · 1.37 KB
/
logResult.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
package main
import (
"encoding/json"
"fmt"
)
// Result for a given job
type logResult struct {
job []byte
jobType string
metaData map[string]string
payload []byte
}
func (r *logResult) Job() []byte {
return r.job
}
func (r *logResult) Decode() (Job, error) {
if r.jobType == LogQueueJobType {
jd := &logQueueJob{}
err := json.Unmarshal(r.job, jd)
if err != nil {
return jd, nil
}
return nil, err
} else if r.jobType == LogProcessorJobType {
jd := &logProcessorJob{}
err := json.Unmarshal(r.job, jd)
if err != nil {
return jd, nil
}
return nil, err
}
ne := fmt.Errorf("Unknown job type: %v", r.jobType)
return nil, ne
}
// LogErrorResults generic handler for errors while
// running a LogJob
func (r *logResult) LogErrorResults(job interface{}, callingerror error) (Result, error) {
var jobD []byte
// return errors in metadata
md := make(map[string]string)
md["original_error"] = callingerror.Error()
jobD, err := json.Marshal(job)
if err != nil {
md["marshal_error"] = "Failed to marshal job data"
// jobType is undefined if marshal fails
md["jobtype_error"] = "No Job type error"
r.jobType = "UNDEFINED"
} else {
r.jobType = job.(Job).Type()
}
r.metaData = md
r.job = jobD
return r, nil
}
func (r *logResult) MetaData() map[string]string {
return r.metaData
}
func (r *logResult) Payload() []byte {
return r.payload
}