Skip to content

Commit

Permalink
Get stacktrace from error object if if implements Stacktracer interfa…
Browse files Browse the repository at this point in the history
…ce (#18)
  • Loading branch information
aheuermann authored and evalphobia committed Sep 9, 2016
1 parent 0d3c704 commit 162f5c6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
12 changes: 11 additions & 1 deletion sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ type SentryHook struct {
extraFilters map[string]func(interface{}) interface{}
}

type Stacktracer interface {
GetStacktrace() *raven.Stacktrace
}

// StackTraceConfiguration allows for configuring stacktraces
type StackTraceConfiguration struct {
// whether stacktraces should be enabled
Expand Down Expand Up @@ -125,12 +129,18 @@ func (hook *SentryHook) Fire(entry *logrus.Entry) error {

stConfig := &hook.StacktraceConfiguration
if stConfig.Enable && entry.Level <= stConfig.Level {
currentStacktrace := raven.NewStacktrace(stConfig.Skip, stConfig.Context, stConfig.InAppPrefixes)
if err, ok := getAndDelError(d, logrus.ErrorKey); ok {
var currentStacktrace *raven.Stacktrace
if stacktracer, ok := err.(Stacktracer); ok {
currentStacktrace = stacktracer.GetStacktrace()
} else {
currentStacktrace = raven.NewStacktrace(stConfig.Skip, stConfig.Context, stConfig.InAppPrefixes)
}
exc := raven.NewException(err, currentStacktrace)
packet.Interfaces = append(packet.Interfaces, exc)
packet.Culprit = err.Error()
} else {
currentStacktrace := raven.NewStacktrace(stConfig.Skip, stConfig.Context, stConfig.InAppPrefixes)
packet.Interfaces = append(packet.Interfaces, currentStacktrace)
}
}
Expand Down
27 changes: 26 additions & 1 deletion sentry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func getTestLogger() *logrus.Logger {
type resultPacket struct {
raven.Packet
Stacktrace raven.Stacktrace `json:"stacktrace"`
Exception raven.Exception `json:"exception"`
}

func WithTestDSN(t *testing.T, tf func(string, <-chan *resultPacket)) {
Expand Down Expand Up @@ -195,7 +196,7 @@ func TestSentryStacktrace(t *testing.T) {
hook.StacktraceConfiguration.Enable = true

logger.Error(message) // this is the call that the last frame of stacktrace should capture
expectedLineno := 197 //this should be the line number of the previous line
expectedLineno := 198 //this should be the line number of the previous line

packet = <-pch
stacktraceSize = len(packet.Stacktrace.Frames)
Expand Down Expand Up @@ -232,6 +233,16 @@ func TestSentryStacktrace(t *testing.T) {
if !lastFrame.InApp {
t.Error("Frame should be identified as in_app")
}

logger.WithError(myStacktracerError{}).Error(message) // use an error that implements Stacktracer
packet = <-pch
var frames []*raven.StacktraceFrame
if packet.Exception.Stacktrace != nil {
frames = packet.Exception.Stacktrace.Frames
}
if len(frames) != 1 || frames[0].Filename != escpectedStackFrameFilename {
t.Error("Stacktrace should be taken from err if it implements the Stacktracer interface")
}
})
}

Expand Down Expand Up @@ -392,3 +403,17 @@ func (myStringer) String() string { return "myStringer!" }
type notStringer struct{}

func (notStringer) String() {}

type myStacktracerError struct{}

func (myStacktracerError) Error() string { return "myStacktracerError!" }

const escpectedStackFrameFilename = "errorFile.go"

func (myStacktracerError) GetStacktrace() *raven.Stacktrace {
return &raven.Stacktrace{
Frames: []*raven.StacktraceFrame{
{Filename: escpectedStackFrameFilename},
},
}
}

0 comments on commit 162f5c6

Please sign in to comment.