Skip to content
Open
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-bfa9190.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "bugfix",
"category": "AWS SDK for Java v2",
"contributor": "",
"description": "Don't use the value of AwsQueryError in json rpc/smithy-rpc-v2-cbor protocols."
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,14 @@ private String getErrorCode(String errorShapeName) {
}

private boolean isErrorCodeOverridden(ErrorTrait errorTrait) {
return errorTrait != null && !Utils.isNullOrEmpty(errorTrait.getCode());
return protocolSupportsErrorCodeOverride() && errorTrait != null && !Utils.isNullOrEmpty(errorTrait.getCode());
}

// JSON (ie non-rest) and smithy-rpc-v2-cbor should ignore the AwsQueryError trait
// error code on the deserialized exceptions is parsed based on the awsQueryCompatible trait in:
// AwsJsonProtocolErrorUnmarshaller#getEffectiveErrorCode
private boolean protocolSupportsErrorCodeOverride() {
return !getProtocol().equals("json") && !getProtocol().equals("smithy-rpc-v2-cbor");
}

private Integer getHttpStatusCode(String errorShapeName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ public CompletableFuture<APostOperationResponse> aPostOperation(APostOperationRe
return Optional.empty();
}
switch (errorCode) {
case "InvalidInput":
return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400)
case "InvalidInputException":
return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400)
.exceptionBuilderSupplier(InvalidInputException::builder).build());
default:
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ public APostOperationResponse aPostOperation(APostOperationRequest aPostOperatio
return Optional.empty();
}
switch (errorCode) {
case "InvalidInput":
return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400)
case "InvalidInputException":
return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400)
.exceptionBuilderSupplier(InvalidInputException::builder).build());
default:
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.isIn;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
Expand Down Expand Up @@ -59,9 +60,11 @@
import software.amazon.awssdk.services.cloudwatch.model.Metric;
import software.amazon.awssdk.services.cloudwatch.model.MetricAlarm;
import software.amazon.awssdk.services.cloudwatch.model.MetricDatum;
import software.amazon.awssdk.services.cloudwatch.model.MissingRequiredParameterException;
import software.amazon.awssdk.services.cloudwatch.model.PutMetricAlarmRequest;
import software.amazon.awssdk.services.cloudwatch.model.PutMetricDataRequest;
import software.amazon.awssdk.services.cloudwatch.model.StateValue;
import software.amazon.awssdk.services.cloudwatch.model.Statistic;
import software.amazon.awssdk.testutils.Waiter;
import software.amazon.awssdk.testutils.service.AwsIntegrationTestBase;

Expand Down Expand Up @@ -374,6 +377,23 @@ public void testExceptionHandling() throws Exception {
}
}

@Test
public void testQueryCompatibleExceptionHandling() {
try {
cloudwatch.getMetricStatistics(GetMetricStatisticsRequest.builder().namespace("foo").statistics(Statistic.AVERAGE).build());
fail("Expected a MissingRequiredParameterException, but wasn't thrown");
} catch (MissingRequiredParameterException e) {
// There is a strong contract on the value of these fields, and they should never change unexpectedly
assertEquals("MissingParameter", e.awsErrorDetails().errorCode());
assertEquals(400, e.statusCode());

// There is no strong contract on the exact value of these fields, but they should always be at least present
assertFalse(e.requestId().isEmpty());
assertFalse(e.awsErrorDetails().serviceName().isEmpty());
assertFalse(e.getMessage().isEmpty());
}
}

/**
* In the following test, we purposely setting the time offset to trigger a clock skew error.
* The time offset must be fixed and then we validate the global value for time offset has been
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package software.amazon.awssdk.protocol.asserts.unmarshalling;


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.unitils.reflectionassert.ReflectionAssert.assertReflectionEquals;

Expand All @@ -39,6 +40,7 @@ protected void doAssert(UnmarshallingTestContext context, Object actual) throws
}
SdkServiceException actualException = (SdkServiceException) actual;
SdkServiceException expectedException = createExpectedResult(context);
assertEquals(expectedException.getClass(), actualException.getClass());
for (Field field : expectedException.getClass().getDeclaredFields()) {
assertFieldEquals(field, actualException, expectedException);
}
Expand Down
Loading