Skip to content

Commit

Permalink
service/s3/s3manager: Fix check for nil OrigErr in Error()(#1749)
Browse files Browse the repository at this point in the history
S3 Manager's `Error` type did not check for nil of `OrigErr` when calling `Error()`

Fix #1748
  • Loading branch information
caseylucas authored and jasdel committed Jan 24, 2018
1 parent c2623b4 commit 70713ce
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
10 changes: 9 additions & 1 deletion service/s3/s3manager/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,15 @@ func newError(err error, bucket, key *string) Error {
}

func (err *Error) Error() string {
return fmt.Sprintf("failed to upload %q to %q:\n%s", err.Key, err.Bucket, err.OrigErr.Error())
origErr := ""
if err.OrigErr != nil {
origErr = ":\n" + err.OrigErr.Error()
}
return fmt.Sprintf("failed to upload %q to %q%s",
aws.StringValue(err.Key),
aws.StringValue(err.Bucket),
origErr,
)
}

// NewBatchError will return a BatchError that satisfies the awserr.Error interface.
Expand Down
24 changes: 24 additions & 0 deletions service/s3/s3manager/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,30 @@ func (client *mockS3Client) ListObjects(input *s3.ListObjectsInput) (*s3.ListObj
return object, nil
}

func TestNilOrigError(t *testing.T) {
err := Error{
Bucket: aws.String("bucket"),
Key: aws.String("key"),
}
errStr := err.Error()
const expected1 = `failed to upload "key" to "bucket"`
if errStr != expected1 {
t.Errorf("Expected %s, but received %s", expected1, errStr)
}

err = Error{
OrigErr: errors.New("foo"),
Bucket: aws.String("bucket"),
Key: aws.String("key"),
}
errStr = err.Error()
const expected2 = "failed to upload \"key\" to \"bucket\":\nfoo"
if errStr != expected2 {
t.Errorf("Expected %s, but received %s", expected2, errStr)
}

}

func TestBatchDeleteList(t *testing.T) {
count := 0

Expand Down

0 comments on commit 70713ce

Please sign in to comment.