diff --git a/errortype.go b/errortype.go index 5a23171..73ff059 100644 --- a/errortype.go +++ b/errortype.go @@ -2,7 +2,7 @@ package sentry import "strings" -// ErrType represents errors which can be thrown by the sentry client. +// ErrType represents an error which may contain hierarchical error information. type ErrType string // IsInstance will tell you whether a given error is an instance @@ -11,6 +11,12 @@ func (e ErrType) IsInstance(err error) bool { return strings.Contains(err.Error(), string(e)) } +// Unwrap will unwrap this error and return the underlying error which caused +// it to be triggered. +func (e ErrType) Unwrap() error { + return nil +} + // Error gets the error message for this ErrType func (e ErrType) Error() string { return string(e) diff --git a/exception.go b/exception.go index 88e453a..cb8a125 100644 --- a/exception.go +++ b/exception.go @@ -44,7 +44,7 @@ func (e *ExceptionInfo) ForError(err error) *ExceptionInfo { if m := errorMsgPattern.FindStringSubmatch(err.Error()); m != nil { e.Module = m[1] - e.Value = m[2] + e.Type = m[2] } return e @@ -58,11 +58,16 @@ func ExceptionForError(err error) Option { for err != nil { exceptions = append([]*ExceptionInfo{NewExceptionInfo().ForError(err)}, exceptions...) - if causer, ok := err.(interface { + switch e := err.(type) { + case interface { Cause() error - }); ok { - err = causer.Cause() - } else { + }: + err = e.Cause() + case interface { + Unwrap() error + }: + err = e.Unwrap() + default: err = nil } } @@ -80,7 +85,7 @@ func Exception(info *ExceptionInfo) Option { } } -var errorMsgPattern = regexp.MustCompile(`\A(\w+): (.+)\z`) +var errorMsgPattern = regexp.MustCompile(`\A(?:(\w+): )?([^:]+)`) type exceptionOption struct { Exceptions []*ExceptionInfo `json:"values"` diff --git a/exception_test.go b/exception_test.go index e9c8c42..6b64f04 100644 --- a/exception_test.go +++ b/exception_test.go @@ -134,6 +134,8 @@ func TestException(t *testing.T) { ex := &ExceptionInfo{} err := fmt.Errorf("example error") So(ex.ForError(err), ShouldEqual, ex) + So(ex.Type, ShouldEqual, "example error") + So(ex.Module, ShouldEqual, "") So(ex.Value, ShouldEqual, "example error") So(ex.StackTrace, ShouldNotBeNil) }) @@ -141,6 +143,8 @@ func TestException(t *testing.T) { Convey("With a normal error", func() { err := fmt.Errorf("example error") So(ex.ForError(err), ShouldEqual, ex) + So(ex.Type, ShouldEqual, "example error") + So(ex.Module, ShouldEqual, "") So(ex.Value, ShouldEqual, "example error") So(ex.StackTrace, ShouldNotBeNil) }) @@ -148,6 +152,8 @@ func TestException(t *testing.T) { Convey("With a stacktraceable error", func() { err := errors.New("example error") So(ex.ForError(err), ShouldEqual, ex) + So(ex.Module, ShouldEqual, "") + So(ex.Type, ShouldEqual, "example error") So(ex.Value, ShouldEqual, "example error") So(ex.StackTrace, ShouldNotBeNil) }) @@ -156,7 +162,8 @@ func TestException(t *testing.T) { err := errors.New("test: example error") So(ex.ForError(err), ShouldEqual, ex) So(ex.Module, ShouldEqual, "test") - So(ex.Value, ShouldEqual, "example error") + So(ex.Type, ShouldEqual, "example error") + So(ex.Value, ShouldEqual, "test: example error") }) }) }) diff --git a/go.mod b/go.mod index b28e1f2..e87a41d 100644 --- a/go.mod +++ b/go.mod @@ -11,3 +11,5 @@ require ( github.com/stretchr/testify v1.3.0 // indirect golang.org/x/sys v0.0.0-20190220154126-629670e5acc5 // indirect ) + +go 1.13