...
1
2 package bugsnaghandler
3
4 import (
5 "reflect"
6
7 "github.com/bugsnag/bugsnag-go"
8 "github.com/pkg/errors"
9
10 "github.com/goph/emperror"
11 "github.com/goph/emperror/internal/keyvals"
12 )
13
14
15 type Handler struct {
16 notifier *bugsnag.Notifier
17 }
18
19
20 func New(apiKey string) *Handler {
21 return NewFromNotifier(bugsnag.New(bugsnag.Configuration{
22 APIKey: apiKey,
23 }))
24 }
25
26
27 func NewFromNotifier(notifier *bugsnag.Notifier) *Handler {
28 return &Handler{
29 notifier: notifier,
30 }
31 }
32
33
34 func (h *Handler) Handle(err error) {
35
36
37 if e, ok := emperror.ExposeStackTrace(err).(stackTracer); ok {
38 err = newErrorWithStackFrames(e)
39 }
40
41 var rawData []interface{}
42
43 if cause := errors.Cause(err); cause != nil {
44 if name := reflect.TypeOf(cause).String(); len(name) > 0 {
45 errorClass := bugsnag.ErrorClass{Name: name}
46
47 rawData = append(rawData, errorClass)
48 }
49 }
50
51 if ctx := emperror.Context(err); len(ctx) > 0 {
52 rawData = append(rawData, bugsnag.MetaData{
53 "Params": keyvals.ToMap(ctx),
54 })
55 }
56
57 _ = h.notifier.Notify(err, rawData...)
58 }
59
View as plain text