diff --git a/src/main/java/software/amazon/cloudformation/exceptions/CfnNotStabilizedException.java b/src/main/java/software/amazon/cloudformation/exceptions/CfnNotStabilizedException.java index 071e2681..d1cf6427 100644 --- a/src/main/java/software/amazon/cloudformation/exceptions/CfnNotStabilizedException.java +++ b/src/main/java/software/amazon/cloudformation/exceptions/CfnNotStabilizedException.java @@ -25,14 +25,54 @@ public CfnNotStabilizedException(final Throwable cause) { super(cause, ERROR_CODE); } + public CfnNotStabilizedException(final String resourceTypeName, + final String resourceIdentifier, + final String reason) { + this(resourceTypeName, resourceIdentifier, reason, null); + } + + /** + * @param resourceTypeName + * @param resourceIdentifier + * @param reason Reason why the resource did not stabilize. This should include the current + * state. For AWS resources, this should include the relevant API Action name in + * IAM format and the RequestId + * + * Example: "Current Status: modifying (API: rds:DescribeDBClusters, + * RequestId: d682b02c-1383-11b4-a6bb-172dfac7f170)" + * @param cause + */ + public CfnNotStabilizedException(final String resourceTypeName, + final String resourceIdentifier, + final String reason, + final Throwable cause) { + super(String.format(ERROR_CODE.getMessage(), resourceTypeName, resourceIdentifier, reason), + cause, ERROR_CODE); + } + + /** + * use {@link #CfnNotStabilizedException(String, String, String)} + * + * @param resourceTypeName + * @param resourceIdentifier + */ + @Deprecated public CfnNotStabilizedException(final String resourceTypeName, final String resourceIdentifier) { - this(resourceTypeName, resourceIdentifier, null); + this(resourceTypeName, resourceIdentifier, (Throwable) null); } + /** + * use {@link #CfnNotStabilizedException(String, String, String, Throwable)} + * + * @param resourceTypeName + * @param resourceIdentifier + * @param cause + */ + @Deprecated public CfnNotStabilizedException(final String resourceTypeName, final String resourceIdentifier, final Throwable cause) { - super(String.format(ERROR_CODE.getMessage(), resourceTypeName, resourceIdentifier), cause, ERROR_CODE); + this(resourceTypeName, resourceIdentifier, "", cause); } } diff --git a/src/main/java/software/amazon/cloudformation/proxy/ExceptionMessages.java b/src/main/java/software/amazon/cloudformation/proxy/ExceptionMessages.java index f9d0d101..4616d6c8 100644 --- a/src/main/java/software/amazon/cloudformation/proxy/ExceptionMessages.java +++ b/src/main/java/software/amazon/cloudformation/proxy/ExceptionMessages.java @@ -23,7 +23,7 @@ final class ExceptionMessages { static final String INVALID_REQUEST = "Invalid request provided: %s"; static final String NETWORK_FAILURE = "Network failure occurred during operation '%s'."; static final String NOT_FOUND = "Resource of type '%s' with identifier '%s' was not found."; - static final String NOT_STABILIZED = "Resource of type '%s' with identifier '%s' did not stabilize."; + static final String NOT_STABILIZED = "Resource of type '%s' with identifier '%s' did not stabilize. %s"; static final String NOT_UPDATABLE = "Resource of type '%s' with identifier '%s' is not updatable with parameters provided."; static final String RESOURCE_CONFLICT = "Resource of type '%s' with identifier '%s' has a conflict. Reason: %s."; static final String SERVICE_INTERNAL_ERROR = "Internal error reported from downstream service during operation '%s'."; diff --git a/src/test/java/software/amazon/cloudformation/exceptions/CfnNotStabilizedExceptionTests.java b/src/test/java/software/amazon/cloudformation/exceptions/CfnNotStabilizedExceptionTests.java index 4c9ec051..8bbaa6bc 100644 --- a/src/test/java/software/amazon/cloudformation/exceptions/CfnNotStabilizedExceptionTests.java +++ b/src/test/java/software/amazon/cloudformation/exceptions/CfnNotStabilizedExceptionTests.java @@ -23,9 +23,9 @@ public class CfnNotStabilizedExceptionTests { @Test public void cfnNotStabilizedException_isBaseHandlerException() { assertThatExceptionOfType(BaseHandlerException.class).isThrownBy(() -> { - throw new CfnNotStabilizedException("AWS::Type::Resource", "myId", new RuntimeException()); + throw new CfnNotStabilizedException("AWS::Type::Resource", "myId", "Reason", new RuntimeException()); }).withCauseInstanceOf(RuntimeException.class).withMessageContaining("AWS::Type::Resource").withMessageContaining("myId") - .withMessageContaining("not stabilize"); + .withMessageContaining("not stabilize").withMessageContaining("Reason"); } @Test @@ -38,9 +38,9 @@ public void cfnNotStabilizedException_singleArgConstructorHasNoMessage() { @Test public void cfnNotStabilizedException_noCauseGiven() { assertThatExceptionOfType(CfnNotStabilizedException.class).isThrownBy(() -> { - throw new CfnNotStabilizedException("AWS::Type::Resource", "myId"); + throw new CfnNotStabilizedException("AWS::Type::Resource", "myId", "Reason"); }).withNoCause().withMessageContaining("AWS::Type::Resource").withMessageContaining("myId") - .withMessageContaining("not stabilize"); + .withMessageContaining("not stabilize").withMessageContaining("Reason"); } @Test @@ -56,4 +56,22 @@ public void cfnNotStabilizedException_errorMessage() { throw new CfnNotStabilizedException(new RuntimeException("something wrong")); }).satisfies(exception -> assertEquals("something wrong", exception.getMessage())); } + + @Deprecated + @Test + public void cfnNotStabilizedExceptionWithoutReason_isBaseHandlerException() { + assertThatExceptionOfType(BaseHandlerException.class).isThrownBy(() -> { + throw new CfnNotStabilizedException("AWS::Type::Resource", "myId", new RuntimeException()); + }).withCauseInstanceOf(RuntimeException.class).withMessageContaining("AWS::Type::Resource").withMessageContaining("myId") + .withMessageContaining("not stabilize"); + } + + @Deprecated + @Test + public void cfnNotStabilizedExceptionWithoutReason_noCauseGiven() { + assertThatExceptionOfType(CfnNotStabilizedException.class).isThrownBy(() -> { + throw new CfnNotStabilizedException("AWS::Type::Resource", "myId"); + }).withNoCause().withMessageContaining("AWS::Type::Resource").withMessageContaining("myId") + .withMessageContaining("not stabilize"); + } } diff --git a/tests/test_codegen.py b/tests/test_codegen.py index 3c7d985c..fc13b883 100644 --- a/tests/test_codegen.py +++ b/tests/test_codegen.py @@ -177,9 +177,8 @@ def test_generate_without_java_plugin_in_pom_should_not_fail(project): def test__get_plugin_version_invalid_pom(project): - pom = open(project.root / "pom.xml", "w") - pom.write("invalid pom") - pom.close() + with open(project.root / "pom.xml", "w") as pom: + pom.write("invalid pom") with pytest.raises(InvalidMavenPOMError): project._plugin._get_java_plugin_dependency_version(project)