Skip to content

Adding additional information to logging and exception. #36

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/main/java/software/amazon/payloadoffloading/S3Dao.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ public String getTextFromS3(String s3BucketName, String s3Key) {
.key(s3Key)
.build();

ResponseInputStream<GetObjectResponse> object = null;
ResponseInputStream<GetObjectResponse> object;
try {
object = s3Client.getObject(getObjectRequest);
} catch (SdkException e) {
String errorMessage = "Failed to get the S3 object which contains the payload.";
String errorMessage = "Failed to get the S3 object from Bucket: {" + s3BucketName + "} with Key: {" + s3Key + "}.";
LOG.error(errorMessage, e);
throw SdkException.create(errorMessage, e);
}
Expand Down
16 changes: 15 additions & 1 deletion src/test/java/software/amazon/payloadoffloading/S3DaoTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package software.amazon.payloadoffloading;

import software.amazon.awssdk.core.exception.SdkException;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectAclRequest;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.ObjectCannedACL;

import org.junit.jupiter.api.BeforeEach;
Expand All @@ -13,14 +16,15 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class S3DaoTest {

private static final String s3ServerSideEncryptionKMSKeyId = "test-customer-managed-kms-key-id";
private static final String S3_BUCKET_NAME = "test-bucket-name";
private static final String ANY_PAYLOAD = "AnyPayload";
private static final String ANY_S3_KEY = "AnyS3key";
Expand Down Expand Up @@ -75,4 +79,14 @@ public void storeTextInS3WithBothTest() {
assertEquals(objectCannedACL, argument.getValue().acl());
assertEquals(S3_BUCKET_NAME, argument.getValue().bucket());
}

@Test
public void bucketNameAndKeyIncludedInExceptionWhenFailedToGetObject() {
dao = new S3Dao(s3Client);
when(s3Client.getObject(any(GetObjectRequest.class))).thenThrow(SdkException.create("Failed", new Exception()));

SdkException sdkException = assertThrows(SdkException.class, () -> dao.getTextFromS3(S3_BUCKET_NAME, ANY_S3_KEY));

assertEquals("Failed to get the S3 object from Bucket: {test-bucket-name} with Key: {AnyS3key}.", sdkException.getMessage());
}
}