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

Commit

Permalink
ref: Remove reliance on pkg/errors (#228)
Browse files Browse the repository at this point in the history
We don't actually use anything out of this library except talking with
it's interfaces, so let's abstract out usage rather than forcing a
strong dependency on the library.
  • Loading branch information
mattrobenolt authored and kamilogorek committed Jan 25, 2019
1 parent 0b712ea commit 238ebd8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 32 deletions.
31 changes: 28 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"time"

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

const (
Expand Down Expand Up @@ -743,7 +742,7 @@ func (client *Client) CaptureError(err error, tags map[string]string, interfaces
}

extra := extractExtra(err)
cause := pkgErrors.Cause(err)
cause := Cause(err)

packet := NewPacketWithExtra(err.Error(), extra, append(append(interfaces, client.context.interfaces()...), NewException(cause, GetOrNewStacktrace(cause, 1, 3, client.includePaths)))...)
eventID, _ := client.Capture(packet, tags)
Expand All @@ -768,7 +767,7 @@ func (client *Client) CaptureErrorAndWait(err error, tags map[string]string, int
}

extra := extractExtra(err)
cause := pkgErrors.Cause(err)
cause := Cause(err)

packet := NewPacketWithExtra(err.Error(), extra, append(append(interfaces, client.context.interfaces()...), NewException(cause, GetOrNewStacktrace(cause, 1, 3, client.includePaths)))...)
eventID, ch := client.Capture(packet, tags)
Expand Down Expand Up @@ -1054,3 +1053,29 @@ var hostname string
func init() {
hostname, _ = os.Hostname()
}

// Cause returns the underlying cause of the error, if possible.
// An error value has a cause if it implements the following
// interface:
//
// type causer interface {
// Cause() error
// }
//
// If the error does not implement Cause, the original error will
// be returned. If the error is nil, nil will be returned without further
// investigation.
func Cause(err error) error {
type causer interface {
Cause() error
}

for err != nil {
cause, ok := err.(causer)
if !ok {
break
}
err = cause.Cause()
}
return err
}
4 changes: 1 addition & 3 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"reflect"
"testing"

pkgErrors "github.com/pkg/errors"
)

func TestWrapWithExtraGeneratesProperErrWithExtra(t *testing.T) {
Expand Down Expand Up @@ -47,7 +45,7 @@ func TestWrapWithExtraGeneratesProperErrWithExtra(t *testing.T) {
func TestWrapWithExtraGeneratesCausableError(t *testing.T) {
baseErr := fmt.Errorf("this is bad")
testErr := WrapWithExtra(baseErr, nil)
cause := pkgErrors.Cause(testErr)
cause := Cause(testErr)

if !reflect.DeepEqual(cause, baseErr) {
t.Errorf("Failed to unwrap error, got %+v, expected %+v", cause, baseErr)
Expand Down
51 changes: 25 additions & 26 deletions stacktrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"runtime"
"strings"
"sync"

"github.com/pkg/errors"
)

// Stacktrace defines Sentry's spec compliant interface holding Stacktrace information - https://docs.sentry.io/development/sdk-dev/interfaces/stacktrace/
Expand Down Expand Up @@ -56,32 +54,33 @@ type StacktraceFrame struct {

// GetOrNewStacktrace tries 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 fName string
var file string
var line int
if fn != nil {
file, line = fn.FileLine(pc)
fName = fn.Name()
} else {
file = "unknown"
fName = "unknown"
}
frame := NewStacktraceFrame(pc, fName, file, line, context, appPackagePrefixes)
if frame != nil {
frames = append([]*StacktraceFrame{frame}, frames...)
}
type stackTracer interface {
StackTrace() []runtime.Frame
}
stacktrace, ok := err.(stackTracer)
if !ok {
return NewStacktrace(skip+1, context, appPackagePrefixes)
}
var frames []*StacktraceFrame
for f := range stacktrace.StackTrace() {
pc := uintptr(f) - 1
fn := runtime.FuncForPC(pc)
var fName string
var file string
var line int
if fn != nil {
file, line = fn.FileLine(pc)
fName = fn.Name()
} else {
file = "unknown"
fName = "unknown"
}
frame := NewStacktraceFrame(pc, fName, file, line, context, appPackagePrefixes)
if frame != nil {
frames = append([]*StacktraceFrame{frame}, frames...)
}
return &Stacktrace{Frames: frames}
}
return NewStacktrace(skip+1, context, appPackagePrefixes)
return &Stacktrace{Frames: frames}
}

// NewStacktrace intializes and populates a new stacktrace, skipping skip frames.
Expand Down

0 comments on commit 238ebd8

Please sign in to comment.