Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace error type assertions with errors.As in DefaultFormattingStrategy #353

Merged
merged 2 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions strategy/exception/default_exception_formatting_strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package exception
import (
"bytes"
"crypto/rand"
goerrors "errors"
"fmt"
"runtime"
"strings"
Expand Down Expand Up @@ -115,7 +116,8 @@ func (dEFS *DefaultFormattingStrategy) Panicf(formatString string, args ...inter
// ExceptionFromError takes an error and returns value of Exception
func (dEFS *DefaultFormattingStrategy) ExceptionFromError(err error) Exception {
var isRemote bool
if reqErr, ok := err.(awserr.RequestFailure); ok {
var reqErr awserr.RequestFailure
bhautikpip marked this conversation as resolved.
Show resolved Hide resolved
if goerrors.As(err, &reqErr) {
// A service error occurs
if reqErr.RequestID() != "" {
isRemote = true
Expand All @@ -133,22 +135,25 @@ func (dEFS *DefaultFormattingStrategy) ExceptionFromError(err error) Exception {
Remote: isRemote,
}

if err, ok := err.(*XRayError); ok {
e.Type = err.Type
xRayErr := &XRayError{}
if goerrors.As(err, &xRayErr) {
bhautikpip marked this conversation as resolved.
Show resolved Hide resolved
e.Type = xRayErr.Type
}

var s []uintptr

// This is our publicly supported interface for passing along stack traces
if err, ok := err.(StackTracer); ok {
s = err.StackTrace()
var st StackTracer
if goerrors.As(err, &st) {
s = st.StackTrace()
}

// We also accept github.com/pkg/errors style stack traces for ease of use
if err, ok := err.(interface {
var est interface {
StackTrace() errors.StackTrace
}); ok {
for _, frame := range err.StackTrace() {
}
if goerrors.As(err, &est) {
for _, frame := range est.StackTrace() {
s = append(s, uintptr(frame))
}
}
Expand Down
25 changes: 25 additions & 0 deletions strategy/exception/exception_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"errors"
"testing"

"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -105,6 +106,30 @@ func TestExceptionFromError(t *testing.T) {
assert.Equal(t, "errors.errorString", err.Type)
}

func TestExceptionFromErrorRequestFailure(t *testing.T) {
defaultStrategy := &DefaultFormattingStrategy{}
reqErr := awserr.NewRequestFailure(awserr.New("error code", "error message", errors.New("new error")), 400, "1234")

err := defaultStrategy.ExceptionFromError(reqErr)

assert.NotNil(t, err.ID)
assert.Contains(t, err.Message, "new error")
assert.Contains(t, err.Message, "1234")
assert.Equal(t, "awserr.requestError", err.Type)
assert.Equal(t, true, err.Remote)
}

func TestExceptionFromErrorXRayError(t *testing.T) {
defaultStrategy := &DefaultFormattingStrategy{}
xRayErr := defaultStrategy.Error("new XRayError")

err := defaultStrategy.ExceptionFromError(xRayErr)

assert.NotNil(t, err.ID)
assert.Equal(t, "new XRayError", err.Message)
assert.Equal(t, "error", err.Type)
}

// Benchmarks
func BenchmarkDefaultFormattingStrategy_Error(b *testing.B) {
defs, _ := NewDefaultFormattingStrategy()
Expand Down