Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion python/rpdk/java/templates/generate/HandlerWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package {{ package_name }};

import com.amazonaws.AmazonServiceException;
import software.amazon.awssdk.awscore.exception.AwsServiceException;
import software.amazon.awssdk.regions.PartitionMetadata;
import software.amazon.awssdk.regions.Region;
import software.amazon.cloudformation.Action;
Expand Down Expand Up @@ -100,7 +101,7 @@ public void testEntrypoint(
response = invokeHandler(proxy, payload.getRequest(), payload.getAction(), payload.getCallbackContext());
} catch (final BaseHandlerException e) {
response = ProgressEvent.defaultFailureHandler(e, e.getErrorCode());
} catch (final AmazonServiceException e) {
} catch (final AmazonServiceException | AwsServiceException e) {
response = ProgressEvent.defaultFailureHandler(e, HandlerErrorCode.GeneralServiceException);
} catch (final Throwable e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.json.JSONObject;
import org.json.JSONTokener;
import software.amazon.awssdk.awscore.exception.AwsServiceException;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.apache.ApacheHttpClient;
import software.amazon.awssdk.utils.StringUtils;
Expand Down Expand Up @@ -361,7 +362,7 @@ public void handleRequest(final InputStream inputStream, final OutputStream outp
publishExceptionMetric(request.getAction(), e, e.getErrorCode());
logUnhandledError(e.getMessage(), request, e);
return ProgressEvent.defaultFailureHandler(e, e.getErrorCode());
} catch (final AmazonServiceException e) {
} catch (final AmazonServiceException | AwsServiceException e) {
publishExceptionMetric(request.getAction(), e, HandlerErrorCode.GeneralServiceException);
logUnhandledError("A downstream service error occurred", request, e);
return ProgressEvent.defaultFailureHandler(e, HandlerErrorCode.GeneralServiceException);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.services.cloudwatchlogs.model.CloudWatchLogsException;
import software.amazon.cloudformation.exceptions.ResourceAlreadyExistsException;
import software.amazon.cloudformation.exceptions.ResourceNotFoundException;
import software.amazon.cloudformation.exceptions.TerminalException;
Expand Down Expand Up @@ -720,6 +721,40 @@ public void invokeHandler_throwsAmazonServiceException_returnsServiceException()
}
}

@Test
public void invokeHandler_throwsSDK2ServiceException_returnsServiceException() throws IOException {
wrapper.setInvokeHandlerException(CloudWatchLogsException.builder().build());

wrapper.setTransformResponse(resourceHandlerRequest);

try (final InputStream in = loadRequestStream("create.request.json");
final OutputStream out = new ByteArrayOutputStream()) {
final Context context = getLambdaContext();

wrapper.handleRequest(in, out, context);

// verify initialiseRuntime was called and initialised dependencies
verifyInitialiseRuntime();

// all metrics should be published, once for a single invocation
verify(providerMetricsPublisher, times(1)).publishInvocationMetric(any(Instant.class), eq(Action.CREATE));
verify(providerMetricsPublisher, times(1)).publishDurationMetric(any(Instant.class), eq(Action.CREATE), anyLong());

// failure metric should be published
verify(providerMetricsPublisher, times(1)).publishExceptionMetric(any(Instant.class), any(),
any(CloudWatchLogsException.class), any(HandlerErrorCode.class));

// verify that model validation occurred for CREATE/UPDATE/DELETE
verify(validator, times(1)).validateObject(any(JSONObject.class), any(JSONObject.class));

// verify output response
verifyHandlerResponse(out,
ProgressEvent.<TestModel, TestContext>builder().errorCode(HandlerErrorCode.GeneralServiceException)
.status(OperationStatus.FAILED)
.message("some error (Service: null; Status Code: 0; Error Code: null; Request ID: null)").build());
}
}

@Test
public void invokeHandler_throwsResourceAlreadyExistsException_returnsAlreadyExists() throws IOException {
// exceptions are caught consistently by LambdaWrapper
Expand Down