forked from airbrake/gobrake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotice.go
79 lines (69 loc) · 1.69 KB
/
notice.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
74
75
76
77
78
79
package gobrake
import (
"fmt"
"net/http"
)
func getCreateNoticeURL(host, key string) string {
return fmt.Sprintf("http://%s/api/v3/projects/0/notices?key=%s", host, key)
}
type Error struct {
Type string `json:"type"`
Message string `json:"message"`
Backtrace []StackFrame `json:"backtrace"`
}
type notifier struct {
Name string `json:"name"`
Version string `json:"version"`
URL string `json:"url"`
}
type Notice struct {
Notifier notifier `json:"notifier"`
Errors []Error `json:"errors"`
Context map[string]string `json:"context"`
Env map[string]interface{} `json:"environment"`
Session map[string]interface{} `json:"session"`
Params map[string]interface{} `json:"params"`
}
func NewNotice(e interface{}, stack []StackFrame, req *http.Request) *Notice {
notice := &Notice{
Notifier: notifier{
Name: "gobrake",
Version: "1.0",
URL: "https://github.com/airbrake/gobrake",
},
Errors: []Error{
{
Type: fmt.Sprintf("%T", e),
Message: fmt.Sprint(e),
Backtrace: stack,
},
},
Context: map[string]string{},
Env: map[string]interface{}{},
Session: map[string]interface{}{},
Params: map[string]interface{}{},
}
if req != nil {
notice.Context["url"] = req.URL.String()
if ua := req.Header.Get("User-Agent"); ua != "" {
notice.Context["userAgent"] = ua
}
for k, v := range req.Header {
if len(v) == 1 {
notice.Env[k] = v[0]
} else {
notice.Env[k] = v
}
}
if err := req.ParseForm(); err == nil {
for k, v := range req.Form {
if len(v) == 1 {
notice.Params[k] = v[0]
} else {
notice.Params[k] = v
}
}
}
}
return notice
}