-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
91 lines (71 loc) · 1.91 KB
/
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
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
package sentry
import (
"github.com/getsentry/raven-go"
"github.com/pkg/errors"
"emperror.dev/emperror"
"emperror.dev/emperror/httperr"
"emperror.dev/emperror/utils/keyvals"
)
// Handler is responsible for sending errors to Sentry.
type Handler struct {
client *raven.Client
sendSynchronously bool
}
// New creates a new handler.
func New(dsn string) (*Handler, error) {
client, err := raven.New(dsn)
if err != nil {
return nil, errors.Wrap(err, "failed to create raven client")
}
return NewFromClient(client), nil
}
// NewSync creates a new handler that sends errors synchronously.
func NewSync(dsn string) (*Handler, error) {
handler, err := New(dsn)
if err != nil {
return nil, err
}
handler.sendSynchronously = true
return handler, nil
}
// NewFromClient creates a new handler from a client instance.
func NewFromClient(client *raven.Client) *Handler {
return &Handler{
client: client,
}
}
// NewSyncFromClient creates a new handler from a client instance that sends errors synchronously.
func NewSyncFromClient(client *raven.Client) *Handler {
handler := NewFromClient(client)
handler.sendSynchronously = true
return handler
}
// Handle sends the error to Rollbar.
func (h *Handler) Handle(err error) {
var interfaces []raven.Interface
// Get HTTP request (if any)
if req, ok := httperr.HTTPRequest(err); ok {
interfaces = append(interfaces, raven.NewHttp(req))
}
packet := raven.NewPacketWithExtra(
err.Error(),
keyvals.ToMap(emperror.Context(err)),
append(
interfaces,
raven.NewException(
err,
raven.GetOrNewStacktrace(emperror.ExposeStackTrace(err), 1, 3, h.client.IncludePaths()),
),
)...,
)
eventID, ch := h.client.Capture(packet, nil)
if h.sendSynchronously && eventID != "" {
<-ch
}
}
// Close closes the underlying notifier and waits for asynchronous reports to finish.
func (h *Handler) Close() error {
h.client.Close()
h.client.Wait()
return nil
}