diff --git a/core.go b/core.go index 4395ea8..bee90e8 100644 --- a/core.go +++ b/core.go @@ -1,11 +1,12 @@ package zapsentry import ( - "errors" "reflect" "strings" "time" + "github.com/pkg/errors" + "github.com/getsentry/sentry-go" "go.uber.org/zap" "go.uber.org/zap/zapcore" @@ -128,7 +129,7 @@ func (c *core) Write(ent zapcore.Entry, fs []zapcore.Field) error { } } - _ = c.client.CaptureEvent(event, nil, c.scope()) + _ = c.client.CaptureEvent(event, c.createHint(), c.scope()) } // We may be crashing the program, so should flush any buffered events. @@ -188,6 +189,8 @@ func (c *core) addExceptionsFromError( err error, ) []sentry.Exception { for i := 0; i < maxErrorDepth && err != nil; i++ { + wrappedErr := err + err = errors.Cause(err) if _, ok := processedErrors[getTypeOf(err)]; ok { return exceptions } @@ -195,9 +198,16 @@ func (c *core) addExceptionsFromError( processedErrors[getTypeOf(err)] = struct{}{} exception := sentry.Exception{Value: err.Error(), Type: reflect.TypeOf(err).String()} + if strings.HasSuffix(exception.Type, "errors.fundamental") { + // err was created with `errors.New(msg)` - make `msg` the type, + // and get the wrapped value as the value + exception.Type = exception.Value + exception.Value = wrappedErr.Error() + } if !c.cfg.DisableStacktrace { - exception.Stacktrace = sentry.ExtractStacktrace(err) + // extract from wrappedErr - if err was not wrapped, its the same + exception.Stacktrace = sentry.ExtractStacktrace(wrappedErr) } exceptions = append(exceptions, exception) @@ -205,8 +215,6 @@ func (c *core) addExceptionsFromError( switch previousProvider := err.(type) { case interface{ Unwrap() error }: err = previousProvider.Unwrap() - case interface{ Cause() error }: - err = previousProvider.Cause() default: err = nil } @@ -300,6 +308,16 @@ func (c *core) GetClient() *sentry.Client { return c.client } +func (c *core) createHint() *sentry.EventHint { + hint := sentry.EventHint{ + Data: c.errs, + } + if len(c.errs) > 0 { + hint.OriginalException = c.errs[0] + } + return &hint +} + type core struct { client *sentry.Client cfg *Configuration diff --git a/go.mod b/go.mod index 3172260..74c56da 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( ) require ( + github.com/pkg/errors v0.9.1 go.uber.org/atomic v1.7.0 // indirect go.uber.org/multierr v1.6.0 // indirect golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec // indirect