...

Source file src/github.com/goph/emperror/handler/bugsnaghandler/handler.go

Documentation: github.com/goph/emperror/handler/bugsnaghandler

     1  // Package bugsnaghandler provides Bugsnag integration.
     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  // Handler is responsible for sending errors to Bugsnag.
    15  type Handler struct {
    16  	notifier *bugsnag.Notifier
    17  }
    18  
    19  // New creates a new handler.
    20  func New(apiKey string) *Handler {
    21  	return NewFromNotifier(bugsnag.New(bugsnag.Configuration{
    22  		APIKey: apiKey,
    23  	}))
    24  }
    25  
    26  // NewFromNotifier creates a new handler from an existing notifier instance.
    27  func NewFromNotifier(notifier *bugsnag.Notifier) *Handler {
    28  	return &Handler{
    29  		notifier: notifier,
    30  	}
    31  }
    32  
    33  // Handle sends the error to Bugsnag.
    34  func (h *Handler) Handle(err error) {
    35  	// Expose the stackTracer interface on the outer error (if there is stack trace in the error)
    36  	// Convert error with stack trace to an internal error type
    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