...
1
2 package rollbarhandler
3
4 import (
5 "github.com/rollbar/rollbar-go"
6
7 "github.com/goph/emperror"
8 "github.com/goph/emperror/httperr"
9 "github.com/goph/emperror/internal/keyvals"
10 )
11
12
13 type Handler struct {
14 client *rollbar.Client
15 }
16
17
18 func New(token, environment, codeVersion, serverHost, serverRoot string) *Handler {
19 return NewFromClient(rollbar.New(token, environment, codeVersion, serverHost, serverRoot))
20 }
21
22
23 func NewFromClient(client *rollbar.Client) *Handler {
24 return &Handler{
25 client: client,
26 }
27 }
28
29
30 func (h *Handler) Handle(err error) {
31
32 ctx := keyvals.ToMap(emperror.Context(err))
33
34
35
36 if e, ok := emperror.ExposeStackTrace(err).(stackTracer); ok {
37 err = newCauseStacker(e)
38 }
39
40 if req, ok := httperr.HTTPRequest(err); ok {
41 h.client.RequestErrorWithStackSkipWithExtras(rollbar.ERR, req, err, 3, ctx)
42
43 return
44 }
45
46 h.client.ErrorWithStackSkipWithExtras(rollbar.ERR, err, 3, ctx)
47 }
48
49
50 func (h *Handler) Close() error {
51 return h.client.Close()
52 }
53
View as plain text