From d6998da4a4fef39eaa29128441a6bc5b5c63ebfb Mon Sep 17 00:00:00 2001 From: Michal Wasilewski Date: Fri, 20 Sep 2024 11:48:32 +0200 Subject: [PATCH 1/3] stop unwrapping in spcontext and rely on bugsnag unwrapping Signed-off-by: Michal Wasilewski --- context.go | 36 ++---------------------------------- 1 file changed, 2 insertions(+), 34 deletions(-) diff --git a/context.go b/context.go index 4e7af38..8e95d70 100644 --- a/context.go +++ b/context.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "reflect" "strings" "time" @@ -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) } } From db86f156b67c8d67af0bb454f0ff6e9ab7b7cb8b Mon Sep 17 00:00:00 2001 From: Michal Wasilewski Date: Fri, 20 Sep 2024 14:48:04 +0200 Subject: [PATCH 2/3] fix test Signed-off-by: Michal Wasilewski --- context_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/context_test.go b/context_test.go index 09d241c..9476243 100644 --- a/context_test.go +++ b/context_test.go @@ -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 {}"), )) } From cc20439bc9b693c82d9f70dad3e0f399d9a3b923 Mon Sep 17 00:00:00 2001 From: Michal Wasilewski Date: Fri, 20 Sep 2024 14:53:29 +0200 Subject: [PATCH 3/3] remove unused type Signed-off-by: Michal Wasilewski --- bugsnag.go | 66 ------------------------------------------------------ 1 file changed, 66 deletions(-) diff --git a/bugsnag.go b/bugsnag.go index 5d88968..08e7708 100644 --- a/bugsnag.go +++ b/bugsnag.go @@ -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 @@ -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 -}