Skip to content

Commit 696e95d

Browse files
cenedhryndagnir
andauthored
Updates xml error unmarshalling to look at all request id keys when p… (#2764)
* Updates xml error unmarshalling to look at all request id keys when populating error information * Add tests * Checkstyle fixes Co-authored-by: Dongie Agnir <[email protected]> Co-authored-by: Dongie Agnir <[email protected]>
1 parent 1823c8f commit 696e95d

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"category": "AWS SDK for Java v2",
3+
"contributor": "",
4+
"type": "bugfix",
5+
"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"
6+
}

core/protocols/aws-query-protocol/src/main/java/software/amazon/awssdk/protocols/query/internal/unmarshall/AwsXmlErrorUnmarshaller.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import software.amazon.awssdk.awscore.exception.AwsServiceException;
2828
import software.amazon.awssdk.core.SdkBytes;
2929
import software.amazon.awssdk.core.SdkPojo;
30+
import software.amazon.awssdk.core.http.HttpResponseHandler;
3031
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
3132
import software.amazon.awssdk.core.interceptor.SdkExecutionAttribute;
3233
import software.amazon.awssdk.http.SdkHttpFullResponse;
@@ -39,7 +40,6 @@
3940
*/
4041
@SdkInternalApi
4142
public final class AwsXmlErrorUnmarshaller {
42-
private static final String X_AMZN_REQUEST_ID_HEADER = "x-amzn-RequestId";
4343
private static final String X_AMZ_ID_2_HEADER = "x-amz-id-2";
4444

4545
private final List<ExceptionMetadata> exceptions;
@@ -175,7 +175,16 @@ private String getRequestId(SdkHttpFullResponse response, XmlElement document) {
175175
.orElse(document.getElementByName("RequestID"));
176176
return requestId != null ?
177177
requestId.textContent() :
178-
response.firstMatchingHeader(X_AMZN_REQUEST_ID_HEADER).orElse(null);
178+
matchRequestIdHeaders(response);
179+
}
180+
181+
private String matchRequestIdHeaders(SdkHttpFullResponse response) {
182+
return HttpResponseHandler.X_AMZN_REQUEST_ID_HEADERS.stream()
183+
.map(h -> response.firstMatchingHeader(h))
184+
.filter(Optional::isPresent)
185+
.map(Optional::get)
186+
.findFirst()
187+
.orElse(null);
179188
}
180189

181190
/**

test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/exception/QueryExceptionTests.java

+13
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,19 @@ public void requestIdAndExtendedRequestIdInHeader_IsSetOnException() {
286286
}
287287
}
288288

289+
@Test
290+
public void alternateRequestIdInHeader_IsSetOnException() {
291+
stubFor(post(urlEqualTo(PATH)).willReturn(
292+
aResponse()
293+
.withStatus(404)
294+
.withHeader("x-amz-request-id", "1234")));
295+
try {
296+
client.allTypes();
297+
} catch (ProtocolQueryException e) {
298+
assertThat(e.requestId()).isEqualTo("1234");
299+
}
300+
}
301+
289302
private void callAllTypes() {
290303
client.allTypes(AllTypesRequest.builder().build());
291304
}

test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/exception/RestXmlExceptionTests.java

+13
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,19 @@ public void illegalArgumentException_emptyPathParam() {
216216
IllegalArgumentException.class);
217217
}
218218

219+
@Test
220+
public void alternateRequestIdInHeader_IsSetOnException() {
221+
stubFor(post(urlEqualTo(ALL_TYPES_PATH)).willReturn(
222+
aResponse()
223+
.withStatus(404)
224+
.withHeader("x-amz-request-id", "1234")));
225+
try {
226+
client.allTypes();
227+
} catch (ProtocolRestXmlException e) {
228+
assertThat(e.requestId()).isEqualTo("1234");
229+
}
230+
}
231+
219232
private void callAllTypes() {
220233
client.allTypes(AllTypesRequest.builder().build());
221234
}

0 commit comments

Comments
 (0)