Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Commit

Permalink
Integration with github.com/pkg/errors (#152)
Browse files Browse the repository at this point in the history
* Squashed changes from #137

* Try to extract pkg/errors cause and use it
  • Loading branch information
jsm authored and mattrobenolt committed Nov 8, 2017
1 parent 1452f63 commit d3909e6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
5 changes: 4 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"time"

"github.com/certifi/gocertifi"
pkgErrors "github.com/pkg/errors"
)

const (
Expand Down Expand Up @@ -639,7 +640,9 @@ func (client *Client) CaptureError(err error, tags map[string]string, interfaces
return ""
}

packet := NewPacket(err.Error(), append(append(interfaces, client.context.interfaces()...), NewException(err, NewStacktrace(1, 3, client.includePaths)))...)
cause := pkgErrors.Cause(err)

packet := NewPacket(cause.Error(), append(append(interfaces, client.context.interfaces()...), NewException(cause, GetOrNewStacktrace(err, 1, 3, client.includePaths)))...)
eventID, _ := client.Capture(packet, tags)

return eventID
Expand Down
2 changes: 1 addition & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func RecoveryHandler(handler func(http.ResponseWriter, *http.Request)) func(http
if rval := recover(); rval != nil {
debug.PrintStack()
rvalStr := fmt.Sprint(rval)
packet := NewPacket(rvalStr, NewException(errors.New(rvalStr), NewStacktrace(2, 3, nil)), NewHttp(r))
packet := NewPacket(rvalStr, NewException(errors.New(rvalStr), GetOrNewStacktrace(rval.(error), 2, 3, nil)), NewHttp(r))
Capture(packet, nil)
w.WriteHeader(http.StatusInternalServerError)
}
Expand Down
31 changes: 31 additions & 0 deletions stacktrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"runtime"
"strings"
"sync"

"github.com/pkg/errors"
)

// https://docs.getsentry.com/hosted/clientdev/interfaces/#failure-interfaces
Expand Down Expand Up @@ -49,6 +51,35 @@ type StacktraceFrame struct {
InApp bool `json:"in_app"`
}

// Try to get stacktrace from err as an interface of github.com/pkg/errors, or else NewStacktrace()
func GetOrNewStacktrace(err error, skip int, context int, appPackagePrefixes []string) *Stacktrace {
stacktracer, errHasStacktrace := err.(interface {
StackTrace() errors.StackTrace
})
if errHasStacktrace {
var frames []*StacktraceFrame
for _, f := range stacktracer.StackTrace() {
pc := uintptr(f) - 1
fn := runtime.FuncForPC(pc)
var file string
var line int
if fn != nil {
file, line = fn.FileLine(pc)
} else {
file = "unknown"
}
frame := NewStacktraceFrame(pc, file, line, context, appPackagePrefixes)
if frame != nil {
frames = append([]*StacktraceFrame{frame}, frames...)
}
}
return &Stacktrace{Frames: frames}
} else {
return NewStacktrace(skip + 1, context, appPackagePrefixes)
}
}


// Intialize and populate a new stacktrace, skipping skip frames.
//
// context is the number of surrounding lines that should be included for context.
Expand Down

0 comments on commit d3909e6

Please sign in to comment.