Skip to content

Commit

Permalink
Add the cause of failure to the stacktrace for the CRT-based S3 client (
Browse files Browse the repository at this point in the history
  • Loading branch information
zoewangg authored Oct 27, 2023
1 parent b7eff6e commit e99bebf
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "feature",
"category": "AWS CRT-based S3 Client",
"contributor": "",
"description": "Add the cause of failure to the stacktrace if present for the AWS CRT-based S3 client. Related to [aws-crt-java#497](https://github.com/awslabs/aws-crt-java/issues/697)."
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
<rxjava.version>2.2.21</rxjava.version>
<commons-codec.verion>1.15</commons-codec.verion>
<jmh.version>1.29</jmh.version>
<awscrt.version>0.27.7</awscrt.version>
<awscrt.version>0.28.0</awscrt.version>

<!--Test dependencies -->
<junit5.version>5.10.0</junit5.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,8 @@ public int onResponseBody(ByteBuffer bodyBytesIn, long objectRangeStart, long ob
@Override
public void onFinished(S3FinishedResponseContext context) {
int crtCode = context.getErrorCode();
int responseStatus = context.getResponseStatus();
byte[] errorPayload = context.getErrorPayload();
if (crtCode != CRT.AWS_CRT_SUCCESS) {
handleError(crtCode, responseStatus, errorPayload);
handleError(context);
} else {
onSuccessfulResponseComplete();
}
Expand Down Expand Up @@ -127,13 +125,19 @@ public void cancelRequest() {
failResponseHandlerAndFuture(sdkClientException);
}

private void handleError(int crtCode, int responseStatus, byte[] errorPayload) {
private void handleError(S3FinishedResponseContext context) {
int crtCode = context.getErrorCode();
int responseStatus = context.getResponseStatus();
byte[] errorPayload = context.getErrorPayload();

if (isErrorResponse(responseStatus) && errorPayload != null) {
onErrorResponseComplete(errorPayload);
} else {
Throwable cause = context.getCause();

SdkClientException sdkClientException =
SdkClientException.create("Failed to send the request: " +
CRT.awsErrorString(crtCode));
CRT.awsErrorString(crtCode), cause);
failResponseHandlerAndFuture(sdkClientException);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@ public void requestFailed_shouldCompleteFutureExceptionally() {
verify(s3MetaRequest).close();
}

@Test
public void requestFailedWithCause_shouldCompleteFutureExceptionallyWithCause() {
RuntimeException cause = new RuntimeException("error");
S3FinishedResponseContext s3FinishedResponseContext = stubResponseContext(1, 0, null);
when(s3FinishedResponseContext.getCause()).thenReturn(cause);

responseHandlerAdapter.onFinished(s3FinishedResponseContext);
Throwable actualException = sdkResponseHandler.error;
String message = "Failed to send the request";
assertThat(actualException).isInstanceOf(SdkClientException.class).hasMessageContaining(message);
assertThat(future).isCompletedExceptionally();

assertThatThrownBy(() -> future.join()).hasRootCause(cause).hasMessageContaining(message);
verify(s3MetaRequest).close();
}

private S3FinishedResponseContext stubResponseContext(int errorCode, int responseStatus, byte[] errorPayload) {
Mockito.reset(context);
when(context.getErrorCode()).thenReturn(errorCode);
Expand Down

0 comments on commit e99bebf

Please sign in to comment.