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

Remove reliance on pkg/errors #228

Merged
merged 2 commits into from
Jan 25, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -707,7 +706,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 @@ -732,7 +731,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 @@ -975,3 +974,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
52 changes: 25 additions & 27 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"
)

// https://docs.getsentry.com/hosted/clientdev/interfaces/#failure-interfaces
Expand Down Expand Up @@ -53,33 +51,33 @@ type StacktraceFrame struct {

// 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 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}
} else {
type stackTracer interface {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is when i remember i dont know go well enough to understand what this is for

StackTrace() []runtime.Frame

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In respect of /pkg/errors this implementation does not work. Not yet sure if I'm doing something wrong, but passing error created by errors.New(...) to this function, ends up in !ok if statement. This is because pkg/errors says:

// New, Errorf, Wrap, and Wrapf record a stack trace at the point they are
// invoked. This information can be retrieved with the following interface:
//
//     type stackTracer interface {
//             StackTrace() errors.StackTrace
//     }
//
// The returned errors.StackTrace type is defined as
//
//     type StackTrace []Frame

How should I use this method with /pkg/errors ? Im confused.

}
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}
}

// Intialize and populate a new stacktrace, skipping skip frames.
Expand Down