Skip to content

Commit

Permalink
fix: missing stack track for parsing error in logrusentry (#677)
Browse files Browse the repository at this point in the history
  • Loading branch information
yiuiua committed Aug 7, 2023
1 parent cda691d commit 959597d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
20 changes: 17 additions & 3 deletions logrus/logrusentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ package sentrylogrus
import (
"errors"
"net/http"
"reflect"
"time"

sentry "github.com/getsentry/sentry-go"
"github.com/sirupsen/logrus"

sentry "github.com/getsentry/sentry-go"
)

// These default log field keys are used to pass specific metadata in a way that
Expand Down Expand Up @@ -182,17 +184,22 @@ func (h *Hook) entryToEvent(l *logrus.Entry) *sentry.Event {
}

func (h *Hook) exceptions(err error) []sentry.Exception {
if err == nil {
return nil
}

if !h.hub.Client().Options().AttachStacktrace {
return []sentry.Exception{{
Type: "error",
Type: reflect.TypeOf(err).String(),
Value: err.Error(),
}}
}

excs := []sentry.Exception{}
var last *sentry.Exception
for ; err != nil; err = errors.Unwrap(err) {
exc := sentry.Exception{
Type: "error",
Type: reflect.TypeOf(err).String(),
Value: err.Error(),
Stacktrace: sentry.ExtractStacktrace(err),
}
Expand All @@ -208,6 +215,13 @@ func (h *Hook) exceptions(err error) []sentry.Exception {
excs = append(excs, exc)
last = &excs[len(excs)-1]
}

// Add a trace of the current stack to the most recent error in a chain if
// it doesn't have a stack trace yet.
if excs[0].Stacktrace == nil {
excs[0].Stacktrace = sentry.NewStacktrace()
}

// reverse
for i, j := 0, len(excs)-1; i < j; i, j = i+1, j-1 {
excs[i], excs[j] = excs[j], excs[i]
Expand Down
30 changes: 18 additions & 12 deletions logrus/logrusentry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,37 +267,43 @@ func Test_exceptions(t *testing.T) {
err error
want []sentry.Exception
}{
{
name: "error is nil",
trace: true,
err: nil,
want: nil,
},
{
name: "std error",
trace: true,
err: errors.New("foo"),
want: []sentry.Exception{
{Type: "error", Value: "foo"},
{Type: "*errors.errorString", Value: "foo", Stacktrace: &sentry.Stacktrace{Frames: []sentry.Frame{}}},
},
},
{
name: "wrapped, no stack",
name: "wrapped error",
trace: true,
err: fmt.Errorf("foo: %w", errors.New("bar")),
want: []sentry.Exception{
{Type: "error", Value: "bar"},
{Type: "error", Value: "foo: bar"},
{Type: "*errors.errorString", Value: "bar"},
{Type: "*fmt.wrapError", Value: "foo: bar", Stacktrace: &sentry.Stacktrace{Frames: []sentry.Frame{}}},
},
},
{
name: "ignored stack",
name: "missing stack for pkgerr",
trace: false,
err: pkgerr.New("foo"),
want: []sentry.Exception{
{Type: "error", Value: "foo"},
{Type: "*errors.fundamental", Value: "foo"},
},
},
{
name: "stack",
trace: true,
err: pkgerr.New("foo"),
want: []sentry.Exception{
{Type: "error", Value: "foo", Stacktrace: &sentry.Stacktrace{Frames: []sentry.Frame{}}},
{Type: "*errors.fundamental", Value: "foo", Stacktrace: &sentry.Stacktrace{Frames: []sentry.Frame{}}},
},
},
{
Expand All @@ -311,11 +317,11 @@ func Test_exceptions(t *testing.T) {
return fmt.Errorf("wrapped: %w", err)
}(),
want: []sentry.Exception{
{Type: "error", Value: "original"},
{Type: "error", Value: "fmt: original"},
{Type: "error", Value: "wrap: fmt: original", Stacktrace: &sentry.Stacktrace{Frames: []sentry.Frame{}}},
{Type: "error", Value: "wrap: fmt: original", Stacktrace: &sentry.Stacktrace{Frames: []sentry.Frame{}}},
{Type: "error", Value: "wrapped: wrap: fmt: original"},
{Type: "*errors.errorString", Value: "original"},
{Type: "*fmt.wrapError", Value: "fmt: original"},
{Type: "*errors.withStack", Value: "wrap: fmt: original", Stacktrace: &sentry.Stacktrace{Frames: []sentry.Frame{}}},
{Type: "*errors.withStack", Value: "wrap: fmt: original", Stacktrace: &sentry.Stacktrace{Frames: []sentry.Frame{}}},
{Type: "*fmt.wrapError", Value: "wrapped: wrap: fmt: original", Stacktrace: &sentry.Stacktrace{Frames: []sentry.Frame{}}},
},
},
}
Expand Down

0 comments on commit 959597d

Please sign in to comment.