Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: stop unwrapping in spcontext and rely on bugsnag unwrapping #22

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
66 changes: 0 additions & 66 deletions bugsnag.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package spcontext

import (
"runtime"
"strings"

bugsnagerrors "github.com/bugsnag/bugsnag-go/v2/errors"
"github.com/pkg/errors"
)

// BugsnagLogger wraps the given Context inside a bugsnag friendly logger.
type BugsnagLogger struct {
Ctx Context
Expand All @@ -17,61 +9,3 @@ type BugsnagLogger struct {
func (l *BugsnagLogger) Printf(format string, v ...interface{}) {
l.Ctx.Infof(format, v...)
}

type stackTracer interface {
error
StackTrace() errors.StackTrace
}

// errorWithStackFrames satisfies bugsnag.ErrorWithStackFrames for a github.com/pkg/errors error.
type errorWithStackFrames struct {
err stackTracer
}

// Cause returns initial error, provides compatibility for pkg/errors chains.
func (w *errorWithStackFrames) Cause() error { return w.err }

// Unwrap returns initial error, provides compatibility for Go 1.13 error chains.
func (w *errorWithStackFrames) Unwrap() error { return w.err }

func (e *errorWithStackFrames) Error() string {
return e.err.Error()
}

func (e *errorWithStackFrames) StackFrames() []bugsnagerrors.StackFrame {
stackTrace := e.err.StackTrace()

out := make([]bugsnagerrors.StackFrame, len(stackTrace))
for i, frame := range stackTrace {
pc := uintptr(frame) - 1
fn := runtime.FuncForPC(pc)
file, line := fn.FileLine(pc)

name := fn.Name()
var pkg string
var fnName string
if pkgDivider := strings.LastIndex(name, "/"); pkgDivider != -1 {
pkg = name[:pkgDivider]
fnName = name[pkgDivider+1:]
if fnDivider := strings.LastIndex(fnName, "."); fnDivider != -1 {
fnName = fnName[fnDivider+1:]
}
} else if pkgDivider := strings.Index(name, "."); pkgDivider != -1 {
pkg = name[:pkgDivider]
fnName = name[pkgDivider+1:]
if fnDivider := strings.LastIndex(fnName, "."); fnDivider != -1 {
fnName = fnName[fnDivider+1:]
}
}

out[i] = bugsnagerrors.StackFrame{
File: file,
LineNumber: line,
Name: fnName,
Package: pkg,
ProgramCounter: pc,
}
}

return out
}
36 changes: 2 additions & 34 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"reflect"
"strings"
"time"

Expand Down Expand Up @@ -332,39 +331,8 @@ func (ctx *Context) error(fields []interface{}, err error, internal InternalMess
}

if ctx.Notifier != nil && !strings.Contains(err.Error(), context.Canceled.Error()) {
var parentErr = err
var st stackTracer
var errorClass string
curSt, ok := parentErr.(stackTracer)
if ok {
st = curSt
}

var curErr = err
for {
unwrapped := errors.Unwrap(curErr)
if unwrapped == nil {
break
}
curErr = unwrapped
curSt, ok := unwrapped.(stackTracer)
if !ok {
continue
}
st = curSt
}
if st != nil {
curErr = &errorWithStackFrames{err: st}
errorClass = reflect.TypeOf(st).String()
} else {
curErr = parentErr
errorClass = reflect.TypeOf(parentErr).String()
}

fieldsMap["original_error"] = parentErr.Error()

if err := ctx.Notifier.Notify(curErr, bugsnag.MetaData{FieldsTab: fieldsMap}, ctx, bugsnag.ErrorClass{Name: errorClass}); err != nil {
ctx.Errorf("error notifying the exception tracker: %v", err)
if notifierError := ctx.Notifier.Notify(err, bugsnag.MetaData{FieldsTab: fieldsMap}, ctx); notifierError != nil {
ctx.Errorf("error notifying the exception tracker: %v", notifierError)
}
}

Expand Down
2 changes: 1 addition & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestContext(t *testing.T) {
suite.True(notifier.AssertCalled(
t,
"Notify",
mock.AnythingOfType("*spcontext.errorWithStackFrames"),
mock.AnythingOfType("*errors.fundamental"),
mock.AnythingOfType("[]interface {}"),
))
}
Expand Down
Loading