-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
52 lines (41 loc) · 1.41 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
// Package rollbar provides Rollbar integration.
package rollbar
import (
"github.com/rollbar/rollbar-go"
"emperror.dev/emperror"
"emperror.dev/emperror/httperr"
"emperror.dev/emperror/utils/keyvals"
)
// Handler is responsible for sending errors to Rollbar.
type Handler struct {
client *rollbar.Client
}
// New creates a new handler.
func New(token, environment, codeVersion, serverHost, serverRoot string) *Handler {
return NewFromClient(rollbar.New(token, environment, codeVersion, serverHost, serverRoot))
}
// NewFromClient creates a new handler from a client instance.
func NewFromClient(client *rollbar.Client) *Handler {
return &Handler{
client: client,
}
}
// Handle sends the error to Rollbar.
func (h *Handler) Handle(err error) {
// Get the context from the error
ctx := keyvals.ToMap(emperror.Context(err))
// Expose the stackTracer interface on the outer error (if there is stack trace in the error)
// Convert error with stack trace to an internal error type
if e, ok := emperror.ExposeStackTrace(err).(stackTracer); ok {
err = newCauseStacker(e)
}
if req, ok := httperr.HTTPRequest(err); ok {
h.client.RequestErrorWithStackSkipWithExtras(rollbar.ERR, req, err, 3, ctx)
return
}
h.client.ErrorWithStackSkipWithExtras(rollbar.ERR, err, 3, ctx)
}
// Close closes the underlying notifier and waits for asynchronous reports to finish.
func (h *Handler) Close() error {
return h.client.Close()
}