Skip to content

Commit

Permalink
feat: Improve error handling to support Go 1.13
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Sep 11, 2019
1 parent 2a480ab commit 34b47bb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
8 changes: 7 additions & 1 deletion errortype.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
17 changes: 11 additions & 6 deletions exception.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
Expand All @@ -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"`
Expand Down
9 changes: 8 additions & 1 deletion exception_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,26 @@ 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)
})

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)
})

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)
})
Expand All @@ -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")
})
})
})
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 34b47bb

Please sign in to comment.