Skip to content

Commit

Permalink
Add AppendLogicalError method to append a logical error if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
catsby committed Sep 5, 2018
1 parent 2f828cb commit 2109610
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
13 changes: 13 additions & 0 deletions helper/awsutil/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package awsutil

import (
awsRequest "github.com/aws/aws-sdk-go/aws/request"
multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/vault/logical"
)

Expand All @@ -16,3 +17,15 @@ func CheckAWSError(err error) error {
}
return nil
}

// AppendLogicalError checks if the given error is a known AWS error we modify,
// and if so then returns a go-multierror, appending the original and the
// logical error.
// If the error is not an AWS error, or not an error we wish to modify, then
// return the original error.
func AppendLogicalError(err error) error {
if awserr := CheckAWSError(err); awserr != nil {
err = multierror.Append(err, awserr)
}
return err
}
42 changes: 42 additions & 0 deletions helper/awsutil/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/aws/aws-sdk-go/aws/awserr"
multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/vault/logical"
)

Expand Down Expand Up @@ -50,3 +51,44 @@ func Test_CheckAWSError(t *testing.T) {
})
}
}

func Test_AppendLogicalError(t *testing.T) {
awsErr := awserr.New("Throttling", "", nil)
testCases := []struct {
Name string
Err error
Expected error
}{
{
Name: "Something not checked",
Err: fmt.Errorf("something"),
Expected: fmt.Errorf("something"),
},
{
Name: "Upstream throttle error",
Err: awsErr,
Expected: multierror.Append(awsErr, logical.ErrUpstreamRateLimited),
},
{
Name: "Nil",
},
}

for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
err := AppendLogicalError(tc.Err)
if err == nil && tc.Expected != nil {
t.Fatalf("expected non-nil error (%#v), got nil", tc.Expected)
}
if err != nil && tc.Expected == nil {
t.Fatalf("expected nil error, got (%#v)", err)
}
if err == nil && tc.Expected == nil {
return
}
if err.Error() != tc.Expected.Error() {
t.Fatalf("expected error (%#v), got (%#v)", tc.Expected.Error(), err.Error())
}
})
}
}

0 comments on commit 2109610

Please sign in to comment.