-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraygun.go
95 lines (78 loc) · 1.76 KB
/
raygun.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package goraygun
import (
"encoding/json"
"errors"
"log"
"net/http"
"strings"
)
const ENDPOINT = "https://api.raygun.io/entries"
const (
ClientName = "Go-Raygun"
ClientVersion = "0.0.1"
ClientRepo = "http://github.com/sditools/go-raygun"
)
type Settings struct {
ApiKey string
Endpoint string
Environment string
}
type Client struct {
settings Settings
Entry Entry
}
func Init(s Settings, e Entry) *Client {
// provide user the option to override (for testing)
if s.Endpoint == "" {
s.Endpoint = ENDPOINT
}
c := &Client{}
c.settings = s
e.Details.Environment.populate()
c.Entry = e
return c
}
func (c *Client) Recover() {
if err := recover(); err != nil {
c.Report(getError(err), c.Entry)
}
}
func (c *Client) Report(err error, entry Entry) {
st, stErr := GetStackTrace(3)
if stErr != nil {
// handle stErr
return
}
entry.populate(err, st, c.settings.Environment)
c.post(entry, c.settings.Endpoint)
}
func (c *Client) post(e Entry, uri string) {
data, err := json.Marshal(e)
if err != nil {
log.Printf("Error Marshalling RayGun Message: %v:", err)
}
req, err := http.NewRequest("POST", uri, strings.NewReader(string(data)))
if err != nil {
log.Printf("Error creating POST request: %v", err)
}
req.Header.Set("X-ApiKey", c.settings.ApiKey)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Printf("Error sending Request: %v", err)
}
if resp.StatusCode != http.StatusAccepted {
log.Printf("Error status sent back: %v", resp.StatusCode)
}
defer resp.Body.Close()
}
func getError(err interface{}) error {
switch err := err.(type) {
case error:
return err
case string:
return errors.New(err)
default:
return errors.New("")
}
}