...

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

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

     1  package rollbarhandler
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  	"strconv"
     7  	"strings"
     8  
     9  	"github.com/pkg/errors"
    10  	"github.com/rollbar/rollbar-go"
    11  )
    12  
    13  type stackTracer interface {
    14  	Error() string
    15  	StackTrace() errors.StackTrace
    16  }
    17  
    18  type causeStacker struct {
    19  	err stackTracer
    20  }
    21  
    22  func newCauseStacker(err stackTracer) *causeStacker {
    23  	return &causeStacker{
    24  		err: err,
    25  	}
    26  }
    27  
    28  func (e *causeStacker) Error() string {
    29  	return e.err.Error()
    30  }
    31  
    32  func (e *causeStacker) Cause() error {
    33  	if c, ok := e.err.(interface{ Cause() error }); ok {
    34  		return c.Cause()
    35  	}
    36  
    37  	return nil
    38  }
    39  
    40  func (e *causeStacker) Stack() rollbar.Stack {
    41  	stackTrace := e.err.StackTrace()
    42  	stack := make(rollbar.Stack, len(stackTrace))
    43  
    44  	for i, frame := range stackTrace {
    45  		line, _ := strconv.Atoi(fmt.Sprintf("%d", frame))
    46  
    47  		stack[i] = rollbar.Frame{
    48  			Filename: shortenFilePath(strings.SplitN(fmt.Sprintf("%+s", frame), "\n\t", 2)[1]), // nolint: govet
    49  			Method:   fmt.Sprintf("%n", frame),                                                 // nolint: govet
    50  			Line:     line,
    51  		}
    52  	}
    53  
    54  	return stack
    55  }
    56  
    57  // copied from rollbar
    58  
    59  // nolint: gochecknoglobals
    60  var (
    61  	knownFilePathPatterns = []string{
    62  		"github.com/",
    63  		"code.google.com/",
    64  		"bitbucket.org/",
    65  		"launchpad.net/",
    66  	}
    67  )
    68  
    69  func shortenFilePath(s string) string {
    70  	// added to the original function
    71  	s = strings.TrimPrefix(s, runtime.GOROOT())
    72  
    73  	idx := strings.Index(s, "/src/pkg/")
    74  	if idx != -1 {
    75  		return s[idx+5:]
    76  	}
    77  	for _, pattern := range knownFilePathPatterns {
    78  		idx = strings.Index(s, pattern)
    79  		if idx != -1 {
    80  			return s[idx:]
    81  		}
    82  	}
    83  	return s
    84  }
    85  

View as plain text