...

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

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

     1  // Package rollbarhandler provides Rollbar integration.
     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  // Handler is responsible for sending errors to Rollbar.
    13  type Handler struct {
    14  	client *rollbar.Client
    15  }
    16  
    17  // New creates a new handler.
    18  func New(token, environment, codeVersion, serverHost, serverRoot string) *Handler {
    19  	return NewFromClient(rollbar.New(token, environment, codeVersion, serverHost, serverRoot))
    20  }
    21  
    22  // NewFromClient creates a new handler from a client instance.
    23  func NewFromClient(client *rollbar.Client) *Handler {
    24  	return &Handler{
    25  		client: client,
    26  	}
    27  }
    28  
    29  // Handle sends the error to Rollbar.
    30  func (h *Handler) Handle(err error) {
    31  	// Get the context from the error
    32  	ctx := keyvals.ToMap(emperror.Context(err))
    33  
    34  	// Expose the stackTracer interface on the outer error (if there is stack trace in the error)
    35  	// Convert error with stack trace to an internal error type
    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  // Close closes the underlying notifier and waits for asynchronous reports to finish.
    50  func (h *Handler) Close() error {
    51  	return h.client.Close()
    52  }
    53  

View as plain text