Skip to content

Updates xml error unmarshalling to look at all request id keys when p… #2764

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

Merged
merged 4 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions .changes/next-release/bugfix-AWSSDKforJavav2-a6e2ed8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"category": "AWS SDK for Java v2",
"contributor": "",
"type": "bugfix",
"description": "Fixes a bug in XML error unmarshalling where error responses with empty body won't populate the requestId field. Affects Amazon S3 API calls such as Head object"
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import software.amazon.awssdk.awscore.exception.AwsServiceException;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.core.SdkPojo;
import software.amazon.awssdk.core.http.HttpResponseHandler;
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
import software.amazon.awssdk.core.interceptor.SdkExecutionAttribute;
import software.amazon.awssdk.http.SdkHttpFullResponse;
Expand All @@ -39,7 +40,6 @@
*/
@SdkInternalApi
public final class AwsXmlErrorUnmarshaller {
private static final String X_AMZN_REQUEST_ID_HEADER = "x-amzn-RequestId";
private static final String X_AMZ_ID_2_HEADER = "x-amz-id-2";

private final List<ExceptionMetadata> exceptions;
Expand Down Expand Up @@ -175,7 +175,16 @@ private String getRequestId(SdkHttpFullResponse response, XmlElement document) {
.orElse(document.getElementByName("RequestID"));
return requestId != null ?
requestId.textContent() :
response.firstMatchingHeader(X_AMZN_REQUEST_ID_HEADER).orElse(null);
matchRequestIdHeaders(response);
}

private String matchRequestIdHeaders(SdkHttpFullResponse response) {
return HttpResponseHandler.X_AMZN_REQUEST_ID_HEADERS.stream()
.map(h -> response.firstMatchingHeader(h))
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst()
.orElse(null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,19 @@ public void requestIdAndExtendedRequestIdInHeader_IsSetOnException() {
}
}

@Test
public void alternateRequestIdInHeader_IsSetOnException() {
stubFor(post(urlEqualTo(PATH)).willReturn(
aResponse()
.withStatus(404)
.withHeader("x-amz-request-id", "1234")));
try {
client.allTypes();
} catch (ProtocolQueryException e) {
assertThat(e.requestId()).isEqualTo("1234");
}
}

private void callAllTypes() {
client.allTypes(AllTypesRequest.builder().build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,19 @@ public void illegalArgumentException_emptyPathParam() {
IllegalArgumentException.class);
}

@Test
public void alternateRequestIdInHeader_IsSetOnException() {
stubFor(post(urlEqualTo(ALL_TYPES_PATH)).willReturn(
aResponse()
.withStatus(404)
.withHeader("x-amz-request-id", "1234")));
try {
client.allTypes();
} catch (ProtocolRestXmlException e) {
assertThat(e.requestId()).isEqualTo("1234");
}
}

private void callAllTypes() {
client.allTypes(AllTypesRequest.builder().build());
}
Expand Down