Skip to content

Commit 08f5aaf

Browse files
Added UnsupportedTarget error code (#406)
1 parent 98ccd95 commit 08f5aaf

File tree

5 files changed

+116
-0
lines changed

5 files changed

+116
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
package software.amazon.cloudformation.exceptions;
16+
17+
import software.amazon.cloudformation.proxy.HandlerErrorCode;
18+
19+
public class CfnUnsupportedTargetException extends BaseHandlerException {
20+
21+
private static final long serialVersionUID = -1646136434112354328L;
22+
private static final HandlerErrorCode ERROR_CODE = HandlerErrorCode.UnsupportedTarget;
23+
24+
public CfnUnsupportedTargetException(final Throwable cause) {
25+
super(cause, ERROR_CODE);
26+
}
27+
28+
public CfnUnsupportedTargetException(final String hookTypeName,
29+
final String targetTypeName) {
30+
this(hookTypeName, targetTypeName, null);
31+
}
32+
33+
public CfnUnsupportedTargetException(final String hookTypeName,
34+
final String targetTypeName,
35+
final Throwable cause) {
36+
super(String.format(ERROR_CODE.getMessage(), hookTypeName, targetTypeName), cause, ERROR_CODE);
37+
}
38+
}

src/main/java/software/amazon/cloudformation/exceptions/UnsupportedTargetException.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
*/
1515
package software.amazon.cloudformation.exceptions;
1616

17+
/**
18+
* Uses for this exception class should delegate instead to
19+
* CfnUnsupportedTargetException as it maps to UnsupportedTarget error code.
20+
* This deprecated exception maps to an InvalidRequest error code. Keeping the
21+
* same for backwards-compatibility
22+
*/
1723
public class UnsupportedTargetException extends CfnInvalidRequestException {
1824

1925
private static final long serialVersionUID = -1646136434112354328L;

src/main/java/software/amazon/cloudformation/proxy/ExceptionMessages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ final class ExceptionMessages {
3232
static final String INVALID_TYPECONFIGURATION = "Invalid TypeConfiguration provided for type '%s'. Reason: %s";
3333
static final String HANDLER_INTERNAL_FAILURE = "Internal error occurred in the handler.";
3434
static final String NON_COMPLIANT = "Hook of type '%s' returned a Non-Complaint status. Reason %s";
35+
static final String UNSUPPORTED_TARGET = "Hook of type '%s' received request for unsupported target '%s'.";
3536
static final String UNKNOWN = "Unknown error occurred.";
3637

3738
private ExceptionMessages() {

src/main/java/software/amazon/cloudformation/proxy/HandlerErrorCode.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ public enum HandlerErrorCode {
119119
*/
120120
NonCompliant(ExceptionMessages.NON_COMPLIANT),
121121

122+
/**
123+
* The specified target in the hook request is not supported. Applicable when
124+
* hook has wildcard targets. Hook wildcard may be matched to target that hook
125+
* did not support at time of registration
126+
*/
127+
UnsupportedTarget(ExceptionMessages.UNSUPPORTED_TARGET),
128+
122129
/**
123130
* the Hook has returned a failure for an Unknown reason. Only applicable to
124131
* Hook type handlers (terminal) Hook Handlers can return this when a hook has
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
package software.amazon.cloudformation.exceptions;
16+
17+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
18+
import org.junit.jupiter.api.Assertions;
19+
import org.junit.jupiter.api.Test;
20+
import software.amazon.cloudformation.proxy.HandlerErrorCode;
21+
22+
public class CfnUnsupportedTargetExceptionTests {
23+
private static final String HOOK_TYPE = "AWS::Hook::Type";
24+
private static final String UNSUPPORTED_TYPE = "AWS::Service::Resource";
25+
private static final String STATUS = "unsupported target";
26+
private static final String ERROR_MESSAGE = "something wrong";
27+
28+
@Test
29+
public void cfnUnsupportedTargetException_isBaseHandlerException() {
30+
assertThatExceptionOfType(BaseHandlerException.class).isThrownBy(() -> {
31+
throw new CfnUnsupportedTargetException(HOOK_TYPE, UNSUPPORTED_TYPE, new RuntimeException());
32+
}).withCauseInstanceOf(RuntimeException.class).withMessageContaining(HOOK_TYPE).withMessageContaining(UNSUPPORTED_TYPE)
33+
.withMessageContaining(STATUS);
34+
}
35+
36+
@Test
37+
public void cfnUnsupportedTargetException_singleArgsConstructorHasNoMessage() {
38+
assertThatExceptionOfType(CfnUnsupportedTargetException.class).isThrownBy(() -> {
39+
throw new CfnUnsupportedTargetException(new RuntimeException());
40+
}).withCauseInstanceOf(RuntimeException.class).withMessage(null);
41+
}
42+
43+
@Test
44+
public void cfnUnsupportedTargetException_noCauseGiven() {
45+
assertThatExceptionOfType(CfnUnsupportedTargetException.class).isThrownBy(() -> {
46+
throw new CfnUnsupportedTargetException(HOOK_TYPE, UNSUPPORTED_TYPE);
47+
}).withNoCause().withMessageContaining(HOOK_TYPE).withMessageContaining(UNSUPPORTED_TYPE).withMessageContaining(STATUS);
48+
}
49+
50+
@Test
51+
public void cfnUnsupportedTargetException_errorCodeIsAppropriate() {
52+
assertThatExceptionOfType(CfnUnsupportedTargetException.class).isThrownBy(() -> {
53+
throw new CfnUnsupportedTargetException(new RuntimeException());
54+
}).satisfies(exception -> Assertions.assertEquals(HandlerErrorCode.UnsupportedTarget, exception.getErrorCode()));
55+
}
56+
57+
@Test
58+
public void cfnUnsupportedTargetException_errorMessage() {
59+
assertThatExceptionOfType(CfnUnsupportedTargetException.class).isThrownBy(() -> {
60+
throw new CfnUnsupportedTargetException(new RuntimeException(ERROR_MESSAGE));
61+
}).satisfies(exception -> Assertions.assertEquals(ERROR_MESSAGE, exception.getMessage()));
62+
}
63+
64+
}

0 commit comments

Comments
 (0)