From 0283fa8bb1b09ef8cf1e4493fc66dedaed14f207 Mon Sep 17 00:00:00 2001 From: Saranya Somepalli Date: Tue, 20 May 2025 13:21:59 -0700 Subject: [PATCH 1/2] Update non-streaming error unmarshaller with the new mapping function --- .../bugfix-AWSSDKforJavav2-b6c94e2.json | 6 + .../poet/client/specs/JsonProtocolSpec.java | 56 +- .../sra/test-aws-json-async-client-class.java | 744 +++++++++------ .../sra/test-cbor-async-client-class.java | 744 +++++++++------ .../sra/test-json-async-client-class.java | 871 ++++++++++------- .../client/sra/test-json-client-class.java | 219 ++++- .../test-aws-json-async-client-class.java | 754 +++++++++------ ...ry-compatible-json-async-client-class.java | 62 +- ...ery-compatible-json-sync-client-class.java | 33 +- .../poet/client/test-batchmanager-async.java | 49 +- .../client/test-cbor-async-client-class.java | 754 +++++++++------ .../poet/client/test-cbor-client-class.java | 555 +++++++---- .../poet/client/test-custompackage-async.java | 51 +- .../poet/client/test-custompackage-sync.java | 19 +- .../test-customservicemetadata-async.java | 51 +- .../test-customservicemetadata-sync.java | 19 +- .../client/test-endpoint-discovery-async.java | 231 +++-- .../client/test-endpoint-discovery-sync.java | 191 ++-- .../client/test-json-async-client-class.java | 879 +++++++++++------- .../poet/client/test-json-client-class.java | 219 ++++- .../client/test-rpcv2-async-client-class.java | 591 ++++++++---- .../codegen/poet/client/test-rpcv2-sync.java | 294 +++++- ...gned-payload-trait-async-client-class.java | 466 ++++++---- ...igned-payload-trait-sync-client-class.java | 469 ++++++---- .../kinesis/KinesisExceptionTest.java | 108 +++ 25 files changed, 5401 insertions(+), 3034 deletions(-) create mode 100644 .changes/next-release/bugfix-AWSSDKforJavav2-b6c94e2.json create mode 100644 services/kinesis/src/test/java/software/amazon/awssdk/services/kinesis/KinesisExceptionTest.java diff --git a/.changes/next-release/bugfix-AWSSDKforJavav2-b6c94e2.json b/.changes/next-release/bugfix-AWSSDKforJavav2-b6c94e2.json new file mode 100644 index 000000000000..ed664db81e5c --- /dev/null +++ b/.changes/next-release/bugfix-AWSSDKforJavav2-b6c94e2.json @@ -0,0 +1,6 @@ +{ + "type": "bugfix", + "category": "AWS SDK for Java v2", + "contributor": "", + "description": "Update non-streaming error unmarshalling to properly unmarshall exceptions to their expected types." +} diff --git a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/client/specs/JsonProtocolSpec.java b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/client/specs/JsonProtocolSpec.java index 9fa214efb8fe..5d4ef551216d 100644 --- a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/client/specs/JsonProtocolSpec.java +++ b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/client/specs/JsonProtocolSpec.java @@ -39,6 +39,7 @@ import software.amazon.awssdk.codegen.model.intermediate.OperationModel; import software.amazon.awssdk.codegen.model.intermediate.Protocol; import software.amazon.awssdk.codegen.model.intermediate.ShapeModel; +import software.amazon.awssdk.codegen.model.intermediate.ShapeType; import software.amazon.awssdk.codegen.poet.PoetExtension; import software.amazon.awssdk.codegen.poet.auth.scheme.AuthSchemeSpecUtils; import software.amazon.awssdk.codegen.poet.client.traits.HttpChecksumRequiredTrait; @@ -116,7 +117,6 @@ public MethodSpec initProtocolFactory(IntermediateModel model) { methodSpec.addCode("$L", hasAwsQueryCompatible()); } - registerModeledExceptions(model, poetExtensions).forEach(methodSpec::addCode); methodSpec.addCode(";"); return methodSpec.build(); @@ -170,11 +170,40 @@ public CodeBlock responseHandler(IntermediateModel model, OperationModel opModel public Optional errorResponseHandler(OperationModel opModel) { String protocolFactory = protocolFactoryLiteral(model, opModel); - return Optional.of( - CodeBlock.builder() - .add("\n\n$T<$T> errorResponseHandler = createErrorResponseHandler($L, operationMetadata);", - HttpResponseHandler.class, AwsServiceException.class, protocolFactory) - .build()); + CodeBlock.Builder builder = CodeBlock.builder(); + ParameterizedTypeName metadataMapperType = ParameterizedTypeName.get( + ClassName.get(Function.class), + ClassName.get(String.class), + ParameterizedTypeName.get(Optional.class, ExceptionMetadata.class)); + + builder.add("\n$T exceptionMetadataMapper = errorCode -> {\n", metadataMapperType); + builder.add("if (errorCode == null) {\n"); + builder.add("return $T.empty();\n", Optional.class); + builder.add("}\n"); + builder.add("switch (errorCode) {\n"); + model.getShapes().values().stream() + .filter(shape -> shape.getShapeType() == ShapeType.Exception) + .forEach(exceptionShape -> { + String exceptionName = exceptionShape.getShapeName(); + String errorCode = exceptionShape.getErrorCode(); + + builder.add("case $S:\n", errorCode); + builder.add("return $T.of($T.builder()\n", Optional.class, ExceptionMetadata.class) + .add(".errorCode($S)\n", errorCode); + builder.add(populateHttpStatusCode(exceptionShape, model)); + builder.add(".exceptionBuilderSupplier($T::builder)\n", + poetExtensions.getModelClassFromShape(exceptionShape)) + .add(".build());\n"); + }); + + builder.add("default: return $T.empty();\n", Optional.class); + builder.add("}\n"); + builder.add("};\n"); + + builder.add("$T<$T> errorResponseHandler = createErrorResponseHandler($L, operationMetadata, exceptionMetadataMapper);", + HttpResponseHandler.class, AwsServiceException.class, protocolFactory); + + return Optional.of(builder.build()); } @Override @@ -411,21 +440,6 @@ public Optional createErrorResponseHandler() { ClassName httpResponseHandler = ClassName.get(HttpResponseHandler.class); ClassName sdkBaseException = ClassName.get(AwsServiceException.class); TypeName responseHandlerOfException = ParameterizedTypeName.get(httpResponseHandler, sdkBaseException); - - return Optional.of(MethodSpec.methodBuilder("createErrorResponseHandler") - .addParameter(BaseAwsJsonProtocolFactory.class, "protocolFactory") - .addParameter(JsonOperationMetadata.class, "operationMetadata") - .returns(responseHandlerOfException) - .addModifiers(Modifier.PRIVATE) - .addStatement("return protocolFactory.createErrorResponseHandler(operationMetadata)") - .build()); - } - - @Override - public Optional createEventstreamErrorResponseHandler() { - ClassName httpResponseHandler = ClassName.get(HttpResponseHandler.class); - ClassName sdkBaseException = ClassName.get(AwsServiceException.class); - TypeName responseHandlerOfException = ParameterizedTypeName.get(httpResponseHandler, sdkBaseException); ParameterizedTypeName mapperType = ParameterizedTypeName.get(ClassName.get(Function.class), ClassName.get(String.class), ParameterizedTypeName.get(Optional.class, ExceptionMetadata.class)); diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/sra/test-aws-json-async-client-class.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/sra/test-aws-json-async-client-class.java index 7298068aa6ad..e4a6f0f5d2ab 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/sra/test-aws-json-async-client-class.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/sra/test-aws-json-async-client-class.java @@ -128,7 +128,7 @@ final class DefaultJsonAsyncClient implements JsonAsyncClient { private static final Logger log = LoggerFactory.getLogger(DefaultJsonAsyncClient.class); private static final AwsProtocolMetadata protocolMetadata = AwsProtocolMetadata.builder() - .serviceProtocol(AwsServiceProtocol.AWS_JSON).build(); + .serviceProtocol(AwsServiceProtocol.AWS_JSON).build(); private final AsyncClientHandler clientHandler; @@ -178,32 +178,46 @@ public JsonUtilities utilities() { public CompletableFuture aPostOperation(APostOperationRequest aPostOperationRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(aPostOperationRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, aPostOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "APostOperation"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, APostOperationResponse::builder); - + operationMetadata, APostOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); String hostPrefix = "{StringMember}-foo."; HostnameValidator.validateHostnameCompliant(aPostOperationRequest.stringMember(), "StringMember", - "aPostOperationRequest"); + "aPostOperationRequest"); String resolvedHostExpression = String.format("%s-foo.", aPostOperationRequest.stringMember()); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("APostOperation").withProtocolMetadata(protocolMetadata) - .withMarshaller(new APostOperationRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .hostPrefixExpression(resolvedHostExpression).withInput(aPostOperationRequest)); + .execute(new ClientExecutionParams() + .withOperationName("APostOperation").withProtocolMetadata(protocolMetadata) + .withMarshaller(new APostOperationRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .hostPrefixExpression(resolvedHostExpression).withInput(aPostOperationRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -242,32 +256,46 @@ public CompletableFuture aPostOperation(APostOperationRe */ @Override public CompletableFuture aPostOperationWithOutput( - APostOperationWithOutputRequest aPostOperationWithOutputRequest) { + APostOperationWithOutputRequest aPostOperationWithOutputRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(aPostOperationWithOutputRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, aPostOperationWithOutputRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "APostOperationWithOutput"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, APostOperationWithOutputResponse::builder); - + operationMetadata, APostOperationWithOutputResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("APostOperationWithOutput").withProtocolMetadata(protocolMetadata) - .withMarshaller(new APostOperationWithOutputRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(aPostOperationWithOutputRequest)); + .execute(new ClientExecutionParams() + .withOperationName("APostOperationWithOutput").withProtocolMetadata(protocolMetadata) + .withMarshaller(new APostOperationWithOutputRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(aPostOperationWithOutputRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -301,66 +329,81 @@ public CompletableFuture aPostOperationWithOut */ @Override public CompletableFuture eventStreamOperation(EventStreamOperationRequest eventStreamOperationRequest, - Publisher requestStream, EventStreamOperationResponseHandler asyncResponseHandler) { + Publisher requestStream, EventStreamOperationResponseHandler asyncResponseHandler) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(eventStreamOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, eventStreamOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "EventStreamOperation"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = new AttachHttpMetadataResponseHandler( - protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationResponse::builder)); + protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationResponse::builder)); HttpResponseHandler voidResponseHandler = protocolFactory.createResponseHandler(JsonOperationMetadata - .builder().isPayloadJson(false).hasStreamingSuccessResponse(true).build(), VoidSdkResponse::builder); + .builder().isPayloadJson(false).hasStreamingSuccessResponse(true).build(), VoidSdkResponse::builder); HttpResponseHandler eventResponseHandler = protocolFactory.createResponseHandler( - JsonOperationMetadata.builder().isPayloadJson(true).hasStreamingSuccessResponse(false).build(), - EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventOne", EventStream::eventOneBuilder) - .putSdkPojoSupplier("EventTheSecond", EventStream::eventTheSecondBuilder) - .putSdkPojoSupplier("secondEventOne", EventStream::secondEventOneBuilder) - .putSdkPojoSupplier("eventThree", EventStream::eventThreeBuilder) - .defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN)).build()); + JsonOperationMetadata.builder().isPayloadJson(true).hasStreamingSuccessResponse(false).build(), + EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventOne", EventStream::eventOneBuilder) + .putSdkPojoSupplier("EventTheSecond", EventStream::eventTheSecondBuilder) + .putSdkPojoSupplier("secondEventOne", EventStream::secondEventOneBuilder) + .putSdkPojoSupplier("eventThree", EventStream::eventThreeBuilder) + .defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN)).build()); Function> eventstreamExceptionMetadataMapper = errorCode -> { switch (errorCode) { - default: - return Optional.empty(); + default: + return Optional.empty(); } }; HttpResponseHandler errorEventResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata, eventstreamExceptionMetadataMapper); + operationMetadata, eventstreamExceptionMetadataMapper); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); EventStreamTaggedUnionJsonMarshaller eventMarshaller = EventStreamTaggedUnionJsonMarshaller.builder() - .putMarshaller(DefaultInputEvent.class, new InputEventMarshaller(protocolFactory)).build(); + .putMarshaller(DefaultInputEvent.class, new InputEventMarshaller(protocolFactory)).build(); SdkPublisher eventPublisher = SdkPublisher.adapt(requestStream); Publisher adapted = eventPublisher.map(event -> eventMarshaller.marshall(event)).map( - AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer); + AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer); CompletableFuture future = new CompletableFuture<>(); EventStreamAsyncResponseTransformer asyncResponseTransformer = EventStreamAsyncResponseTransformer - . builder().eventStreamResponseHandler(asyncResponseHandler) - .eventResponseHandler(eventResponseHandler).initialResponseHandler(responseHandler) - .exceptionResponseHandler(errorEventResponseHandler).future(future).executor(executor) - .serviceName(serviceName()).build(); + . builder().eventStreamResponseHandler(asyncResponseHandler) + .eventResponseHandler(eventResponseHandler).initialResponseHandler(responseHandler) + .exceptionResponseHandler(errorEventResponseHandler).future(future).executor(executor) + .serviceName(serviceName()).build(); CompletableFuture executeFuture = clientHandler.execute( - new ClientExecutionParams() - .withOperationName("EventStreamOperation").withProtocolMetadata(protocolMetadata) - .withMarshaller(new EventStreamOperationRequestMarshaller(protocolFactory)) - .withAsyncRequestBody(AsyncRequestBody.fromPublisher(adapted)).withFullDuplex(true) - .withInitialRequestEvent(true).withResponseHandler(voidResponseHandler) - .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector).withInput(eventStreamOperationRequest), - asyncResponseTransformer); + new ClientExecutionParams() + .withOperationName("EventStreamOperation").withProtocolMetadata(protocolMetadata) + .withMarshaller(new EventStreamOperationRequestMarshaller(protocolFactory)) + .withAsyncRequestBody(AsyncRequestBody.fromPublisher(adapted)).withFullDuplex(true) + .withInitialRequestEvent(true).withResponseHandler(voidResponseHandler) + .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector).withInput(eventStreamOperationRequest), + asyncResponseTransformer); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { if (e != null) { try { @@ -375,7 +418,7 @@ public CompletableFuture eventStreamOperation(EventStreamOperationRequest return CompletableFutureUtils.forwardExceptionTo(future, executeFuture); } catch (Throwable t) { runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> asyncResponseHandler.exceptionOccurred(t)); + () -> asyncResponseHandler.exceptionOccurred(t)); metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); return CompletableFutureUtils.failedFuture(t); } @@ -404,40 +447,54 @@ public CompletableFuture eventStreamOperation(EventStreamOperationRequest */ @Override public CompletableFuture eventStreamOperationWithOnlyInput( - EventStreamOperationWithOnlyInputRequest eventStreamOperationWithOnlyInputRequest, - Publisher requestStream) { + EventStreamOperationWithOnlyInputRequest eventStreamOperationWithOnlyInputRequest, + Publisher requestStream) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(eventStreamOperationWithOnlyInputRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - eventStreamOperationWithOnlyInputRequest.overrideConfiguration().orElse(null)); + eventStreamOperationWithOnlyInputRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "EventStreamOperationWithOnlyInput"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory - .createResponseHandler(operationMetadata, EventStreamOperationWithOnlyInputResponse::builder); - + .createResponseHandler(operationMetadata, EventStreamOperationWithOnlyInputResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); EventStreamTaggedUnionJsonMarshaller eventMarshaller = EventStreamTaggedUnionJsonMarshaller.builder() - .putMarshaller(DefaultInputEventOne.class, new InputEventMarshaller(protocolFactory)) - .putMarshaller(DefaultInputEventTwo.class, new InputEventTwoMarshaller(protocolFactory)).build(); + .putMarshaller(DefaultInputEventOne.class, new InputEventMarshaller(protocolFactory)) + .putMarshaller(DefaultInputEventTwo.class, new InputEventTwoMarshaller(protocolFactory)).build(); SdkPublisher eventPublisher = SdkPublisher.adapt(requestStream); Publisher adapted = eventPublisher.map(event -> eventMarshaller.marshall(event)).map( - AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer); + AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("EventStreamOperationWithOnlyInput").withProtocolMetadata(protocolMetadata) - .withMarshaller(new EventStreamOperationWithOnlyInputRequestMarshaller(protocolFactory)) - .withAsyncRequestBody(AsyncRequestBody.fromPublisher(adapted)).withInitialRequestEvent(true) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(eventStreamOperationWithOnlyInputRequest)); + .execute(new ClientExecutionParams() + .withOperationName("EventStreamOperationWithOnlyInput").withProtocolMetadata(protocolMetadata) + .withMarshaller(new EventStreamOperationWithOnlyInputRequestMarshaller(protocolFactory)) + .withAsyncRequestBody(AsyncRequestBody.fromPublisher(adapted)).withInitialRequestEvent(true) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(eventStreamOperationWithOnlyInputRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -472,60 +529,75 @@ public CompletableFuture eventStreamO */ @Override public CompletableFuture eventStreamOperationWithOnlyOutput( - EventStreamOperationWithOnlyOutputRequest eventStreamOperationWithOnlyOutputRequest, - EventStreamOperationWithOnlyOutputResponseHandler asyncResponseHandler) { + EventStreamOperationWithOnlyOutputRequest eventStreamOperationWithOnlyOutputRequest, + EventStreamOperationWithOnlyOutputResponseHandler asyncResponseHandler) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(eventStreamOperationWithOnlyOutputRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - eventStreamOperationWithOnlyOutputRequest.overrideConfiguration().orElse(null)); + eventStreamOperationWithOnlyOutputRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "EventStreamOperationWithOnlyOutput"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = new AttachHttpMetadataResponseHandler( - protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationWithOnlyOutputResponse::builder)); + protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationWithOnlyOutputResponse::builder)); HttpResponseHandler voidResponseHandler = protocolFactory.createResponseHandler(JsonOperationMetadata - .builder().isPayloadJson(false).hasStreamingSuccessResponse(true).build(), VoidSdkResponse::builder); + .builder().isPayloadJson(false).hasStreamingSuccessResponse(true).build(), VoidSdkResponse::builder); HttpResponseHandler eventResponseHandler = protocolFactory.createResponseHandler( - JsonOperationMetadata.builder().isPayloadJson(true).hasStreamingSuccessResponse(false).build(), - EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventOne", EventStream::eventOneBuilder) - .putSdkPojoSupplier("EventTheSecond", EventStream::eventTheSecondBuilder) - .putSdkPojoSupplier("secondEventOne", EventStream::secondEventOneBuilder) - .putSdkPojoSupplier("eventThree", EventStream::eventThreeBuilder) - .defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN)).build()); + JsonOperationMetadata.builder().isPayloadJson(true).hasStreamingSuccessResponse(false).build(), + EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventOne", EventStream::eventOneBuilder) + .putSdkPojoSupplier("EventTheSecond", EventStream::eventTheSecondBuilder) + .putSdkPojoSupplier("secondEventOne", EventStream::secondEventOneBuilder) + .putSdkPojoSupplier("eventThree", EventStream::eventThreeBuilder) + .defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN)).build()); Function> eventstreamExceptionMetadataMapper = errorCode -> { switch (errorCode) { - default: - return Optional.empty(); + default: + return Optional.empty(); } }; HttpResponseHandler errorEventResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata, eventstreamExceptionMetadataMapper); + operationMetadata, eventstreamExceptionMetadataMapper); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture future = new CompletableFuture<>(); EventStreamAsyncResponseTransformer asyncResponseTransformer = EventStreamAsyncResponseTransformer - . builder() - .eventStreamResponseHandler(asyncResponseHandler).eventResponseHandler(eventResponseHandler) - .initialResponseHandler(responseHandler).exceptionResponseHandler(errorEventResponseHandler).future(future) - .executor(executor).serviceName(serviceName()).build(); + . builder() + .eventStreamResponseHandler(asyncResponseHandler).eventResponseHandler(eventResponseHandler) + .initialResponseHandler(responseHandler).exceptionResponseHandler(errorEventResponseHandler).future(future) + .executor(executor).serviceName(serviceName()).build(); CompletableFuture executeFuture = clientHandler.execute( - new ClientExecutionParams() - .withOperationName("EventStreamOperationWithOnlyOutput").withProtocolMetadata(protocolMetadata) - .withMarshaller(new EventStreamOperationWithOnlyOutputRequestMarshaller(protocolFactory)) - .withResponseHandler(voidResponseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(eventStreamOperationWithOnlyOutputRequest), asyncResponseTransformer); + new ClientExecutionParams() + .withOperationName("EventStreamOperationWithOnlyOutput").withProtocolMetadata(protocolMetadata) + .withMarshaller(new EventStreamOperationWithOnlyOutputRequestMarshaller(protocolFactory)) + .withResponseHandler(voidResponseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(eventStreamOperationWithOnlyOutputRequest), asyncResponseTransformer); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { if (e != null) { try { @@ -540,7 +612,7 @@ public CompletableFuture eventStreamOperationWithOnlyOutput( return CompletableFutureUtils.forwardExceptionTo(future, executeFuture); } catch (Throwable t) { runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> asyncResponseHandler.exceptionOccurred(t)); + () -> asyncResponseHandler.exceptionOccurred(t)); metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); return CompletableFutureUtils.failedFuture(t); } @@ -572,32 +644,46 @@ public CompletableFuture eventStreamOperationWithOnlyOutput( */ @Override public CompletableFuture getWithoutRequiredMembers( - GetWithoutRequiredMembersRequest getWithoutRequiredMembersRequest) { + GetWithoutRequiredMembersRequest getWithoutRequiredMembersRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(getWithoutRequiredMembersRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, getWithoutRequiredMembersRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "GetWithoutRequiredMembers"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, GetWithoutRequiredMembersResponse::builder); - + operationMetadata, GetWithoutRequiredMembersResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("GetWithoutRequiredMembers").withProtocolMetadata(protocolMetadata) - .withMarshaller(new GetWithoutRequiredMembersRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(getWithoutRequiredMembersRequest)); + .execute(new ClientExecutionParams() + .withOperationName("GetWithoutRequiredMembers").withProtocolMetadata(protocolMetadata) + .withMarshaller(new GetWithoutRequiredMembersRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(getWithoutRequiredMembersRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -632,36 +718,50 @@ public CompletableFuture getWithoutRequiredMe */ @Override public CompletableFuture operationWithChecksumRequired( - OperationWithChecksumRequiredRequest operationWithChecksumRequiredRequest) { + OperationWithChecksumRequiredRequest operationWithChecksumRequiredRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(operationWithChecksumRequiredRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - operationWithChecksumRequiredRequest.overrideConfiguration().orElse(null)); + operationWithChecksumRequiredRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "OperationWithChecksumRequired"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OperationWithChecksumRequiredResponse::builder); - + operationMetadata, OperationWithChecksumRequiredResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("OperationWithChecksumRequired") - .withProtocolMetadata(protocolMetadata) - .withMarshaller(new OperationWithChecksumRequiredRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector) - .putExecutionAttribute(SdkInternalExecutionAttribute.HTTP_CHECKSUM_REQUIRED, - HttpChecksumRequired.create()).withInput(operationWithChecksumRequiredRequest)); + .execute(new ClientExecutionParams() + .withOperationName("OperationWithChecksumRequired") + .withProtocolMetadata(protocolMetadata) + .withMarshaller(new OperationWithChecksumRequiredRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector) + .putExecutionAttribute(SdkInternalExecutionAttribute.HTTP_CHECKSUM_REQUIRED, + HttpChecksumRequired.create()).withInput(operationWithChecksumRequiredRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -695,32 +795,46 @@ public CompletableFuture operationWithChe */ @Override public CompletableFuture operationWithNoneAuthType( - OperationWithNoneAuthTypeRequest operationWithNoneAuthTypeRequest) { + OperationWithNoneAuthTypeRequest operationWithNoneAuthTypeRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(operationWithNoneAuthTypeRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, operationWithNoneAuthTypeRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "OperationWithNoneAuthType"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OperationWithNoneAuthTypeResponse::builder); - + operationMetadata, OperationWithNoneAuthTypeResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("OperationWithNoneAuthType").withProtocolMetadata(protocolMetadata) - .withMarshaller(new OperationWithNoneAuthTypeRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(operationWithNoneAuthTypeRequest)); + .execute(new ClientExecutionParams() + .withOperationName("OperationWithNoneAuthType").withProtocolMetadata(protocolMetadata) + .withMarshaller(new OperationWithNoneAuthTypeRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(operationWithNoneAuthTypeRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -755,37 +869,51 @@ public CompletableFuture operationWithNoneAut */ @Override public CompletableFuture operationWithRequestCompression( - OperationWithRequestCompressionRequest operationWithRequestCompressionRequest) { + OperationWithRequestCompressionRequest operationWithRequestCompressionRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(operationWithRequestCompressionRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - operationWithRequestCompressionRequest.overrideConfiguration().orElse(null)); + operationWithRequestCompressionRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "OperationWithRequestCompression"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OperationWithRequestCompressionResponse::builder); - + operationMetadata, OperationWithRequestCompressionResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("OperationWithRequestCompression") - .withProtocolMetadata(protocolMetadata) - .withMarshaller(new OperationWithRequestCompressionRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector) - .putExecutionAttribute(SdkInternalExecutionAttribute.REQUEST_COMPRESSION, - RequestCompression.builder().encodings("gzip").isStreaming(false).build()) - .withInput(operationWithRequestCompressionRequest)); + .execute(new ClientExecutionParams() + .withOperationName("OperationWithRequestCompression") + .withProtocolMetadata(protocolMetadata) + .withMarshaller(new OperationWithRequestCompressionRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector) + .putExecutionAttribute(SdkInternalExecutionAttribute.REQUEST_COMPRESSION, + RequestCompression.builder().encodings("gzip").isStreaming(false).build()) + .withInput(operationWithRequestCompressionRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -820,32 +948,46 @@ public CompletableFuture operationWithR */ @Override public CompletableFuture paginatedOperationWithResultKey( - PaginatedOperationWithResultKeyRequest paginatedOperationWithResultKeyRequest) { + PaginatedOperationWithResultKeyRequest paginatedOperationWithResultKeyRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(paginatedOperationWithResultKeyRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - paginatedOperationWithResultKeyRequest.overrideConfiguration().orElse(null)); + paginatedOperationWithResultKeyRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "PaginatedOperationWithResultKey"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, PaginatedOperationWithResultKeyResponse::builder); - + operationMetadata, PaginatedOperationWithResultKeyResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("PaginatedOperationWithResultKey").withProtocolMetadata(protocolMetadata) - .withMarshaller(new PaginatedOperationWithResultKeyRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(paginatedOperationWithResultKeyRequest)); + .execute(new ClientExecutionParams() + .withOperationName("PaginatedOperationWithResultKey").withProtocolMetadata(protocolMetadata) + .withMarshaller(new PaginatedOperationWithResultKeyRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(paginatedOperationWithResultKeyRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -880,32 +1022,46 @@ public CompletableFuture paginatedOpera */ @Override public CompletableFuture paginatedOperationWithoutResultKey( - PaginatedOperationWithoutResultKeyRequest paginatedOperationWithoutResultKeyRequest) { + PaginatedOperationWithoutResultKeyRequest paginatedOperationWithoutResultKeyRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(paginatedOperationWithoutResultKeyRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - paginatedOperationWithoutResultKeyRequest.overrideConfiguration().orElse(null)); + paginatedOperationWithoutResultKeyRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "PaginatedOperationWithoutResultKey"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory - .createResponseHandler(operationMetadata, PaginatedOperationWithoutResultKeyResponse::builder); - + .createResponseHandler(operationMetadata, PaginatedOperationWithoutResultKeyResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("PaginatedOperationWithoutResultKey").withProtocolMetadata(protocolMetadata) - .withMarshaller(new PaginatedOperationWithoutResultKeyRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(paginatedOperationWithoutResultKeyRequest)); + .execute(new ClientExecutionParams() + .withOperationName("PaginatedOperationWithoutResultKey").withProtocolMetadata(protocolMetadata) + .withMarshaller(new PaginatedOperationWithoutResultKeyRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(paginatedOperationWithoutResultKeyRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -944,36 +1100,50 @@ public CompletableFuture paginatedOp */ @Override public CompletableFuture streamingInputOperation( - StreamingInputOperationRequest streamingInputOperationRequest, AsyncRequestBody requestBody) { + StreamingInputOperationRequest streamingInputOperationRequest, AsyncRequestBody requestBody) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(streamingInputOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, streamingInputOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "StreamingInputOperation"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, StreamingInputOperationResponse::builder); - + operationMetadata, StreamingInputOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("StreamingInputOperation") - .withProtocolMetadata(protocolMetadata) - .withMarshaller( - AsyncStreamingRequestMarshaller.builder() - .delegateMarshaller(new StreamingInputOperationRequestMarshaller(protocolFactory)) - .asyncRequestBody(requestBody).build()).withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector).withAsyncRequestBody(requestBody) - .withInput(streamingInputOperationRequest)); + .execute(new ClientExecutionParams() + .withOperationName("StreamingInputOperation") + .withProtocolMetadata(protocolMetadata) + .withMarshaller( + AsyncStreamingRequestMarshaller.builder() + .delegateMarshaller(new StreamingInputOperationRequestMarshaller(protocolFactory)) + .asyncRequestBody(requestBody).build()).withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector).withAsyncRequestBody(requestBody) + .withInput(streamingInputOperationRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -1017,49 +1187,63 @@ public CompletableFuture streamingInputOperatio */ @Override public CompletableFuture streamingInputOutputOperation( - StreamingInputOutputOperationRequest streamingInputOutputOperationRequest, AsyncRequestBody requestBody, - AsyncResponseTransformer asyncResponseTransformer) { + StreamingInputOutputOperationRequest streamingInputOutputOperationRequest, AsyncRequestBody requestBody, + AsyncResponseTransformer asyncResponseTransformer) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(streamingInputOutputOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - streamingInputOutputOperationRequest.overrideConfiguration().orElse(null)); + streamingInputOutputOperationRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "StreamingInputOutputOperation"); Pair, CompletableFuture> pair = AsyncResponseTransformerUtils - .wrapWithEndOfStreamFuture(asyncResponseTransformer); + .wrapWithEndOfStreamFuture(asyncResponseTransformer); asyncResponseTransformer = pair.left(); CompletableFuture endOfStreamFuture = pair.right(); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(true) - .isPayloadJson(false).build(); + .isPayloadJson(false).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, StreamingInputOutputOperationResponse::builder); - + operationMetadata, StreamingInputOutputOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler.execute( - new ClientExecutionParams() - .withOperationName("StreamingInputOutputOperation") - .withProtocolMetadata(protocolMetadata) - .withMarshaller( - AsyncStreamingRequestMarshaller - .builder() - .delegateMarshaller( - new StreamingInputOutputOperationRequestMarshaller(protocolFactory)) - .asyncRequestBody(requestBody).transferEncoding(true).build()) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withAsyncRequestBody(requestBody).withInput(streamingInputOutputOperationRequest), - asyncResponseTransformer); + new ClientExecutionParams() + .withOperationName("StreamingInputOutputOperation") + .withProtocolMetadata(protocolMetadata) + .withMarshaller( + AsyncStreamingRequestMarshaller + .builder() + .delegateMarshaller( + new StreamingInputOutputOperationRequestMarshaller(protocolFactory)) + .asyncRequestBody(requestBody).transferEncoding(true).build()) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withAsyncRequestBody(requestBody).withInput(streamingInputOutputOperationRequest), + asyncResponseTransformer); AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { if (e != null) { runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(e)); + () -> finalAsyncResponseTransformer.exceptionOccurred(e)); } endOfStreamFuture.whenComplete((r2, e2) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); @@ -1070,7 +1254,7 @@ public CompletableFuture streamingInputOutputOperation( } catch (Throwable t) { AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(t)); + () -> finalAsyncResponseTransformer.exceptionOccurred(t)); metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); return CompletableFutureUtils.failedFuture(t); } @@ -1103,42 +1287,56 @@ public CompletableFuture streamingInputOutputOperation( */ @Override public CompletableFuture streamingOutputOperation( - StreamingOutputOperationRequest streamingOutputOperationRequest, - AsyncResponseTransformer asyncResponseTransformer) { + StreamingOutputOperationRequest streamingOutputOperationRequest, + AsyncResponseTransformer asyncResponseTransformer) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(streamingOutputOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, streamingOutputOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "StreamingOutputOperation"); Pair, CompletableFuture> pair = AsyncResponseTransformerUtils - .wrapWithEndOfStreamFuture(asyncResponseTransformer); + .wrapWithEndOfStreamFuture(asyncResponseTransformer); asyncResponseTransformer = pair.left(); CompletableFuture endOfStreamFuture = pair.right(); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(true) - .isPayloadJson(false).build(); + .isPayloadJson(false).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, StreamingOutputOperationResponse::builder); - + operationMetadata, StreamingOutputOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler.execute( - new ClientExecutionParams() - .withOperationName("StreamingOutputOperation").withProtocolMetadata(protocolMetadata) - .withMarshaller(new StreamingOutputOperationRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(streamingOutputOperationRequest), asyncResponseTransformer); + new ClientExecutionParams() + .withOperationName("StreamingOutputOperation").withProtocolMetadata(protocolMetadata) + .withMarshaller(new StreamingOutputOperationRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(streamingOutputOperationRequest), asyncResponseTransformer); AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { if (e != null) { runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(e)); + () -> finalAsyncResponseTransformer.exceptionOccurred(e)); } endOfStreamFuture.whenComplete((r2, e2) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); @@ -1149,7 +1347,7 @@ public CompletableFuture streamingOutputOperation( } catch (Throwable t) { AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(t)); + () -> finalAsyncResponseTransformer.exceptionOccurred(t)); metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); return CompletableFutureUtils.failedFuture(t); } @@ -1166,21 +1364,12 @@ public final String serviceName() { } private > T init(T builder) { - return builder - .clientConfiguration(clientConfiguration) - .defaultServiceExceptionSupplier(JsonException::builder) - .protocol(AwsJsonProtocol.AWS_JSON) - .protocolVersion("1.1") - .registerModeledException( - ExceptionMetadata.builder().errorCode("InvalidInputException") - .exceptionBuilderSupplier(InvalidInputException::builder).httpStatusCode(400).build()) - .registerModeledException( - ExceptionMetadata.builder().errorCode("ServiceFaultException") - .exceptionBuilderSupplier(ServiceFaultException::builder).httpStatusCode(500).build()); + return builder.clientConfiguration(clientConfiguration).defaultServiceExceptionSupplier(JsonException::builder) + .protocol(AwsJsonProtocol.AWS_JSON).protocolVersion("1.1"); } private static List resolveMetricPublishers(SdkClientConfiguration clientConfiguration, - RequestOverrideConfiguration requestOverrideConfiguration) { + RequestOverrideConfiguration requestOverrideConfiguration) { List publishers = null; if (requestOverrideConfiguration != null) { publishers = requestOverrideConfiguration.metricPublishers(); @@ -1232,12 +1421,7 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, } private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata) { - return protocolFactory.createErrorResponseHandler(operationMetadata); - } - - private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { + JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { return protocolFactory.createErrorResponseHandler(operationMetadata, exceptionMetadataMapper); } diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/sra/test-cbor-async-client-class.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/sra/test-cbor-async-client-class.java index 8953d6a681c1..235d8c308f2e 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/sra/test-cbor-async-client-class.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/sra/test-cbor-async-client-class.java @@ -129,7 +129,7 @@ final class DefaultJsonAsyncClient implements JsonAsyncClient { private static final Logger log = LoggerFactory.getLogger(DefaultJsonAsyncClient.class); private static final AwsProtocolMetadata protocolMetadata = AwsProtocolMetadata.builder() - .serviceProtocol(AwsServiceProtocol.CBOR).build(); + .serviceProtocol(AwsServiceProtocol.CBOR).build(); private final AsyncClientHandler clientHandler; @@ -182,32 +182,46 @@ public JsonUtilities utilities() { public CompletableFuture aPostOperation(APostOperationRequest aPostOperationRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(aPostOperationRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, aPostOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "APostOperation"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, APostOperationResponse::builder); - + operationMetadata, APostOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); String hostPrefix = "{StringMember}-foo."; HostnameValidator.validateHostnameCompliant(aPostOperationRequest.stringMember(), "StringMember", - "aPostOperationRequest"); + "aPostOperationRequest"); String resolvedHostExpression = String.format("%s-foo.", aPostOperationRequest.stringMember()); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("APostOperation").withProtocolMetadata(protocolMetadata) - .withMarshaller(new APostOperationRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .hostPrefixExpression(resolvedHostExpression).withInput(aPostOperationRequest)); + .execute(new ClientExecutionParams() + .withOperationName("APostOperation").withProtocolMetadata(protocolMetadata) + .withMarshaller(new APostOperationRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .hostPrefixExpression(resolvedHostExpression).withInput(aPostOperationRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -246,32 +260,46 @@ public CompletableFuture aPostOperation(APostOperationRe */ @Override public CompletableFuture aPostOperationWithOutput( - APostOperationWithOutputRequest aPostOperationWithOutputRequest) { + APostOperationWithOutputRequest aPostOperationWithOutputRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(aPostOperationWithOutputRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, aPostOperationWithOutputRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "APostOperationWithOutput"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, APostOperationWithOutputResponse::builder); - + operationMetadata, APostOperationWithOutputResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("APostOperationWithOutput").withProtocolMetadata(protocolMetadata) - .withMarshaller(new APostOperationWithOutputRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(aPostOperationWithOutputRequest)); + .execute(new ClientExecutionParams() + .withOperationName("APostOperationWithOutput").withProtocolMetadata(protocolMetadata) + .withMarshaller(new APostOperationWithOutputRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(aPostOperationWithOutputRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -305,66 +333,81 @@ public CompletableFuture aPostOperationWithOut */ @Override public CompletableFuture eventStreamOperation(EventStreamOperationRequest eventStreamOperationRequest, - Publisher requestStream, EventStreamOperationResponseHandler asyncResponseHandler) { + Publisher requestStream, EventStreamOperationResponseHandler asyncResponseHandler) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(eventStreamOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, eventStreamOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "EventStreamOperation"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = new AttachHttpMetadataResponseHandler( - protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationResponse::builder)); + protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationResponse::builder)); HttpResponseHandler voidResponseHandler = protocolFactory.createResponseHandler(JsonOperationMetadata - .builder().isPayloadJson(false).hasStreamingSuccessResponse(true).build(), VoidSdkResponse::builder); + .builder().isPayloadJson(false).hasStreamingSuccessResponse(true).build(), VoidSdkResponse::builder); HttpResponseHandler eventResponseHandler = protocolFactory.createResponseHandler( - JsonOperationMetadata.builder().isPayloadJson(true).hasStreamingSuccessResponse(false).build(), - EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventOne", EventStream::eventOneBuilder) - .putSdkPojoSupplier("EventTheSecond", EventStream::eventTheSecondBuilder) - .putSdkPojoSupplier("secondEventOne", EventStream::secondEventOneBuilder) - .putSdkPojoSupplier("eventThree", EventStream::eventThreeBuilder) - .defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN)).build()); + JsonOperationMetadata.builder().isPayloadJson(true).hasStreamingSuccessResponse(false).build(), + EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventOne", EventStream::eventOneBuilder) + .putSdkPojoSupplier("EventTheSecond", EventStream::eventTheSecondBuilder) + .putSdkPojoSupplier("secondEventOne", EventStream::secondEventOneBuilder) + .putSdkPojoSupplier("eventThree", EventStream::eventThreeBuilder) + .defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN)).build()); Function> eventstreamExceptionMetadataMapper = errorCode -> { switch (errorCode) { - default: - return Optional.empty(); + default: + return Optional.empty(); } }; HttpResponseHandler errorEventResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata, eventstreamExceptionMetadataMapper); + operationMetadata, eventstreamExceptionMetadataMapper); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); EventStreamTaggedUnionJsonMarshaller eventMarshaller = EventStreamTaggedUnionJsonMarshaller.builder() - .putMarshaller(DefaultInputEvent.class, new InputEventMarshaller(protocolFactory)).build(); + .putMarshaller(DefaultInputEvent.class, new InputEventMarshaller(protocolFactory)).build(); SdkPublisher eventPublisher = SdkPublisher.adapt(requestStream); Publisher adapted = eventPublisher.map(event -> eventMarshaller.marshall(event)).map( - AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer); + AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer); CompletableFuture future = new CompletableFuture<>(); EventStreamAsyncResponseTransformer asyncResponseTransformer = EventStreamAsyncResponseTransformer - . builder().eventStreamResponseHandler(asyncResponseHandler) - .eventResponseHandler(eventResponseHandler).initialResponseHandler(responseHandler) - .exceptionResponseHandler(errorEventResponseHandler).future(future).executor(executor) - .serviceName(serviceName()).build(); + . builder().eventStreamResponseHandler(asyncResponseHandler) + .eventResponseHandler(eventResponseHandler).initialResponseHandler(responseHandler) + .exceptionResponseHandler(errorEventResponseHandler).future(future).executor(executor) + .serviceName(serviceName()).build(); CompletableFuture executeFuture = clientHandler.execute( - new ClientExecutionParams() - .withOperationName("EventStreamOperation").withProtocolMetadata(protocolMetadata) - .withMarshaller(new EventStreamOperationRequestMarshaller(protocolFactory)) - .withAsyncRequestBody(AsyncRequestBody.fromPublisher(adapted)).withFullDuplex(true) - .withInitialRequestEvent(true).withResponseHandler(voidResponseHandler) - .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector).withInput(eventStreamOperationRequest), - asyncResponseTransformer); + new ClientExecutionParams() + .withOperationName("EventStreamOperation").withProtocolMetadata(protocolMetadata) + .withMarshaller(new EventStreamOperationRequestMarshaller(protocolFactory)) + .withAsyncRequestBody(AsyncRequestBody.fromPublisher(adapted)).withFullDuplex(true) + .withInitialRequestEvent(true).withResponseHandler(voidResponseHandler) + .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector).withInput(eventStreamOperationRequest), + asyncResponseTransformer); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { if (e != null) { try { @@ -379,7 +422,7 @@ public CompletableFuture eventStreamOperation(EventStreamOperationRequest return CompletableFutureUtils.forwardExceptionTo(future, executeFuture); } catch (Throwable t) { runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> asyncResponseHandler.exceptionOccurred(t)); + () -> asyncResponseHandler.exceptionOccurred(t)); metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); return CompletableFutureUtils.failedFuture(t); } @@ -408,40 +451,54 @@ public CompletableFuture eventStreamOperation(EventStreamOperationRequest */ @Override public CompletableFuture eventStreamOperationWithOnlyInput( - EventStreamOperationWithOnlyInputRequest eventStreamOperationWithOnlyInputRequest, - Publisher requestStream) { + EventStreamOperationWithOnlyInputRequest eventStreamOperationWithOnlyInputRequest, + Publisher requestStream) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(eventStreamOperationWithOnlyInputRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - eventStreamOperationWithOnlyInputRequest.overrideConfiguration().orElse(null)); + eventStreamOperationWithOnlyInputRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "EventStreamOperationWithOnlyInput"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory - .createResponseHandler(operationMetadata, EventStreamOperationWithOnlyInputResponse::builder); - + .createResponseHandler(operationMetadata, EventStreamOperationWithOnlyInputResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); EventStreamTaggedUnionJsonMarshaller eventMarshaller = EventStreamTaggedUnionJsonMarshaller.builder() - .putMarshaller(DefaultInputEventOne.class, new InputEventMarshaller(protocolFactory)) - .putMarshaller(DefaultInputEventTwo.class, new InputEventTwoMarshaller(protocolFactory)).build(); + .putMarshaller(DefaultInputEventOne.class, new InputEventMarshaller(protocolFactory)) + .putMarshaller(DefaultInputEventTwo.class, new InputEventTwoMarshaller(protocolFactory)).build(); SdkPublisher eventPublisher = SdkPublisher.adapt(requestStream); Publisher adapted = eventPublisher.map(event -> eventMarshaller.marshall(event)).map( - AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer); + AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("EventStreamOperationWithOnlyInput").withProtocolMetadata(protocolMetadata) - .withMarshaller(new EventStreamOperationWithOnlyInputRequestMarshaller(protocolFactory)) - .withAsyncRequestBody(AsyncRequestBody.fromPublisher(adapted)).withInitialRequestEvent(true) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(eventStreamOperationWithOnlyInputRequest)); + .execute(new ClientExecutionParams() + .withOperationName("EventStreamOperationWithOnlyInput").withProtocolMetadata(protocolMetadata) + .withMarshaller(new EventStreamOperationWithOnlyInputRequestMarshaller(protocolFactory)) + .withAsyncRequestBody(AsyncRequestBody.fromPublisher(adapted)).withInitialRequestEvent(true) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(eventStreamOperationWithOnlyInputRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -476,60 +533,75 @@ public CompletableFuture eventStreamO */ @Override public CompletableFuture eventStreamOperationWithOnlyOutput( - EventStreamOperationWithOnlyOutputRequest eventStreamOperationWithOnlyOutputRequest, - EventStreamOperationWithOnlyOutputResponseHandler asyncResponseHandler) { + EventStreamOperationWithOnlyOutputRequest eventStreamOperationWithOnlyOutputRequest, + EventStreamOperationWithOnlyOutputResponseHandler asyncResponseHandler) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(eventStreamOperationWithOnlyOutputRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - eventStreamOperationWithOnlyOutputRequest.overrideConfiguration().orElse(null)); + eventStreamOperationWithOnlyOutputRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "EventStreamOperationWithOnlyOutput"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = new AttachHttpMetadataResponseHandler( - protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationWithOnlyOutputResponse::builder)); + protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationWithOnlyOutputResponse::builder)); HttpResponseHandler voidResponseHandler = protocolFactory.createResponseHandler(JsonOperationMetadata - .builder().isPayloadJson(false).hasStreamingSuccessResponse(true).build(), VoidSdkResponse::builder); + .builder().isPayloadJson(false).hasStreamingSuccessResponse(true).build(), VoidSdkResponse::builder); HttpResponseHandler eventResponseHandler = protocolFactory.createResponseHandler( - JsonOperationMetadata.builder().isPayloadJson(true).hasStreamingSuccessResponse(false).build(), - EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventOne", EventStream::eventOneBuilder) - .putSdkPojoSupplier("EventTheSecond", EventStream::eventTheSecondBuilder) - .putSdkPojoSupplier("secondEventOne", EventStream::secondEventOneBuilder) - .putSdkPojoSupplier("eventThree", EventStream::eventThreeBuilder) - .defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN)).build()); + JsonOperationMetadata.builder().isPayloadJson(true).hasStreamingSuccessResponse(false).build(), + EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventOne", EventStream::eventOneBuilder) + .putSdkPojoSupplier("EventTheSecond", EventStream::eventTheSecondBuilder) + .putSdkPojoSupplier("secondEventOne", EventStream::secondEventOneBuilder) + .putSdkPojoSupplier("eventThree", EventStream::eventThreeBuilder) + .defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN)).build()); Function> eventstreamExceptionMetadataMapper = errorCode -> { switch (errorCode) { - default: - return Optional.empty(); + default: + return Optional.empty(); } }; HttpResponseHandler errorEventResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata, eventstreamExceptionMetadataMapper); + operationMetadata, eventstreamExceptionMetadataMapper); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture future = new CompletableFuture<>(); EventStreamAsyncResponseTransformer asyncResponseTransformer = EventStreamAsyncResponseTransformer - . builder() - .eventStreamResponseHandler(asyncResponseHandler).eventResponseHandler(eventResponseHandler) - .initialResponseHandler(responseHandler).exceptionResponseHandler(errorEventResponseHandler).future(future) - .executor(executor).serviceName(serviceName()).build(); + . builder() + .eventStreamResponseHandler(asyncResponseHandler).eventResponseHandler(eventResponseHandler) + .initialResponseHandler(responseHandler).exceptionResponseHandler(errorEventResponseHandler).future(future) + .executor(executor).serviceName(serviceName()).build(); CompletableFuture executeFuture = clientHandler.execute( - new ClientExecutionParams() - .withOperationName("EventStreamOperationWithOnlyOutput").withProtocolMetadata(protocolMetadata) - .withMarshaller(new EventStreamOperationWithOnlyOutputRequestMarshaller(protocolFactory)) - .withResponseHandler(voidResponseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(eventStreamOperationWithOnlyOutputRequest), asyncResponseTransformer); + new ClientExecutionParams() + .withOperationName("EventStreamOperationWithOnlyOutput").withProtocolMetadata(protocolMetadata) + .withMarshaller(new EventStreamOperationWithOnlyOutputRequestMarshaller(protocolFactory)) + .withResponseHandler(voidResponseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(eventStreamOperationWithOnlyOutputRequest), asyncResponseTransformer); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { if (e != null) { try { @@ -544,7 +616,7 @@ public CompletableFuture eventStreamOperationWithOnlyOutput( return CompletableFutureUtils.forwardExceptionTo(future, executeFuture); } catch (Throwable t) { runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> asyncResponseHandler.exceptionOccurred(t)); + () -> asyncResponseHandler.exceptionOccurred(t)); metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); return CompletableFutureUtils.failedFuture(t); } @@ -576,32 +648,46 @@ public CompletableFuture eventStreamOperationWithOnlyOutput( */ @Override public CompletableFuture getWithoutRequiredMembers( - GetWithoutRequiredMembersRequest getWithoutRequiredMembersRequest) { + GetWithoutRequiredMembersRequest getWithoutRequiredMembersRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(getWithoutRequiredMembersRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, getWithoutRequiredMembersRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "GetWithoutRequiredMembers"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, GetWithoutRequiredMembersResponse::builder); - + operationMetadata, GetWithoutRequiredMembersResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("GetWithoutRequiredMembers").withProtocolMetadata(protocolMetadata) - .withMarshaller(new GetWithoutRequiredMembersRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(getWithoutRequiredMembersRequest)); + .execute(new ClientExecutionParams() + .withOperationName("GetWithoutRequiredMembers").withProtocolMetadata(protocolMetadata) + .withMarshaller(new GetWithoutRequiredMembersRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(getWithoutRequiredMembersRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -636,36 +722,50 @@ public CompletableFuture getWithoutRequiredMe */ @Override public CompletableFuture operationWithChecksumRequired( - OperationWithChecksumRequiredRequest operationWithChecksumRequiredRequest) { + OperationWithChecksumRequiredRequest operationWithChecksumRequiredRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(operationWithChecksumRequiredRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - operationWithChecksumRequiredRequest.overrideConfiguration().orElse(null)); + operationWithChecksumRequiredRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "OperationWithChecksumRequired"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OperationWithChecksumRequiredResponse::builder); - + operationMetadata, OperationWithChecksumRequiredResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("OperationWithChecksumRequired") - .withProtocolMetadata(protocolMetadata) - .withMarshaller(new OperationWithChecksumRequiredRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector) - .putExecutionAttribute(SdkInternalExecutionAttribute.HTTP_CHECKSUM_REQUIRED, - HttpChecksumRequired.create()).withInput(operationWithChecksumRequiredRequest)); + .execute(new ClientExecutionParams() + .withOperationName("OperationWithChecksumRequired") + .withProtocolMetadata(protocolMetadata) + .withMarshaller(new OperationWithChecksumRequiredRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector) + .putExecutionAttribute(SdkInternalExecutionAttribute.HTTP_CHECKSUM_REQUIRED, + HttpChecksumRequired.create()).withInput(operationWithChecksumRequiredRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -699,32 +799,46 @@ public CompletableFuture operationWithChe */ @Override public CompletableFuture operationWithNoneAuthType( - OperationWithNoneAuthTypeRequest operationWithNoneAuthTypeRequest) { + OperationWithNoneAuthTypeRequest operationWithNoneAuthTypeRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(operationWithNoneAuthTypeRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, operationWithNoneAuthTypeRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "OperationWithNoneAuthType"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OperationWithNoneAuthTypeResponse::builder); - + operationMetadata, OperationWithNoneAuthTypeResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("OperationWithNoneAuthType").withProtocolMetadata(protocolMetadata) - .withMarshaller(new OperationWithNoneAuthTypeRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(operationWithNoneAuthTypeRequest)); + .execute(new ClientExecutionParams() + .withOperationName("OperationWithNoneAuthType").withProtocolMetadata(protocolMetadata) + .withMarshaller(new OperationWithNoneAuthTypeRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(operationWithNoneAuthTypeRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -759,37 +873,51 @@ public CompletableFuture operationWithNoneAut */ @Override public CompletableFuture operationWithRequestCompression( - OperationWithRequestCompressionRequest operationWithRequestCompressionRequest) { + OperationWithRequestCompressionRequest operationWithRequestCompressionRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(operationWithRequestCompressionRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - operationWithRequestCompressionRequest.overrideConfiguration().orElse(null)); + operationWithRequestCompressionRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "OperationWithRequestCompression"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OperationWithRequestCompressionResponse::builder); - + operationMetadata, OperationWithRequestCompressionResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("OperationWithRequestCompression") - .withProtocolMetadata(protocolMetadata) - .withMarshaller(new OperationWithRequestCompressionRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector) - .putExecutionAttribute(SdkInternalExecutionAttribute.REQUEST_COMPRESSION, - RequestCompression.builder().encodings("gzip").isStreaming(false).build()) - .withInput(operationWithRequestCompressionRequest)); + .execute(new ClientExecutionParams() + .withOperationName("OperationWithRequestCompression") + .withProtocolMetadata(protocolMetadata) + .withMarshaller(new OperationWithRequestCompressionRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector) + .putExecutionAttribute(SdkInternalExecutionAttribute.REQUEST_COMPRESSION, + RequestCompression.builder().encodings("gzip").isStreaming(false).build()) + .withInput(operationWithRequestCompressionRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -824,32 +952,46 @@ public CompletableFuture operationWithR */ @Override public CompletableFuture paginatedOperationWithResultKey( - PaginatedOperationWithResultKeyRequest paginatedOperationWithResultKeyRequest) { + PaginatedOperationWithResultKeyRequest paginatedOperationWithResultKeyRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(paginatedOperationWithResultKeyRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - paginatedOperationWithResultKeyRequest.overrideConfiguration().orElse(null)); + paginatedOperationWithResultKeyRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "PaginatedOperationWithResultKey"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, PaginatedOperationWithResultKeyResponse::builder); - + operationMetadata, PaginatedOperationWithResultKeyResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("PaginatedOperationWithResultKey").withProtocolMetadata(protocolMetadata) - .withMarshaller(new PaginatedOperationWithResultKeyRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(paginatedOperationWithResultKeyRequest)); + .execute(new ClientExecutionParams() + .withOperationName("PaginatedOperationWithResultKey").withProtocolMetadata(protocolMetadata) + .withMarshaller(new PaginatedOperationWithResultKeyRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(paginatedOperationWithResultKeyRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -884,32 +1026,46 @@ public CompletableFuture paginatedOpera */ @Override public CompletableFuture paginatedOperationWithoutResultKey( - PaginatedOperationWithoutResultKeyRequest paginatedOperationWithoutResultKeyRequest) { + PaginatedOperationWithoutResultKeyRequest paginatedOperationWithoutResultKeyRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(paginatedOperationWithoutResultKeyRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - paginatedOperationWithoutResultKeyRequest.overrideConfiguration().orElse(null)); + paginatedOperationWithoutResultKeyRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "PaginatedOperationWithoutResultKey"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory - .createResponseHandler(operationMetadata, PaginatedOperationWithoutResultKeyResponse::builder); - + .createResponseHandler(operationMetadata, PaginatedOperationWithoutResultKeyResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("PaginatedOperationWithoutResultKey").withProtocolMetadata(protocolMetadata) - .withMarshaller(new PaginatedOperationWithoutResultKeyRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(paginatedOperationWithoutResultKeyRequest)); + .execute(new ClientExecutionParams() + .withOperationName("PaginatedOperationWithoutResultKey").withProtocolMetadata(protocolMetadata) + .withMarshaller(new PaginatedOperationWithoutResultKeyRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(paginatedOperationWithoutResultKeyRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -948,36 +1104,50 @@ public CompletableFuture paginatedOp */ @Override public CompletableFuture streamingInputOperation( - StreamingInputOperationRequest streamingInputOperationRequest, AsyncRequestBody requestBody) { + StreamingInputOperationRequest streamingInputOperationRequest, AsyncRequestBody requestBody) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(streamingInputOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, streamingInputOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "StreamingInputOperation"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, StreamingInputOperationResponse::builder); - + operationMetadata, StreamingInputOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("StreamingInputOperation") - .withProtocolMetadata(protocolMetadata) - .withMarshaller( - AsyncStreamingRequestMarshaller.builder() - .delegateMarshaller(new StreamingInputOperationRequestMarshaller(protocolFactory)) - .asyncRequestBody(requestBody).build()).withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector).withAsyncRequestBody(requestBody) - .withInput(streamingInputOperationRequest)); + .execute(new ClientExecutionParams() + .withOperationName("StreamingInputOperation") + .withProtocolMetadata(protocolMetadata) + .withMarshaller( + AsyncStreamingRequestMarshaller.builder() + .delegateMarshaller(new StreamingInputOperationRequestMarshaller(protocolFactory)) + .asyncRequestBody(requestBody).build()).withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector).withAsyncRequestBody(requestBody) + .withInput(streamingInputOperationRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -1021,49 +1191,63 @@ public CompletableFuture streamingInputOperatio */ @Override public CompletableFuture streamingInputOutputOperation( - StreamingInputOutputOperationRequest streamingInputOutputOperationRequest, AsyncRequestBody requestBody, - AsyncResponseTransformer asyncResponseTransformer) { + StreamingInputOutputOperationRequest streamingInputOutputOperationRequest, AsyncRequestBody requestBody, + AsyncResponseTransformer asyncResponseTransformer) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(streamingInputOutputOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - streamingInputOutputOperationRequest.overrideConfiguration().orElse(null)); + streamingInputOutputOperationRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "StreamingInputOutputOperation"); Pair, CompletableFuture> pair = AsyncResponseTransformerUtils - .wrapWithEndOfStreamFuture(asyncResponseTransformer); + .wrapWithEndOfStreamFuture(asyncResponseTransformer); asyncResponseTransformer = pair.left(); CompletableFuture endOfStreamFuture = pair.right(); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(true) - .isPayloadJson(false).build(); + .isPayloadJson(false).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, StreamingInputOutputOperationResponse::builder); - + operationMetadata, StreamingInputOutputOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler.execute( - new ClientExecutionParams() - .withOperationName("StreamingInputOutputOperation") - .withProtocolMetadata(protocolMetadata) - .withMarshaller( - AsyncStreamingRequestMarshaller - .builder() - .delegateMarshaller( - new StreamingInputOutputOperationRequestMarshaller(protocolFactory)) - .asyncRequestBody(requestBody).transferEncoding(true).build()) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withAsyncRequestBody(requestBody).withInput(streamingInputOutputOperationRequest), - asyncResponseTransformer); + new ClientExecutionParams() + .withOperationName("StreamingInputOutputOperation") + .withProtocolMetadata(protocolMetadata) + .withMarshaller( + AsyncStreamingRequestMarshaller + .builder() + .delegateMarshaller( + new StreamingInputOutputOperationRequestMarshaller(protocolFactory)) + .asyncRequestBody(requestBody).transferEncoding(true).build()) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withAsyncRequestBody(requestBody).withInput(streamingInputOutputOperationRequest), + asyncResponseTransformer); AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { if (e != null) { runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(e)); + () -> finalAsyncResponseTransformer.exceptionOccurred(e)); } endOfStreamFuture.whenComplete((r2, e2) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); @@ -1074,7 +1258,7 @@ public CompletableFuture streamingInputOutputOperation( } catch (Throwable t) { AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(t)); + () -> finalAsyncResponseTransformer.exceptionOccurred(t)); metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); return CompletableFutureUtils.failedFuture(t); } @@ -1107,42 +1291,56 @@ public CompletableFuture streamingInputOutputOperation( */ @Override public CompletableFuture streamingOutputOperation( - StreamingOutputOperationRequest streamingOutputOperationRequest, - AsyncResponseTransformer asyncResponseTransformer) { + StreamingOutputOperationRequest streamingOutputOperationRequest, + AsyncResponseTransformer asyncResponseTransformer) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(streamingOutputOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, streamingOutputOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "StreamingOutputOperation"); Pair, CompletableFuture> pair = AsyncResponseTransformerUtils - .wrapWithEndOfStreamFuture(asyncResponseTransformer); + .wrapWithEndOfStreamFuture(asyncResponseTransformer); asyncResponseTransformer = pair.left(); CompletableFuture endOfStreamFuture = pair.right(); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(true) - .isPayloadJson(false).build(); + .isPayloadJson(false).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, StreamingOutputOperationResponse::builder); - + operationMetadata, StreamingOutputOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler.execute( - new ClientExecutionParams() - .withOperationName("StreamingOutputOperation").withProtocolMetadata(protocolMetadata) - .withMarshaller(new StreamingOutputOperationRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(streamingOutputOperationRequest), asyncResponseTransformer); + new ClientExecutionParams() + .withOperationName("StreamingOutputOperation").withProtocolMetadata(protocolMetadata) + .withMarshaller(new StreamingOutputOperationRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(streamingOutputOperationRequest), asyncResponseTransformer); AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { if (e != null) { runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(e)); + () -> finalAsyncResponseTransformer.exceptionOccurred(e)); } endOfStreamFuture.whenComplete((r2, e2) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); @@ -1153,7 +1351,7 @@ public CompletableFuture streamingOutputOperation( } catch (Throwable t) { AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(t)); + () -> finalAsyncResponseTransformer.exceptionOccurred(t)); metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); return CompletableFutureUtils.failedFuture(t); } @@ -1170,21 +1368,12 @@ public final String serviceName() { } private > T init(T builder) { - return builder - .clientConfiguration(clientConfiguration) - .defaultServiceExceptionSupplier(JsonException::builder) - .protocol(AwsJsonProtocol.AWS_JSON) - .protocolVersion("1.1") - .registerModeledException( - ExceptionMetadata.builder().errorCode("InvalidInputException") - .exceptionBuilderSupplier(InvalidInputException::builder).httpStatusCode(400).build()) - .registerModeledException( - ExceptionMetadata.builder().errorCode("ServiceFaultException") - .exceptionBuilderSupplier(ServiceFaultException::builder).httpStatusCode(500).build()); + return builder.clientConfiguration(clientConfiguration).defaultServiceExceptionSupplier(JsonException::builder) + .protocol(AwsJsonProtocol.AWS_JSON).protocolVersion("1.1"); } private static List resolveMetricPublishers(SdkClientConfiguration clientConfiguration, - RequestOverrideConfiguration requestOverrideConfiguration) { + RequestOverrideConfiguration requestOverrideConfiguration) { List publishers = null; if (requestOverrideConfiguration != null) { publishers = requestOverrideConfiguration.metricPublishers(); @@ -1236,12 +1425,7 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, } private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata) { - return protocolFactory.createErrorResponseHandler(operationMetadata); - } - - private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { + JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { return protocolFactory.createErrorResponseHandler(operationMetadata, exceptionMetadataMapper); } diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/sra/test-json-async-client-class.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/sra/test-json-async-client-class.java index 4a99f346bc6c..48a6d4ca2de0 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/sra/test-json-async-client-class.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/sra/test-json-async-client-class.java @@ -139,7 +139,7 @@ final class DefaultJsonAsyncClient implements JsonAsyncClient { private static final Logger log = LoggerFactory.getLogger(DefaultJsonAsyncClient.class); private static final AwsProtocolMetadata protocolMetadata = AwsProtocolMetadata.builder() - .serviceProtocol(AwsServiceProtocol.REST_JSON).build(); + .serviceProtocol(AwsServiceProtocol.REST_JSON).build(); private final AsyncClientHandler clientHandler; @@ -192,32 +192,43 @@ public JsonUtilities utilities() { public CompletableFuture aPostOperation(APostOperationRequest aPostOperationRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(aPostOperationRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, aPostOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "APostOperation"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, APostOperationResponse::builder); - + operationMetadata, APostOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); String hostPrefix = "{StringMember}-foo."; HostnameValidator.validateHostnameCompliant(aPostOperationRequest.stringMember(), "StringMember", - "aPostOperationRequest"); + "aPostOperationRequest"); String resolvedHostExpression = String.format("%s-foo.", aPostOperationRequest.stringMember()); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("APostOperation").withProtocolMetadata(protocolMetadata) - .withMarshaller(new APostOperationRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .hostPrefixExpression(resolvedHostExpression).withInput(aPostOperationRequest)); + .execute(new ClientExecutionParams() + .withOperationName("APostOperation").withProtocolMetadata(protocolMetadata) + .withMarshaller(new APostOperationRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .hostPrefixExpression(resolvedHostExpression).withInput(aPostOperationRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -255,32 +266,43 @@ public CompletableFuture aPostOperation(APostOperationRe */ @Override public CompletableFuture aPostOperationWithOutput( - APostOperationWithOutputRequest aPostOperationWithOutputRequest) { + APostOperationWithOutputRequest aPostOperationWithOutputRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(aPostOperationWithOutputRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, aPostOperationWithOutputRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "APostOperationWithOutput"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, APostOperationWithOutputResponse::builder); - + operationMetadata, APostOperationWithOutputResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("APostOperationWithOutput").withProtocolMetadata(protocolMetadata) - .withMarshaller(new APostOperationWithOutputRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(aPostOperationWithOutputRequest)); + .execute(new ClientExecutionParams() + .withOperationName("APostOperationWithOutput").withProtocolMetadata(protocolMetadata) + .withMarshaller(new APostOperationWithOutputRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(aPostOperationWithOutputRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -314,32 +336,43 @@ public CompletableFuture aPostOperationWithOut */ @Override public CompletableFuture bearerAuthOperation( - BearerAuthOperationRequest bearerAuthOperationRequest) { + BearerAuthOperationRequest bearerAuthOperationRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(bearerAuthOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, bearerAuthOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "BearerAuthOperation"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, BearerAuthOperationResponse::builder); - + operationMetadata, BearerAuthOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("BearerAuthOperation").withProtocolMetadata(protocolMetadata) - .withMarshaller(new BearerAuthOperationRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .credentialType(CredentialType.TOKEN).withInput(bearerAuthOperationRequest)); + .execute(new ClientExecutionParams() + .withOperationName("BearerAuthOperation").withProtocolMetadata(protocolMetadata) + .withMarshaller(new BearerAuthOperationRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .credentialType(CredentialType.TOKEN).withInput(bearerAuthOperationRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -373,75 +406,87 @@ public CompletableFuture bearerAuthOperation( */ @Override public CompletableFuture eventStreamOperation(EventStreamOperationRequest eventStreamOperationRequest, - Publisher requestStream, EventStreamOperationResponseHandler asyncResponseHandler) { + Publisher requestStream, EventStreamOperationResponseHandler asyncResponseHandler) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(eventStreamOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, eventStreamOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "EventStreamOperation"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = new AttachHttpMetadataResponseHandler( - protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationResponse::builder)); + protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationResponse::builder)); HttpResponseHandler voidResponseHandler = protocolFactory.createResponseHandler(JsonOperationMetadata - .builder().isPayloadJson(false).hasStreamingSuccessResponse(true).build(), VoidSdkResponse::builder); + .builder().isPayloadJson(false).hasStreamingSuccessResponse(true).build(), VoidSdkResponse::builder); HttpResponseHandler eventResponseHandler = protocolFactory.createResponseHandler( - JsonOperationMetadata.builder().isPayloadJson(true).hasStreamingSuccessResponse(false).build(), - EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventOne", EventStream::eventOneBuilder) - .putSdkPojoSupplier("EventTheSecond", EventStream::eventTheSecondBuilder) - .putSdkPojoSupplier("secondEventOne", EventStream::secondEventOneBuilder) - .putSdkPojoSupplier("eventThree", EventStream::eventThreeBuilder) - .defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN)).build()); + JsonOperationMetadata.builder().isPayloadJson(true).hasStreamingSuccessResponse(false).build(), + EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventOne", EventStream::eventOneBuilder) + .putSdkPojoSupplier("EventTheSecond", EventStream::eventTheSecondBuilder) + .putSdkPojoSupplier("secondEventOne", EventStream::secondEventOneBuilder) + .putSdkPojoSupplier("eventThree", EventStream::eventThreeBuilder) + .defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN)).build()); Function> eventstreamExceptionMetadataMapper = errorCode -> { switch (errorCode) { - case "errorOne": - return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) - .exceptionBuilderSupplier(InvalidInputException::builder).build()); - case "errorTwo": - return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) - .exceptionBuilderSupplier(InvalidInputException::builder).build()); - default: - return Optional.empty(); + case "errorOne": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "errorTwo": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); } }; HttpResponseHandler errorEventResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata, eventstreamExceptionMetadataMapper); + operationMetadata, eventstreamExceptionMetadataMapper); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); EventStreamTaggedUnionJsonMarshaller eventMarshaller = EventStreamTaggedUnionJsonMarshaller.builder() - .putMarshaller(DefaultInputEvent.class, new InputEventMarshaller(protocolFactory)).build(); + .putMarshaller(DefaultInputEvent.class, new InputEventMarshaller(protocolFactory)).build(); SdkPublisher eventPublisher = SdkPublisher.adapt(requestStream); Publisher adapted = eventPublisher.map(event -> eventMarshaller.marshall(event)).map( - AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer); + AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer); CompletableFuture future = new CompletableFuture<>(); EventStreamAsyncResponseTransformer asyncResponseTransformer = EventStreamAsyncResponseTransformer - . builder().eventStreamResponseHandler(asyncResponseHandler) - .eventResponseHandler(eventResponseHandler).initialResponseHandler(responseHandler) - .exceptionResponseHandler(errorEventResponseHandler).future(future).executor(executor) - .serviceName(serviceName()).build(); + . builder().eventStreamResponseHandler(asyncResponseHandler) + .eventResponseHandler(eventResponseHandler).initialResponseHandler(responseHandler) + .exceptionResponseHandler(errorEventResponseHandler).future(future).executor(executor) + .serviceName(serviceName()).build(); RestEventStreamAsyncResponseTransformer restAsyncResponseTransformer = RestEventStreamAsyncResponseTransformer - . builder() - .eventStreamAsyncResponseTransformer(asyncResponseTransformer) - .eventStreamResponseHandler(asyncResponseHandler).build(); + . builder() + .eventStreamAsyncResponseTransformer(asyncResponseTransformer) + .eventStreamResponseHandler(asyncResponseHandler).build(); CompletableFuture executeFuture = clientHandler.execute( - new ClientExecutionParams() - .withOperationName("EventStreamOperation").withProtocolMetadata(protocolMetadata) - .withMarshaller(new EventStreamOperationRequestMarshaller(protocolFactory)) - .withAsyncRequestBody(AsyncRequestBody.fromPublisher(adapted)).withFullDuplex(true) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(eventStreamOperationRequest), restAsyncResponseTransformer); + new ClientExecutionParams() + .withOperationName("EventStreamOperation").withProtocolMetadata(protocolMetadata) + .withMarshaller(new EventStreamOperationRequestMarshaller(protocolFactory)) + .withAsyncRequestBody(AsyncRequestBody.fromPublisher(adapted)).withFullDuplex(true) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(eventStreamOperationRequest), restAsyncResponseTransformer); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { if (e != null) { try { @@ -456,7 +501,7 @@ public CompletableFuture eventStreamOperation(EventStreamOperationRequest return CompletableFutureUtils.forwardExceptionTo(future, executeFuture); } catch (Throwable t) { runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> asyncResponseHandler.exceptionOccurred(t)); + () -> asyncResponseHandler.exceptionOccurred(t)); metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); return CompletableFutureUtils.failedFuture(t); } @@ -485,39 +530,50 @@ public CompletableFuture eventStreamOperation(EventStreamOperationRequest */ @Override public CompletableFuture eventStreamOperationWithOnlyInput( - EventStreamOperationWithOnlyInputRequest eventStreamOperationWithOnlyInputRequest, - Publisher requestStream) { + EventStreamOperationWithOnlyInputRequest eventStreamOperationWithOnlyInputRequest, + Publisher requestStream) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(eventStreamOperationWithOnlyInputRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - eventStreamOperationWithOnlyInputRequest.overrideConfiguration().orElse(null)); + eventStreamOperationWithOnlyInputRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "EventStreamOperationWithOnlyInput"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory - .createResponseHandler(operationMetadata, EventStreamOperationWithOnlyInputResponse::builder); - + .createResponseHandler(operationMetadata, EventStreamOperationWithOnlyInputResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); EventStreamTaggedUnionJsonMarshaller eventMarshaller = EventStreamTaggedUnionJsonMarshaller.builder() - .putMarshaller(DefaultInputEventOne.class, new InputEventMarshaller(protocolFactory)) - .putMarshaller(DefaultInputEventTwo.class, new InputEventTwoMarshaller(protocolFactory)).build(); + .putMarshaller(DefaultInputEventOne.class, new InputEventMarshaller(protocolFactory)) + .putMarshaller(DefaultInputEventTwo.class, new InputEventTwoMarshaller(protocolFactory)).build(); SdkPublisher eventPublisher = SdkPublisher.adapt(requestStream); Publisher adapted = eventPublisher.map(event -> eventMarshaller.marshall(event)).map( - AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer); + AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("EventStreamOperationWithOnlyInput").withProtocolMetadata(protocolMetadata) - .withMarshaller(new EventStreamOperationWithOnlyInputRequestMarshaller(protocolFactory)) - .withAsyncRequestBody(AsyncRequestBody.fromPublisher(adapted)).withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector).withInput(eventStreamOperationWithOnlyInputRequest)); + .execute(new ClientExecutionParams() + .withOperationName("EventStreamOperationWithOnlyInput").withProtocolMetadata(protocolMetadata) + .withMarshaller(new EventStreamOperationWithOnlyInputRequestMarshaller(protocolFactory)) + .withAsyncRequestBody(AsyncRequestBody.fromPublisher(adapted)).withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector).withInput(eventStreamOperationWithOnlyInputRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -552,72 +608,84 @@ public CompletableFuture eventStreamO */ @Override public CompletableFuture eventStreamOperationWithOnlyOutput( - EventStreamOperationWithOnlyOutputRequest eventStreamOperationWithOnlyOutputRequest, - EventStreamOperationWithOnlyOutputResponseHandler asyncResponseHandler) { + EventStreamOperationWithOnlyOutputRequest eventStreamOperationWithOnlyOutputRequest, + EventStreamOperationWithOnlyOutputResponseHandler asyncResponseHandler) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(eventStreamOperationWithOnlyOutputRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - eventStreamOperationWithOnlyOutputRequest.overrideConfiguration().orElse(null)); + eventStreamOperationWithOnlyOutputRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "EventStreamOperationWithOnlyOutput"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = new AttachHttpMetadataResponseHandler( - protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationWithOnlyOutputResponse::builder)); + protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationWithOnlyOutputResponse::builder)); HttpResponseHandler voidResponseHandler = protocolFactory.createResponseHandler(JsonOperationMetadata - .builder().isPayloadJson(false).hasStreamingSuccessResponse(true).build(), VoidSdkResponse::builder); + .builder().isPayloadJson(false).hasStreamingSuccessResponse(true).build(), VoidSdkResponse::builder); HttpResponseHandler eventResponseHandler = protocolFactory.createResponseHandler( - JsonOperationMetadata.builder().isPayloadJson(true).hasStreamingSuccessResponse(false).build(), - EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventOne", EventStream::eventOneBuilder) - .putSdkPojoSupplier("EventTheSecond", EventStream::eventTheSecondBuilder) - .putSdkPojoSupplier("secondEventOne", EventStream::secondEventOneBuilder) - .putSdkPojoSupplier("eventThree", EventStream::eventThreeBuilder) - .defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN)).build()); + JsonOperationMetadata.builder().isPayloadJson(true).hasStreamingSuccessResponse(false).build(), + EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventOne", EventStream::eventOneBuilder) + .putSdkPojoSupplier("EventTheSecond", EventStream::eventTheSecondBuilder) + .putSdkPojoSupplier("secondEventOne", EventStream::secondEventOneBuilder) + .putSdkPojoSupplier("eventThree", EventStream::eventThreeBuilder) + .defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN)).build()); Function> eventstreamExceptionMetadataMapper = errorCode -> { switch (errorCode) { - case "errorOne": - return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) - .exceptionBuilderSupplier(InvalidInputException::builder).build()); - case "errorTwo": - return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) - .exceptionBuilderSupplier(InvalidInputException::builder).build()); - default: - return Optional.empty(); + case "errorOne": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "errorTwo": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); } }; HttpResponseHandler errorEventResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata, eventstreamExceptionMetadataMapper); + operationMetadata, eventstreamExceptionMetadataMapper); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture future = new CompletableFuture<>(); EventStreamAsyncResponseTransformer asyncResponseTransformer = EventStreamAsyncResponseTransformer - . builder() - .eventStreamResponseHandler(asyncResponseHandler).eventResponseHandler(eventResponseHandler) - .initialResponseHandler(responseHandler).exceptionResponseHandler(errorEventResponseHandler).future(future) - .executor(executor).serviceName(serviceName()).build(); + . builder() + .eventStreamResponseHandler(asyncResponseHandler).eventResponseHandler(eventResponseHandler) + .initialResponseHandler(responseHandler).exceptionResponseHandler(errorEventResponseHandler).future(future) + .executor(executor).serviceName(serviceName()).build(); RestEventStreamAsyncResponseTransformer restAsyncResponseTransformer = RestEventStreamAsyncResponseTransformer - . builder() - .eventStreamAsyncResponseTransformer(asyncResponseTransformer) - .eventStreamResponseHandler(asyncResponseHandler).build(); + . builder() + .eventStreamAsyncResponseTransformer(asyncResponseTransformer) + .eventStreamResponseHandler(asyncResponseHandler).build(); CompletableFuture executeFuture = clientHandler - .execute( - new ClientExecutionParams() - .withOperationName("EventStreamOperationWithOnlyOutput") - .withProtocolMetadata(protocolMetadata) - .withMarshaller(new EventStreamOperationWithOnlyOutputRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(eventStreamOperationWithOnlyOutputRequest), restAsyncResponseTransformer); + .execute( + new ClientExecutionParams() + .withOperationName("EventStreamOperationWithOnlyOutput") + .withProtocolMetadata(protocolMetadata) + .withMarshaller(new EventStreamOperationWithOnlyOutputRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(eventStreamOperationWithOnlyOutputRequest), restAsyncResponseTransformer); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { if (e != null) { try { @@ -632,7 +700,7 @@ public CompletableFuture eventStreamOperationWithOnlyOutput( return CompletableFutureUtils.forwardExceptionTo(future, executeFuture); } catch (Throwable t) { runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> asyncResponseHandler.exceptionOccurred(t)); + () -> asyncResponseHandler.exceptionOccurred(t)); metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); return CompletableFutureUtils.failedFuture(t); } @@ -660,40 +728,51 @@ public CompletableFuture eventStreamOperationWithOnlyOutput( */ @Override public CompletableFuture getOperationWithChecksum( - GetOperationWithChecksumRequest getOperationWithChecksumRequest) { + GetOperationWithChecksumRequest getOperationWithChecksumRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(getOperationWithChecksumRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, getOperationWithChecksumRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "GetOperationWithChecksum"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(false).build(); + .isPayloadJson(false).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, GetOperationWithChecksumResponse::builder); - + operationMetadata, GetOperationWithChecksumResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("GetOperationWithChecksum") - .withProtocolMetadata(protocolMetadata) - .withMarshaller(new GetOperationWithChecksumRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector) - .putExecutionAttribute( - SdkInternalExecutionAttribute.HTTP_CHECKSUM, - HttpChecksum.builder().requestChecksumRequired(true).isRequestStreaming(false) - .requestAlgorithm(getOperationWithChecksumRequest.checksumAlgorithmAsString()) - .requestAlgorithmHeader("x-amz-sdk-checksum-algorithm").build()) - .withInput(getOperationWithChecksumRequest)); + .execute(new ClientExecutionParams() + .withOperationName("GetOperationWithChecksum") + .withProtocolMetadata(protocolMetadata) + .withMarshaller(new GetOperationWithChecksumRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector) + .putExecutionAttribute( + SdkInternalExecutionAttribute.HTTP_CHECKSUM, + HttpChecksum.builder().requestChecksumRequired(true).isRequestStreaming(false) + .requestAlgorithm(getOperationWithChecksumRequest.checksumAlgorithmAsString()) + .requestAlgorithmHeader("x-amz-sdk-checksum-algorithm").build()) + .withInput(getOperationWithChecksumRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -731,32 +810,43 @@ public CompletableFuture getOperationWithCheck */ @Override public CompletableFuture getWithoutRequiredMembers( - GetWithoutRequiredMembersRequest getWithoutRequiredMembersRequest) { + GetWithoutRequiredMembersRequest getWithoutRequiredMembersRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(getWithoutRequiredMembersRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, getWithoutRequiredMembersRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "GetWithoutRequiredMembers"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, GetWithoutRequiredMembersResponse::builder); - + operationMetadata, GetWithoutRequiredMembersResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("GetWithoutRequiredMembers").withProtocolMetadata(protocolMetadata) - .withMarshaller(new GetWithoutRequiredMembersRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(getWithoutRequiredMembersRequest)); + .execute(new ClientExecutionParams() + .withOperationName("GetWithoutRequiredMembers").withProtocolMetadata(protocolMetadata) + .withMarshaller(new GetWithoutRequiredMembersRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(getWithoutRequiredMembersRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -791,36 +881,47 @@ public CompletableFuture getWithoutRequiredMe */ @Override public CompletableFuture operationWithChecksumRequired( - OperationWithChecksumRequiredRequest operationWithChecksumRequiredRequest) { + OperationWithChecksumRequiredRequest operationWithChecksumRequiredRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(operationWithChecksumRequiredRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - operationWithChecksumRequiredRequest.overrideConfiguration().orElse(null)); + operationWithChecksumRequiredRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "OperationWithChecksumRequired"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OperationWithChecksumRequiredResponse::builder); - + operationMetadata, OperationWithChecksumRequiredResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("OperationWithChecksumRequired") - .withProtocolMetadata(protocolMetadata) - .withMarshaller(new OperationWithChecksumRequiredRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector) - .putExecutionAttribute(SdkInternalExecutionAttribute.HTTP_CHECKSUM_REQUIRED, - HttpChecksumRequired.create()).withInput(operationWithChecksumRequiredRequest)); + .execute(new ClientExecutionParams() + .withOperationName("OperationWithChecksumRequired") + .withProtocolMetadata(protocolMetadata) + .withMarshaller(new OperationWithChecksumRequiredRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector) + .putExecutionAttribute(SdkInternalExecutionAttribute.HTTP_CHECKSUM_REQUIRED, + HttpChecksumRequired.create()).withInput(operationWithChecksumRequiredRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -855,37 +956,48 @@ public CompletableFuture operationWithChe */ @Override public CompletableFuture operationWithRequestCompression( - OperationWithRequestCompressionRequest operationWithRequestCompressionRequest) { + OperationWithRequestCompressionRequest operationWithRequestCompressionRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(operationWithRequestCompressionRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - operationWithRequestCompressionRequest.overrideConfiguration().orElse(null)); + operationWithRequestCompressionRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "OperationWithRequestCompression"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OperationWithRequestCompressionResponse::builder); - + operationMetadata, OperationWithRequestCompressionResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("OperationWithRequestCompression") - .withProtocolMetadata(protocolMetadata) - .withMarshaller(new OperationWithRequestCompressionRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector) - .putExecutionAttribute(SdkInternalExecutionAttribute.REQUEST_COMPRESSION, - RequestCompression.builder().encodings("gzip").isStreaming(false).build()) - .withInput(operationWithRequestCompressionRequest)); + .execute(new ClientExecutionParams() + .withOperationName("OperationWithRequestCompression") + .withProtocolMetadata(protocolMetadata) + .withMarshaller(new OperationWithRequestCompressionRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector) + .putExecutionAttribute(SdkInternalExecutionAttribute.REQUEST_COMPRESSION, + RequestCompression.builder().encodings("gzip").isStreaming(false).build()) + .withInput(operationWithRequestCompressionRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -920,32 +1032,43 @@ public CompletableFuture operationWithR */ @Override public CompletableFuture paginatedOperationWithResultKey( - PaginatedOperationWithResultKeyRequest paginatedOperationWithResultKeyRequest) { + PaginatedOperationWithResultKeyRequest paginatedOperationWithResultKeyRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(paginatedOperationWithResultKeyRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - paginatedOperationWithResultKeyRequest.overrideConfiguration().orElse(null)); + paginatedOperationWithResultKeyRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "PaginatedOperationWithResultKey"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, PaginatedOperationWithResultKeyResponse::builder); - + operationMetadata, PaginatedOperationWithResultKeyResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("PaginatedOperationWithResultKey").withProtocolMetadata(protocolMetadata) - .withMarshaller(new PaginatedOperationWithResultKeyRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(paginatedOperationWithResultKeyRequest)); + .execute(new ClientExecutionParams() + .withOperationName("PaginatedOperationWithResultKey").withProtocolMetadata(protocolMetadata) + .withMarshaller(new PaginatedOperationWithResultKeyRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(paginatedOperationWithResultKeyRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -980,32 +1103,43 @@ public CompletableFuture paginatedOpera */ @Override public CompletableFuture paginatedOperationWithoutResultKey( - PaginatedOperationWithoutResultKeyRequest paginatedOperationWithoutResultKeyRequest) { + PaginatedOperationWithoutResultKeyRequest paginatedOperationWithoutResultKeyRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(paginatedOperationWithoutResultKeyRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - paginatedOperationWithoutResultKeyRequest.overrideConfiguration().orElse(null)); + paginatedOperationWithoutResultKeyRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "PaginatedOperationWithoutResultKey"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory - .createResponseHandler(operationMetadata, PaginatedOperationWithoutResultKeyResponse::builder); - + .createResponseHandler(operationMetadata, PaginatedOperationWithoutResultKeyResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("PaginatedOperationWithoutResultKey").withProtocolMetadata(protocolMetadata) - .withMarshaller(new PaginatedOperationWithoutResultKeyRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(paginatedOperationWithoutResultKeyRequest)); + .execute(new ClientExecutionParams() + .withOperationName("PaginatedOperationWithoutResultKey").withProtocolMetadata(protocolMetadata) + .withMarshaller(new PaginatedOperationWithoutResultKeyRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(paginatedOperationWithoutResultKeyRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -1057,59 +1191,70 @@ public CompletableFuture paginatedOp */ @Override public CompletableFuture putOperationWithChecksum( - PutOperationWithChecksumRequest putOperationWithChecksumRequest, AsyncRequestBody requestBody, - AsyncResponseTransformer asyncResponseTransformer) { + PutOperationWithChecksumRequest putOperationWithChecksumRequest, AsyncRequestBody requestBody, + AsyncResponseTransformer asyncResponseTransformer) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(putOperationWithChecksumRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, putOperationWithChecksumRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "PutOperationWithChecksum"); Pair, CompletableFuture> pair = AsyncResponseTransformerUtils - .wrapWithEndOfStreamFuture(asyncResponseTransformer); + .wrapWithEndOfStreamFuture(asyncResponseTransformer); asyncResponseTransformer = pair.left(); CompletableFuture endOfStreamFuture = pair.right(); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(true) - .isPayloadJson(false).build(); + .isPayloadJson(false).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, PutOperationWithChecksumResponse::builder); - + operationMetadata, PutOperationWithChecksumResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler.execute( - new ClientExecutionParams() - .withOperationName("PutOperationWithChecksum") - .withProtocolMetadata(protocolMetadata) - .withMarshaller( - AsyncStreamingRequestMarshaller.builder() - .delegateMarshaller(new PutOperationWithChecksumRequestMarshaller(protocolFactory)) - .asyncRequestBody(requestBody).build()) - .withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector) - .withAsyncRequestBody(requestBody) - .putExecutionAttribute( - SdkInternalExecutionAttribute.HTTP_CHECKSUM, - HttpChecksum - .builder() - .requestChecksumRequired(false) - .isRequestStreaming(true) - .requestValidationMode(putOperationWithChecksumRequest.checksumModeAsString()) - .responseAlgorithmsV2(DefaultChecksumAlgorithm.CRC32C, - DefaultChecksumAlgorithm.CRC32, DefaultChecksumAlgorithm.SHA1, - DefaultChecksumAlgorithm.SHA256).build()) - .withInput(putOperationWithChecksumRequest), asyncResponseTransformer); + new ClientExecutionParams() + .withOperationName("PutOperationWithChecksum") + .withProtocolMetadata(protocolMetadata) + .withMarshaller( + AsyncStreamingRequestMarshaller.builder() + .delegateMarshaller(new PutOperationWithChecksumRequestMarshaller(protocolFactory)) + .asyncRequestBody(requestBody).build()) + .withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector) + .withAsyncRequestBody(requestBody) + .putExecutionAttribute( + SdkInternalExecutionAttribute.HTTP_CHECKSUM, + HttpChecksum + .builder() + .requestChecksumRequired(false) + .isRequestStreaming(true) + .requestValidationMode(putOperationWithChecksumRequest.checksumModeAsString()) + .responseAlgorithmsV2(DefaultChecksumAlgorithm.CRC32C, + DefaultChecksumAlgorithm.CRC32, DefaultChecksumAlgorithm.SHA1, + DefaultChecksumAlgorithm.SHA256).build()) + .withInput(putOperationWithChecksumRequest), asyncResponseTransformer); AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { if (e != null) { runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(e)); + () -> finalAsyncResponseTransformer.exceptionOccurred(e)); } endOfStreamFuture.whenComplete((r2, e2) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); @@ -1120,7 +1265,7 @@ public CompletableFuture putOperationWithChecksum( } catch (Throwable t) { AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(t)); + () -> finalAsyncResponseTransformer.exceptionOccurred(t)); metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); return CompletableFutureUtils.failedFuture(t); } @@ -1153,36 +1298,47 @@ public CompletableFuture putOperationWithChecksum( */ @Override public CompletableFuture streamingInputOperation( - StreamingInputOperationRequest streamingInputOperationRequest, AsyncRequestBody requestBody) { + StreamingInputOperationRequest streamingInputOperationRequest, AsyncRequestBody requestBody) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(streamingInputOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, streamingInputOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "StreamingInputOperation"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, StreamingInputOperationResponse::builder); - + operationMetadata, StreamingInputOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("StreamingInputOperation") - .withProtocolMetadata(protocolMetadata) - .withMarshaller( - AsyncStreamingRequestMarshaller.builder() - .delegateMarshaller(new StreamingInputOperationRequestMarshaller(protocolFactory)) - .asyncRequestBody(requestBody).build()).withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector).withAsyncRequestBody(requestBody) - .withInput(streamingInputOperationRequest)); + .execute(new ClientExecutionParams() + .withOperationName("StreamingInputOperation") + .withProtocolMetadata(protocolMetadata) + .withMarshaller( + AsyncStreamingRequestMarshaller.builder() + .delegateMarshaller(new StreamingInputOperationRequestMarshaller(protocolFactory)) + .asyncRequestBody(requestBody).build()).withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector).withAsyncRequestBody(requestBody) + .withInput(streamingInputOperationRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -1226,49 +1382,60 @@ public CompletableFuture streamingInputOperatio */ @Override public CompletableFuture streamingInputOutputOperation( - StreamingInputOutputOperationRequest streamingInputOutputOperationRequest, AsyncRequestBody requestBody, - AsyncResponseTransformer asyncResponseTransformer) { + StreamingInputOutputOperationRequest streamingInputOutputOperationRequest, AsyncRequestBody requestBody, + AsyncResponseTransformer asyncResponseTransformer) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(streamingInputOutputOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - streamingInputOutputOperationRequest.overrideConfiguration().orElse(null)); + streamingInputOutputOperationRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "StreamingInputOutputOperation"); Pair, CompletableFuture> pair = AsyncResponseTransformerUtils - .wrapWithEndOfStreamFuture(asyncResponseTransformer); + .wrapWithEndOfStreamFuture(asyncResponseTransformer); asyncResponseTransformer = pair.left(); CompletableFuture endOfStreamFuture = pair.right(); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(true) - .isPayloadJson(false).build(); + .isPayloadJson(false).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, StreamingInputOutputOperationResponse::builder); - + operationMetadata, StreamingInputOutputOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler.execute( - new ClientExecutionParams() - .withOperationName("StreamingInputOutputOperation") - .withProtocolMetadata(protocolMetadata) - .withMarshaller( - AsyncStreamingRequestMarshaller - .builder() - .delegateMarshaller( - new StreamingInputOutputOperationRequestMarshaller(protocolFactory)) - .asyncRequestBody(requestBody).transferEncoding(true).build()) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withAsyncRequestBody(requestBody).withInput(streamingInputOutputOperationRequest), - asyncResponseTransformer); + new ClientExecutionParams() + .withOperationName("StreamingInputOutputOperation") + .withProtocolMetadata(protocolMetadata) + .withMarshaller( + AsyncStreamingRequestMarshaller + .builder() + .delegateMarshaller( + new StreamingInputOutputOperationRequestMarshaller(protocolFactory)) + .asyncRequestBody(requestBody).transferEncoding(true).build()) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withAsyncRequestBody(requestBody).withInput(streamingInputOutputOperationRequest), + asyncResponseTransformer); AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { if (e != null) { runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(e)); + () -> finalAsyncResponseTransformer.exceptionOccurred(e)); } endOfStreamFuture.whenComplete((r2, e2) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); @@ -1279,7 +1446,7 @@ public CompletableFuture streamingInputOutputOperation( } catch (Throwable t) { AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(t)); + () -> finalAsyncResponseTransformer.exceptionOccurred(t)); metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); return CompletableFutureUtils.failedFuture(t); } @@ -1312,42 +1479,53 @@ public CompletableFuture streamingInputOutputOperation( */ @Override public CompletableFuture streamingOutputOperation( - StreamingOutputOperationRequest streamingOutputOperationRequest, - AsyncResponseTransformer asyncResponseTransformer) { + StreamingOutputOperationRequest streamingOutputOperationRequest, + AsyncResponseTransformer asyncResponseTransformer) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(streamingOutputOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, streamingOutputOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "StreamingOutputOperation"); Pair, CompletableFuture> pair = AsyncResponseTransformerUtils - .wrapWithEndOfStreamFuture(asyncResponseTransformer); + .wrapWithEndOfStreamFuture(asyncResponseTransformer); asyncResponseTransformer = pair.left(); CompletableFuture endOfStreamFuture = pair.right(); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(true) - .isPayloadJson(false).build(); + .isPayloadJson(false).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, StreamingOutputOperationResponse::builder); - + operationMetadata, StreamingOutputOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler.execute( - new ClientExecutionParams() - .withOperationName("StreamingOutputOperation").withProtocolMetadata(protocolMetadata) - .withMarshaller(new StreamingOutputOperationRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(streamingOutputOperationRequest), asyncResponseTransformer); + new ClientExecutionParams() + .withOperationName("StreamingOutputOperation").withProtocolMetadata(protocolMetadata) + .withMarshaller(new StreamingOutputOperationRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(streamingOutputOperationRequest), asyncResponseTransformer); AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { if (e != null) { runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(e)); + () -> finalAsyncResponseTransformer.exceptionOccurred(e)); } endOfStreamFuture.whenComplete((r2, e2) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); @@ -1358,7 +1536,7 @@ public CompletableFuture streamingOutputOperation( } catch (Throwable t) { AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(t)); + () -> finalAsyncResponseTransformer.exceptionOccurred(t)); metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); return CompletableFutureUtils.failedFuture(t); } @@ -1380,18 +1558,12 @@ public final String serviceName() { } private > T init(T builder) { - return builder - .clientConfiguration(clientConfiguration) - .defaultServiceExceptionSupplier(JsonException::builder) - .protocol(AwsJsonProtocol.REST_JSON) - .protocolVersion("1.1") - .registerModeledException( - ExceptionMetadata.builder().errorCode("InvalidInput") - .exceptionBuilderSupplier(InvalidInputException::builder).httpStatusCode(400).build()); + return builder.clientConfiguration(clientConfiguration).defaultServiceExceptionSupplier(JsonException::builder) + .protocol(AwsJsonProtocol.REST_JSON).protocolVersion("1.1"); } private static List resolveMetricPublishers(SdkClientConfiguration clientConfiguration, - RequestOverrideConfiguration requestOverrideConfiguration) { + RequestOverrideConfiguration requestOverrideConfiguration) { List publishers = null; if (requestOverrideConfiguration != null) { publishers = requestOverrideConfiguration.metricPublishers(); @@ -1443,12 +1615,7 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, } private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata) { - return protocolFactory.createErrorResponseHandler(operationMetadata); - } - - private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { + JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { return protocolFactory.createErrorResponseHandler(operationMetadata, exceptionMetadataMapper); } diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/sra/test-json-client-class.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/sra/test-json-client-class.java index 2ac6c6ae6d23..746f4f882f01 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/sra/test-json-client-class.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/sra/test-json-client-class.java @@ -2,7 +2,9 @@ import java.util.Collections; import java.util.List; +import java.util.Optional; import java.util.function.Consumer; +import java.util.function.Function; import software.amazon.awssdk.annotations.Generated; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.awscore.client.handler.AwsSyncClientHandler; @@ -138,9 +140,20 @@ public APostOperationResponse aPostOperation(APostOperationRequest aPostOperatio HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, APostOperationResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(aPostOperationRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, aPostOperationRequest .overrideConfiguration().orElse(null)); @@ -194,9 +207,20 @@ public APostOperationWithOutputResponse aPostOperationWithOutput( HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, APostOperationWithOutputResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(aPostOperationWithOutputRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, aPostOperationWithOutputRequest @@ -243,9 +267,20 @@ public BearerAuthOperationResponse bearerAuthOperation(BearerAuthOperationReques HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, BearerAuthOperationResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(bearerAuthOperationRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, bearerAuthOperationRequest @@ -292,9 +327,20 @@ public GetOperationWithChecksumResponse getOperationWithChecksum( HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, GetOperationWithChecksumResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(getOperationWithChecksumRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, getOperationWithChecksumRequest @@ -354,9 +400,20 @@ public GetWithoutRequiredMembersResponse getWithoutRequiredMembers( HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, GetWithoutRequiredMembersResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(getWithoutRequiredMembersRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, getWithoutRequiredMembersRequest @@ -404,9 +461,20 @@ public OperationWithChecksumRequiredResponse operationWithChecksumRequired( HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, OperationWithChecksumRequiredResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(operationWithChecksumRequiredRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, @@ -459,9 +527,20 @@ public OperationWithRequestCompressionResponse operationWithRequestCompression( HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, OperationWithRequestCompressionResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(operationWithRequestCompressionRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, @@ -514,9 +593,20 @@ public PaginatedOperationWithResultKeyResponse paginatedOperationWithResultKey( HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, PaginatedOperationWithResultKeyResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(paginatedOperationWithResultKeyRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, @@ -564,9 +654,20 @@ public PaginatedOperationWithoutResultKeyResponse paginatedOperationWithoutResul HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, PaginatedOperationWithoutResultKeyResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(paginatedOperationWithoutResultKeyRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, @@ -640,9 +741,20 @@ public ReturnT putOperationWithChecksum(PutOperationWithChecksumReques HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, PutOperationWithChecksumResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(putOperationWithChecksumRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, putOperationWithChecksumRequest @@ -690,11 +802,11 @@ public ReturnT putOperationWithChecksum(PutOperationWithChecksumReques * The content to send to the service. A {@link RequestBody} can be created using one of several factory * methods for various sources of data. For example, to create a request body from a file you can do the * following. - * + * *
      * {@code RequestBody.fromFile(new File("myfile.txt"))}
      * 
- * + * * See documentation in {@link RequestBody} for additional details and which sources of data are supported. * The service documentation for the request content is as follows 'This be a stream' * @return Result of the StreamingInputOperation operation returned by the service. @@ -717,9 +829,20 @@ public StreamingInputOperationResponse streamingInputOperation(StreamingInputOpe HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, StreamingInputOperationResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(streamingInputOperationRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, streamingInputOperationRequest @@ -757,11 +880,11 @@ public StreamingInputOperationResponse streamingInputOperation(StreamingInputOpe * The content to send to the service. A {@link RequestBody} can be created using one of several factory * methods for various sources of data. For example, to create a request body from a file you can do the * following. - * + * *
      * {@code RequestBody.fromFile(new File("myfile.txt"))}
      * 
- * + * * See documentation in {@link RequestBody} for additional details and which sources of data are supported. * The service documentation for the request content is as follows 'This be a stream' * @param responseTransformer @@ -793,9 +916,20 @@ public ReturnT streamingInputOutputOperation( HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, StreamingInputOutputOperationResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(streamingInputOutputOperationRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, @@ -859,9 +993,20 @@ public ReturnT streamingOutputOperation(StreamingOutputOperationReques HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, StreamingOutputOperationResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(streamingOutputOperationRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, streamingOutputOperationRequest @@ -913,8 +1058,8 @@ private static List resolveMetricPublishers(SdkClientConfigurat } private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata) { - return protocolFactory.createErrorResponseHandler(operationMetadata); + JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { + return protocolFactory.createErrorResponseHandler(operationMetadata, exceptionMetadataMapper); } private void updateRetryStrategyClientConfiguration(SdkClientConfiguration.Builder configuration) { @@ -955,14 +1100,8 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, } private > T init(T builder) { - return builder - .clientConfiguration(clientConfiguration) - .defaultServiceExceptionSupplier(JsonException::builder) - .protocol(AwsJsonProtocol.REST_JSON) - .protocolVersion("1.1") - .registerModeledException( - ExceptionMetadata.builder().errorCode("InvalidInput") - .exceptionBuilderSupplier(InvalidInputException::builder).httpStatusCode(400).build()); + return builder.clientConfiguration(clientConfiguration).defaultServiceExceptionSupplier(JsonException::builder) + .protocol(AwsJsonProtocol.REST_JSON).protocolVersion("1.1"); } @Override diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-aws-json-async-client-class.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-aws-json-async-client-class.java index 9384c1c5d766..8a79e84d719a 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-aws-json-async-client-class.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-aws-json-async-client-class.java @@ -134,7 +134,7 @@ final class DefaultJsonAsyncClient implements JsonAsyncClient { private static final Logger log = LoggerFactory.getLogger(DefaultJsonAsyncClient.class); private static final AwsProtocolMetadata protocolMetadata = AwsProtocolMetadata.builder() - .serviceProtocol(AwsServiceProtocol.AWS_JSON).build(); + .serviceProtocol(AwsServiceProtocol.AWS_JSON).build(); private final AsyncClientHandler clientHandler; @@ -184,32 +184,46 @@ public JsonUtilities utilities() { public CompletableFuture aPostOperation(APostOperationRequest aPostOperationRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(aPostOperationRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, aPostOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "APostOperation"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, APostOperationResponse::builder); - + operationMetadata, APostOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); String hostPrefix = "{StringMember}-foo."; HostnameValidator.validateHostnameCompliant(aPostOperationRequest.stringMember(), "StringMember", - "aPostOperationRequest"); + "aPostOperationRequest"); String resolvedHostExpression = String.format("%s-foo.", aPostOperationRequest.stringMember()); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("APostOperation").withProtocolMetadata(protocolMetadata) - .withMarshaller(new APostOperationRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .hostPrefixExpression(resolvedHostExpression).withInput(aPostOperationRequest)); + .execute(new ClientExecutionParams() + .withOperationName("APostOperation").withProtocolMetadata(protocolMetadata) + .withMarshaller(new APostOperationRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .hostPrefixExpression(resolvedHostExpression).withInput(aPostOperationRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -248,32 +262,46 @@ public CompletableFuture aPostOperation(APostOperationRe */ @Override public CompletableFuture aPostOperationWithOutput( - APostOperationWithOutputRequest aPostOperationWithOutputRequest) { + APostOperationWithOutputRequest aPostOperationWithOutputRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(aPostOperationWithOutputRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, aPostOperationWithOutputRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "APostOperationWithOutput"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, APostOperationWithOutputResponse::builder); - + operationMetadata, APostOperationWithOutputResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("APostOperationWithOutput").withProtocolMetadata(protocolMetadata) - .withMarshaller(new APostOperationWithOutputRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(aPostOperationWithOutputRequest)); + .execute(new ClientExecutionParams() + .withOperationName("APostOperationWithOutput").withProtocolMetadata(protocolMetadata) + .withMarshaller(new APostOperationWithOutputRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(aPostOperationWithOutputRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -307,67 +335,82 @@ public CompletableFuture aPostOperationWithOut */ @Override public CompletableFuture eventStreamOperation(EventStreamOperationRequest eventStreamOperationRequest, - Publisher requestStream, EventStreamOperationResponseHandler asyncResponseHandler) { + Publisher requestStream, EventStreamOperationResponseHandler asyncResponseHandler) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(eventStreamOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, eventStreamOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "EventStreamOperation"); eventStreamOperationRequest = applySignerOverride(eventStreamOperationRequest, EventStreamAws4Signer.create()); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = new AttachHttpMetadataResponseHandler( - protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationResponse::builder)); + protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationResponse::builder)); HttpResponseHandler voidResponseHandler = protocolFactory.createResponseHandler(JsonOperationMetadata - .builder().isPayloadJson(false).hasStreamingSuccessResponse(true).build(), VoidSdkResponse::builder); + .builder().isPayloadJson(false).hasStreamingSuccessResponse(true).build(), VoidSdkResponse::builder); HttpResponseHandler eventResponseHandler = protocolFactory.createResponseHandler( - JsonOperationMetadata.builder().isPayloadJson(true).hasStreamingSuccessResponse(false).build(), - EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventOne", EventStream::eventOneBuilder) - .putSdkPojoSupplier("EventTheSecond", EventStream::eventTheSecondBuilder) - .putSdkPojoSupplier("secondEventOne", EventStream::secondEventOneBuilder) - .putSdkPojoSupplier("eventThree", EventStream::eventThreeBuilder) - .defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN)).build()); + JsonOperationMetadata.builder().isPayloadJson(true).hasStreamingSuccessResponse(false).build(), + EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventOne", EventStream::eventOneBuilder) + .putSdkPojoSupplier("EventTheSecond", EventStream::eventTheSecondBuilder) + .putSdkPojoSupplier("secondEventOne", EventStream::secondEventOneBuilder) + .putSdkPojoSupplier("eventThree", EventStream::eventThreeBuilder) + .defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN)).build()); Function> eventstreamExceptionMetadataMapper = errorCode -> { switch (errorCode) { - default: - return Optional.empty(); + default: + return Optional.empty(); } }; HttpResponseHandler errorEventResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata, eventstreamExceptionMetadataMapper); + operationMetadata, eventstreamExceptionMetadataMapper); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); EventStreamTaggedUnionJsonMarshaller eventMarshaller = EventStreamTaggedUnionJsonMarshaller.builder() - .putMarshaller(DefaultInputEvent.class, new InputEventMarshaller(protocolFactory)).build(); + .putMarshaller(DefaultInputEvent.class, new InputEventMarshaller(protocolFactory)).build(); SdkPublisher eventPublisher = SdkPublisher.adapt(requestStream); Publisher adapted = eventPublisher.map(event -> eventMarshaller.marshall(event)).map( - AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer); + AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer); CompletableFuture future = new CompletableFuture<>(); EventStreamAsyncResponseTransformer asyncResponseTransformer = EventStreamAsyncResponseTransformer - . builder().eventStreamResponseHandler(asyncResponseHandler) - .eventResponseHandler(eventResponseHandler).initialResponseHandler(responseHandler) - .exceptionResponseHandler(errorEventResponseHandler).future(future).executor(executor) - .serviceName(serviceName()).build(); + . builder().eventStreamResponseHandler(asyncResponseHandler) + .eventResponseHandler(eventResponseHandler).initialResponseHandler(responseHandler) + .exceptionResponseHandler(errorEventResponseHandler).future(future).executor(executor) + .serviceName(serviceName()).build(); CompletableFuture executeFuture = clientHandler.execute( - new ClientExecutionParams() - .withOperationName("EventStreamOperation").withProtocolMetadata(protocolMetadata) - .withMarshaller(new EventStreamOperationRequestMarshaller(protocolFactory)) - .withAsyncRequestBody(AsyncRequestBody.fromPublisher(adapted)).withFullDuplex(true) - .withInitialRequestEvent(true).withResponseHandler(voidResponseHandler) - .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector).withInput(eventStreamOperationRequest), - asyncResponseTransformer); + new ClientExecutionParams() + .withOperationName("EventStreamOperation").withProtocolMetadata(protocolMetadata) + .withMarshaller(new EventStreamOperationRequestMarshaller(protocolFactory)) + .withAsyncRequestBody(AsyncRequestBody.fromPublisher(adapted)).withFullDuplex(true) + .withInitialRequestEvent(true).withResponseHandler(voidResponseHandler) + .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector).withInput(eventStreamOperationRequest), + asyncResponseTransformer); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { if (e != null) { try { @@ -382,7 +425,7 @@ public CompletableFuture eventStreamOperation(EventStreamOperationRequest return CompletableFutureUtils.forwardExceptionTo(future, executeFuture); } catch (Throwable t) { runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> asyncResponseHandler.exceptionOccurred(t)); + () -> asyncResponseHandler.exceptionOccurred(t)); metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); return CompletableFutureUtils.failedFuture(t); } @@ -411,42 +454,56 @@ public CompletableFuture eventStreamOperation(EventStreamOperationRequest */ @Override public CompletableFuture eventStreamOperationWithOnlyInput( - EventStreamOperationWithOnlyInputRequest eventStreamOperationWithOnlyInputRequest, - Publisher requestStream) { + EventStreamOperationWithOnlyInputRequest eventStreamOperationWithOnlyInputRequest, + Publisher requestStream) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(eventStreamOperationWithOnlyInputRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - eventStreamOperationWithOnlyInputRequest.overrideConfiguration().orElse(null)); + eventStreamOperationWithOnlyInputRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "EventStreamOperationWithOnlyInput"); eventStreamOperationWithOnlyInputRequest = applySignerOverride(eventStreamOperationWithOnlyInputRequest, - EventStreamAws4Signer.create()); + EventStreamAws4Signer.create()); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory - .createResponseHandler(operationMetadata, EventStreamOperationWithOnlyInputResponse::builder); - + .createResponseHandler(operationMetadata, EventStreamOperationWithOnlyInputResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); EventStreamTaggedUnionJsonMarshaller eventMarshaller = EventStreamTaggedUnionJsonMarshaller.builder() - .putMarshaller(DefaultInputEventOne.class, new InputEventMarshaller(protocolFactory)) - .putMarshaller(DefaultInputEventTwo.class, new InputEventTwoMarshaller(protocolFactory)).build(); + .putMarshaller(DefaultInputEventOne.class, new InputEventMarshaller(protocolFactory)) + .putMarshaller(DefaultInputEventTwo.class, new InputEventTwoMarshaller(protocolFactory)).build(); SdkPublisher eventPublisher = SdkPublisher.adapt(requestStream); Publisher adapted = eventPublisher.map(event -> eventMarshaller.marshall(event)).map( - AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer); + AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("EventStreamOperationWithOnlyInput").withProtocolMetadata(protocolMetadata) - .withMarshaller(new EventStreamOperationWithOnlyInputRequestMarshaller(protocolFactory)) - .withAsyncRequestBody(AsyncRequestBody.fromPublisher(adapted)).withInitialRequestEvent(true) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(eventStreamOperationWithOnlyInputRequest)); + .execute(new ClientExecutionParams() + .withOperationName("EventStreamOperationWithOnlyInput").withProtocolMetadata(protocolMetadata) + .withMarshaller(new EventStreamOperationWithOnlyInputRequestMarshaller(protocolFactory)) + .withAsyncRequestBody(AsyncRequestBody.fromPublisher(adapted)).withInitialRequestEvent(true) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(eventStreamOperationWithOnlyInputRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -481,60 +538,75 @@ public CompletableFuture eventStreamO */ @Override public CompletableFuture eventStreamOperationWithOnlyOutput( - EventStreamOperationWithOnlyOutputRequest eventStreamOperationWithOnlyOutputRequest, - EventStreamOperationWithOnlyOutputResponseHandler asyncResponseHandler) { + EventStreamOperationWithOnlyOutputRequest eventStreamOperationWithOnlyOutputRequest, + EventStreamOperationWithOnlyOutputResponseHandler asyncResponseHandler) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(eventStreamOperationWithOnlyOutputRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - eventStreamOperationWithOnlyOutputRequest.overrideConfiguration().orElse(null)); + eventStreamOperationWithOnlyOutputRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "EventStreamOperationWithOnlyOutput"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = new AttachHttpMetadataResponseHandler( - protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationWithOnlyOutputResponse::builder)); + protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationWithOnlyOutputResponse::builder)); HttpResponseHandler voidResponseHandler = protocolFactory.createResponseHandler(JsonOperationMetadata - .builder().isPayloadJson(false).hasStreamingSuccessResponse(true).build(), VoidSdkResponse::builder); + .builder().isPayloadJson(false).hasStreamingSuccessResponse(true).build(), VoidSdkResponse::builder); HttpResponseHandler eventResponseHandler = protocolFactory.createResponseHandler( - JsonOperationMetadata.builder().isPayloadJson(true).hasStreamingSuccessResponse(false).build(), - EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventOne", EventStream::eventOneBuilder) - .putSdkPojoSupplier("EventTheSecond", EventStream::eventTheSecondBuilder) - .putSdkPojoSupplier("secondEventOne", EventStream::secondEventOneBuilder) - .putSdkPojoSupplier("eventThree", EventStream::eventThreeBuilder) - .defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN)).build()); + JsonOperationMetadata.builder().isPayloadJson(true).hasStreamingSuccessResponse(false).build(), + EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventOne", EventStream::eventOneBuilder) + .putSdkPojoSupplier("EventTheSecond", EventStream::eventTheSecondBuilder) + .putSdkPojoSupplier("secondEventOne", EventStream::secondEventOneBuilder) + .putSdkPojoSupplier("eventThree", EventStream::eventThreeBuilder) + .defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN)).build()); Function> eventstreamExceptionMetadataMapper = errorCode -> { switch (errorCode) { - default: - return Optional.empty(); + default: + return Optional.empty(); } }; HttpResponseHandler errorEventResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata, eventstreamExceptionMetadataMapper); + operationMetadata, eventstreamExceptionMetadataMapper); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture future = new CompletableFuture<>(); EventStreamAsyncResponseTransformer asyncResponseTransformer = EventStreamAsyncResponseTransformer - . builder() - .eventStreamResponseHandler(asyncResponseHandler).eventResponseHandler(eventResponseHandler) - .initialResponseHandler(responseHandler).exceptionResponseHandler(errorEventResponseHandler).future(future) - .executor(executor).serviceName(serviceName()).build(); + . builder() + .eventStreamResponseHandler(asyncResponseHandler).eventResponseHandler(eventResponseHandler) + .initialResponseHandler(responseHandler).exceptionResponseHandler(errorEventResponseHandler).future(future) + .executor(executor).serviceName(serviceName()).build(); CompletableFuture executeFuture = clientHandler.execute( - new ClientExecutionParams() - .withOperationName("EventStreamOperationWithOnlyOutput").withProtocolMetadata(protocolMetadata) - .withMarshaller(new EventStreamOperationWithOnlyOutputRequestMarshaller(protocolFactory)) - .withResponseHandler(voidResponseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(eventStreamOperationWithOnlyOutputRequest), asyncResponseTransformer); + new ClientExecutionParams() + .withOperationName("EventStreamOperationWithOnlyOutput").withProtocolMetadata(protocolMetadata) + .withMarshaller(new EventStreamOperationWithOnlyOutputRequestMarshaller(protocolFactory)) + .withResponseHandler(voidResponseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(eventStreamOperationWithOnlyOutputRequest), asyncResponseTransformer); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { if (e != null) { try { @@ -549,7 +621,7 @@ public CompletableFuture eventStreamOperationWithOnlyOutput( return CompletableFutureUtils.forwardExceptionTo(future, executeFuture); } catch (Throwable t) { runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> asyncResponseHandler.exceptionOccurred(t)); + () -> asyncResponseHandler.exceptionOccurred(t)); metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); return CompletableFutureUtils.failedFuture(t); } @@ -581,32 +653,46 @@ public CompletableFuture eventStreamOperationWithOnlyOutput( */ @Override public CompletableFuture getWithoutRequiredMembers( - GetWithoutRequiredMembersRequest getWithoutRequiredMembersRequest) { + GetWithoutRequiredMembersRequest getWithoutRequiredMembersRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(getWithoutRequiredMembersRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, getWithoutRequiredMembersRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "GetWithoutRequiredMembers"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, GetWithoutRequiredMembersResponse::builder); - + operationMetadata, GetWithoutRequiredMembersResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("GetWithoutRequiredMembers").withProtocolMetadata(protocolMetadata) - .withMarshaller(new GetWithoutRequiredMembersRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(getWithoutRequiredMembersRequest)); + .execute(new ClientExecutionParams() + .withOperationName("GetWithoutRequiredMembers").withProtocolMetadata(protocolMetadata) + .withMarshaller(new GetWithoutRequiredMembersRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(getWithoutRequiredMembersRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -641,36 +727,50 @@ public CompletableFuture getWithoutRequiredMe */ @Override public CompletableFuture operationWithChecksumRequired( - OperationWithChecksumRequiredRequest operationWithChecksumRequiredRequest) { + OperationWithChecksumRequiredRequest operationWithChecksumRequiredRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(operationWithChecksumRequiredRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - operationWithChecksumRequiredRequest.overrideConfiguration().orElse(null)); + operationWithChecksumRequiredRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "OperationWithChecksumRequired"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OperationWithChecksumRequiredResponse::builder); - + operationMetadata, OperationWithChecksumRequiredResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("OperationWithChecksumRequired") - .withProtocolMetadata(protocolMetadata) - .withMarshaller(new OperationWithChecksumRequiredRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector) - .putExecutionAttribute(SdkInternalExecutionAttribute.HTTP_CHECKSUM_REQUIRED, - HttpChecksumRequired.create()).withInput(operationWithChecksumRequiredRequest)); + .execute(new ClientExecutionParams() + .withOperationName("OperationWithChecksumRequired") + .withProtocolMetadata(protocolMetadata) + .withMarshaller(new OperationWithChecksumRequiredRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector) + .putExecutionAttribute(SdkInternalExecutionAttribute.HTTP_CHECKSUM_REQUIRED, + HttpChecksumRequired.create()).withInput(operationWithChecksumRequiredRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -704,33 +804,47 @@ public CompletableFuture operationWithChe */ @Override public CompletableFuture operationWithNoneAuthType( - OperationWithNoneAuthTypeRequest operationWithNoneAuthTypeRequest) { + OperationWithNoneAuthTypeRequest operationWithNoneAuthTypeRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(operationWithNoneAuthTypeRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, operationWithNoneAuthTypeRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "OperationWithNoneAuthType"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OperationWithNoneAuthTypeResponse::builder); - + operationMetadata, OperationWithNoneAuthTypeResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("OperationWithNoneAuthType").withProtocolMetadata(protocolMetadata) - .withMarshaller(new OperationWithNoneAuthTypeRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .putExecutionAttribute(SdkInternalExecutionAttribute.IS_NONE_AUTH_TYPE_REQUEST, false) - .withInput(operationWithNoneAuthTypeRequest)); + .execute(new ClientExecutionParams() + .withOperationName("OperationWithNoneAuthType").withProtocolMetadata(protocolMetadata) + .withMarshaller(new OperationWithNoneAuthTypeRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .putExecutionAttribute(SdkInternalExecutionAttribute.IS_NONE_AUTH_TYPE_REQUEST, false) + .withInput(operationWithNoneAuthTypeRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -765,37 +879,51 @@ public CompletableFuture operationWithNoneAut */ @Override public CompletableFuture operationWithRequestCompression( - OperationWithRequestCompressionRequest operationWithRequestCompressionRequest) { + OperationWithRequestCompressionRequest operationWithRequestCompressionRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(operationWithRequestCompressionRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - operationWithRequestCompressionRequest.overrideConfiguration().orElse(null)); + operationWithRequestCompressionRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "OperationWithRequestCompression"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OperationWithRequestCompressionResponse::builder); - + operationMetadata, OperationWithRequestCompressionResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("OperationWithRequestCompression") - .withProtocolMetadata(protocolMetadata) - .withMarshaller(new OperationWithRequestCompressionRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector) - .putExecutionAttribute(SdkInternalExecutionAttribute.REQUEST_COMPRESSION, - RequestCompression.builder().encodings("gzip").isStreaming(false).build()) - .withInput(operationWithRequestCompressionRequest)); + .execute(new ClientExecutionParams() + .withOperationName("OperationWithRequestCompression") + .withProtocolMetadata(protocolMetadata) + .withMarshaller(new OperationWithRequestCompressionRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector) + .putExecutionAttribute(SdkInternalExecutionAttribute.REQUEST_COMPRESSION, + RequestCompression.builder().encodings("gzip").isStreaming(false).build()) + .withInput(operationWithRequestCompressionRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -830,32 +958,46 @@ public CompletableFuture operationWithR */ @Override public CompletableFuture paginatedOperationWithResultKey( - PaginatedOperationWithResultKeyRequest paginatedOperationWithResultKeyRequest) { + PaginatedOperationWithResultKeyRequest paginatedOperationWithResultKeyRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(paginatedOperationWithResultKeyRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - paginatedOperationWithResultKeyRequest.overrideConfiguration().orElse(null)); + paginatedOperationWithResultKeyRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "PaginatedOperationWithResultKey"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, PaginatedOperationWithResultKeyResponse::builder); - + operationMetadata, PaginatedOperationWithResultKeyResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("PaginatedOperationWithResultKey").withProtocolMetadata(protocolMetadata) - .withMarshaller(new PaginatedOperationWithResultKeyRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(paginatedOperationWithResultKeyRequest)); + .execute(new ClientExecutionParams() + .withOperationName("PaginatedOperationWithResultKey").withProtocolMetadata(protocolMetadata) + .withMarshaller(new PaginatedOperationWithResultKeyRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(paginatedOperationWithResultKeyRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -890,32 +1032,46 @@ public CompletableFuture paginatedOpera */ @Override public CompletableFuture paginatedOperationWithoutResultKey( - PaginatedOperationWithoutResultKeyRequest paginatedOperationWithoutResultKeyRequest) { + PaginatedOperationWithoutResultKeyRequest paginatedOperationWithoutResultKeyRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(paginatedOperationWithoutResultKeyRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - paginatedOperationWithoutResultKeyRequest.overrideConfiguration().orElse(null)); + paginatedOperationWithoutResultKeyRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "PaginatedOperationWithoutResultKey"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory - .createResponseHandler(operationMetadata, PaginatedOperationWithoutResultKeyResponse::builder); - + .createResponseHandler(operationMetadata, PaginatedOperationWithoutResultKeyResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("PaginatedOperationWithoutResultKey").withProtocolMetadata(protocolMetadata) - .withMarshaller(new PaginatedOperationWithoutResultKeyRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(paginatedOperationWithoutResultKeyRequest)); + .execute(new ClientExecutionParams() + .withOperationName("PaginatedOperationWithoutResultKey").withProtocolMetadata(protocolMetadata) + .withMarshaller(new PaginatedOperationWithoutResultKeyRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(paginatedOperationWithoutResultKeyRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -954,13 +1110,13 @@ public CompletableFuture paginatedOp */ @Override public CompletableFuture streamingInputOperation( - StreamingInputOperationRequest streamingInputOperationRequest, AsyncRequestBody requestBody) { + StreamingInputOperationRequest streamingInputOperationRequest, AsyncRequestBody requestBody) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(streamingInputOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, streamingInputOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "StreamingInputOperation"); @@ -968,25 +1124,39 @@ public CompletableFuture streamingInputOperatio streamingInputOperationRequest = applySignerOverride(streamingInputOperationRequest, AsyncAws4Signer.create()); } JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, StreamingInputOperationResponse::builder); - + operationMetadata, StreamingInputOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("StreamingInputOperation") - .withProtocolMetadata(protocolMetadata) - .withMarshaller( - AsyncStreamingRequestMarshaller.builder() - .delegateMarshaller(new StreamingInputOperationRequestMarshaller(protocolFactory)) - .asyncRequestBody(requestBody).build()).withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector).withAsyncRequestBody(requestBody) - .withInput(streamingInputOperationRequest)); + .execute(new ClientExecutionParams() + .withOperationName("StreamingInputOperation") + .withProtocolMetadata(protocolMetadata) + .withMarshaller( + AsyncStreamingRequestMarshaller.builder() + .delegateMarshaller(new StreamingInputOperationRequestMarshaller(protocolFactory)) + .asyncRequestBody(requestBody).build()).withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector).withAsyncRequestBody(requestBody) + .withInput(streamingInputOperationRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -1030,51 +1200,65 @@ public CompletableFuture streamingInputOperatio */ @Override public CompletableFuture streamingInputOutputOperation( - StreamingInputOutputOperationRequest streamingInputOutputOperationRequest, AsyncRequestBody requestBody, - AsyncResponseTransformer asyncResponseTransformer) { + StreamingInputOutputOperationRequest streamingInputOutputOperationRequest, AsyncRequestBody requestBody, + AsyncResponseTransformer asyncResponseTransformer) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(streamingInputOutputOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - streamingInputOutputOperationRequest.overrideConfiguration().orElse(null)); + streamingInputOutputOperationRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "StreamingInputOutputOperation"); Pair, CompletableFuture> pair = AsyncResponseTransformerUtils - .wrapWithEndOfStreamFuture(asyncResponseTransformer); + .wrapWithEndOfStreamFuture(asyncResponseTransformer); asyncResponseTransformer = pair.left(); CompletableFuture endOfStreamFuture = pair.right(); streamingInputOutputOperationRequest = applySignerOverride(streamingInputOutputOperationRequest, - Aws4UnsignedPayloadSigner.create()); + Aws4UnsignedPayloadSigner.create()); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(true) - .isPayloadJson(false).build(); + .isPayloadJson(false).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, StreamingInputOutputOperationResponse::builder); - + operationMetadata, StreamingInputOutputOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler.execute( - new ClientExecutionParams() - .withOperationName("StreamingInputOutputOperation") - .withProtocolMetadata(protocolMetadata) - .withMarshaller( - AsyncStreamingRequestMarshaller - .builder() - .delegateMarshaller( - new StreamingInputOutputOperationRequestMarshaller(protocolFactory)) - .asyncRequestBody(requestBody).transferEncoding(true).build()) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withAsyncRequestBody(requestBody).withInput(streamingInputOutputOperationRequest), - asyncResponseTransformer); + new ClientExecutionParams() + .withOperationName("StreamingInputOutputOperation") + .withProtocolMetadata(protocolMetadata) + .withMarshaller( + AsyncStreamingRequestMarshaller + .builder() + .delegateMarshaller( + new StreamingInputOutputOperationRequestMarshaller(protocolFactory)) + .asyncRequestBody(requestBody).transferEncoding(true).build()) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withAsyncRequestBody(requestBody).withInput(streamingInputOutputOperationRequest), + asyncResponseTransformer); AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { if (e != null) { runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(e)); + () -> finalAsyncResponseTransformer.exceptionOccurred(e)); } endOfStreamFuture.whenComplete((r2, e2) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); @@ -1085,7 +1269,7 @@ public CompletableFuture streamingInputOutputOperation( } catch (Throwable t) { AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(t)); + () -> finalAsyncResponseTransformer.exceptionOccurred(t)); metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); return CompletableFutureUtils.failedFuture(t); } @@ -1118,42 +1302,56 @@ public CompletableFuture streamingInputOutputOperation( */ @Override public CompletableFuture streamingOutputOperation( - StreamingOutputOperationRequest streamingOutputOperationRequest, - AsyncResponseTransformer asyncResponseTransformer) { + StreamingOutputOperationRequest streamingOutputOperationRequest, + AsyncResponseTransformer asyncResponseTransformer) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(streamingOutputOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, streamingOutputOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "StreamingOutputOperation"); Pair, CompletableFuture> pair = AsyncResponseTransformerUtils - .wrapWithEndOfStreamFuture(asyncResponseTransformer); + .wrapWithEndOfStreamFuture(asyncResponseTransformer); asyncResponseTransformer = pair.left(); CompletableFuture endOfStreamFuture = pair.right(); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(true) - .isPayloadJson(false).build(); + .isPayloadJson(false).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, StreamingOutputOperationResponse::builder); - + operationMetadata, StreamingOutputOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler.execute( - new ClientExecutionParams() - .withOperationName("StreamingOutputOperation").withProtocolMetadata(protocolMetadata) - .withMarshaller(new StreamingOutputOperationRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(streamingOutputOperationRequest), asyncResponseTransformer); + new ClientExecutionParams() + .withOperationName("StreamingOutputOperation").withProtocolMetadata(protocolMetadata) + .withMarshaller(new StreamingOutputOperationRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(streamingOutputOperationRequest), asyncResponseTransformer); AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { if (e != null) { runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(e)); + () -> finalAsyncResponseTransformer.exceptionOccurred(e)); } endOfStreamFuture.whenComplete((r2, e2) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); @@ -1164,7 +1362,7 @@ public CompletableFuture streamingOutputOperation( } catch (Throwable t) { AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(t)); + () -> finalAsyncResponseTransformer.exceptionOccurred(t)); metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); return CompletableFutureUtils.failedFuture(t); } @@ -1181,21 +1379,12 @@ public final String serviceName() { } private > T init(T builder) { - return builder - .clientConfiguration(clientConfiguration) - .defaultServiceExceptionSupplier(JsonException::builder) - .protocol(AwsJsonProtocol.AWS_JSON) - .protocolVersion("1.1") - .registerModeledException( - ExceptionMetadata.builder().errorCode("InvalidInputException") - .exceptionBuilderSupplier(InvalidInputException::builder).httpStatusCode(400).build()) - .registerModeledException( - ExceptionMetadata.builder().errorCode("ServiceFaultException") - .exceptionBuilderSupplier(ServiceFaultException::builder).httpStatusCode(500).build()); + return builder.clientConfiguration(clientConfiguration).defaultServiceExceptionSupplier(JsonException::builder) + .protocol(AwsJsonProtocol.AWS_JSON).protocolVersion("1.1"); } private static List resolveMetricPublishers(SdkClientConfiguration clientConfiguration, - RequestOverrideConfiguration requestOverrideConfiguration) { + RequestOverrideConfiguration requestOverrideConfiguration) { List publishers = null; if (requestOverrideConfiguration != null) { publishers = requestOverrideConfiguration.metricPublishers(); @@ -1215,8 +1404,8 @@ private T applySignerOverride(T request, Signer signer) } Consumer signerOverride = b -> b.signer(signer).build(); AwsRequestOverrideConfiguration overrideConfiguration = request.overrideConfiguration() - .map(c -> c.toBuilder().applyMutation(signerOverride).build()) - .orElse((AwsRequestOverrideConfiguration.builder().applyMutation(signerOverride).build())); + .map(c -> c.toBuilder().applyMutation(signerOverride).build()) + .orElse((AwsRequestOverrideConfiguration.builder().applyMutation(signerOverride).build())); return (T) request.toBuilder().overrideConfiguration(overrideConfiguration).build(); } @@ -1262,12 +1451,7 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, } private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata) { - return protocolFactory.createErrorResponseHandler(operationMetadata); - } - - private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { + JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { return protocolFactory.createErrorResponseHandler(operationMetadata, exceptionMetadataMapper); } diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-aws-query-compatible-json-async-client-class.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-aws-query-compatible-json-async-client-class.java index d06d668892ea..4e035d57ed02 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-aws-query-compatible-json-async-client-class.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-aws-query-compatible-json-async-client-class.java @@ -57,7 +57,7 @@ final class DefaultQueryToJsonCompatibleAsyncClient implements QueryToJsonCompat private static final Logger log = LoggerFactory.getLogger(DefaultQueryToJsonCompatibleAsyncClient.class); private static final AwsProtocolMetadata protocolMetadata = AwsProtocolMetadata.builder() - .serviceProtocol(AwsServiceProtocol.AWS_JSON).build(); + .serviceProtocol(AwsServiceProtocol.AWS_JSON).build(); private final AsyncClientHandler clientHandler; @@ -99,32 +99,43 @@ protected DefaultQueryToJsonCompatibleAsyncClient(SdkClientConfiguration clientC public CompletableFuture aPostOperation(APostOperationRequest aPostOperationRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(aPostOperationRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, aPostOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "QueryToJsonCompatibleService"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "APostOperation"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, APostOperationResponse::builder); - + operationMetadata, APostOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); String hostPrefix = "{StringMember}-foo."; HostnameValidator.validateHostnameCompliant(aPostOperationRequest.stringMember(), "StringMember", - "aPostOperationRequest"); + "aPostOperationRequest"); String resolvedHostExpression = String.format("%s-foo.", aPostOperationRequest.stringMember()); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("APostOperation").withProtocolMetadata(protocolMetadata) - .withMarshaller(new APostOperationRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .hostPrefixExpression(resolvedHostExpression).withInput(aPostOperationRequest)); + .execute(new ClientExecutionParams() + .withOperationName("APostOperation").withProtocolMetadata(protocolMetadata) + .withMarshaller(new APostOperationRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .hostPrefixExpression(resolvedHostExpression).withInput(aPostOperationRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -147,19 +158,13 @@ public final String serviceName() { } private > T init(T builder) { - return builder - .clientConfiguration(clientConfiguration) - .defaultServiceExceptionSupplier(QueryToJsonCompatibleException::builder) - .protocol(AwsJsonProtocol.AWS_JSON) - .protocolVersion("1.1") - .hasAwsQueryCompatible(true) - .registerModeledException( - ExceptionMetadata.builder().errorCode("InvalidInput") - .exceptionBuilderSupplier(InvalidInputException::builder).httpStatusCode(400).build()); + return builder.clientConfiguration(clientConfiguration) + .defaultServiceExceptionSupplier(QueryToJsonCompatibleException::builder).protocol(AwsJsonProtocol.AWS_JSON) + .protocolVersion("1.1").hasAwsQueryCompatible(true); } private static List resolveMetricPublishers(SdkClientConfiguration clientConfiguration, - RequestOverrideConfiguration requestOverrideConfiguration) { + RequestOverrideConfiguration requestOverrideConfiguration) { List publishers = null; if (requestOverrideConfiguration != null) { publishers = requestOverrideConfiguration.metricPublishers(); @@ -203,7 +208,7 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, return configuration.build(); } QueryToJsonCompatibleServiceClientConfigurationBuilder serviceConfigBuilder = new QueryToJsonCompatibleServiceClientConfigurationBuilder( - configuration); + configuration); for (SdkPlugin plugin : plugins) { plugin.configureClient(serviceConfigBuilder); } @@ -212,12 +217,7 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, } private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata) { - return protocolFactory.createErrorResponseHandler(operationMetadata); - } - - private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { + JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { return protocolFactory.createErrorResponseHandler(operationMetadata, exceptionMetadataMapper); } diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-aws-query-compatible-json-sync-client-class.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-aws-query-compatible-json-sync-client-class.java index 78f87a74dde3..aa52553f0bf0 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-aws-query-compatible-json-sync-client-class.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-aws-query-compatible-json-sync-client-class.java @@ -2,7 +2,9 @@ import java.util.Collections; import java.util.List; +import java.util.Optional; import java.util.function.Consumer; +import java.util.function.Function; import software.amazon.awssdk.annotations.Generated; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.awscore.client.handler.AwsSyncClientHandler; @@ -93,9 +95,20 @@ public APostOperationResponse aPostOperation(APostOperationRequest aPostOperatio HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, APostOperationResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(aPostOperationRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, aPostOperationRequest .overrideConfiguration().orElse(null)); @@ -141,8 +154,8 @@ private static List resolveMetricPublishers(SdkClientConfigurat } private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata) { - return protocolFactory.createErrorResponseHandler(operationMetadata); + JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { + return protocolFactory.createErrorResponseHandler(operationMetadata, exceptionMetadataMapper); } private void updateRetryStrategyClientConfiguration(SdkClientConfiguration.Builder configuration) { @@ -184,15 +197,9 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, } private > T init(T builder) { - return builder - .clientConfiguration(clientConfiguration) - .defaultServiceExceptionSupplier(QueryToJsonCompatibleException::builder) - .protocol(AwsJsonProtocol.AWS_JSON) - .protocolVersion("1.1") - .hasAwsQueryCompatible(true) - .registerModeledException( - ExceptionMetadata.builder().errorCode("InvalidInput") - .exceptionBuilderSupplier(InvalidInputException::builder).httpStatusCode(400).build()); + return builder.clientConfiguration(clientConfiguration) + .defaultServiceExceptionSupplier(QueryToJsonCompatibleException::builder).protocol(AwsJsonProtocol.AWS_JSON) + .protocolVersion("1.1").hasAwsQueryCompatible(true); } @Override diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-batchmanager-async.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-batchmanager-async.java index 8081d9331cad..8c13f1420fd1 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-batchmanager-async.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-batchmanager-async.java @@ -57,7 +57,7 @@ final class DefaultBatchManagerTestAsyncClient implements BatchManagerTestAsyncC private static final Logger log = LoggerFactory.getLogger(DefaultBatchManagerTestAsyncClient.class); private static final AwsProtocolMetadata protocolMetadata = AwsProtocolMetadata.builder() - .serviceProtocol(AwsServiceProtocol.REST_JSON).build(); + .serviceProtocol(AwsServiceProtocol.REST_JSON).build(); private final AsyncClientHandler clientHandler; @@ -98,28 +98,36 @@ protected DefaultBatchManagerTestAsyncClient(SdkClientConfiguration clientConfig public CompletableFuture sendRequest(SendRequestRequest sendRequestRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(sendRequestRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, sendRequestRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "BatchManagerTest"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "SendRequest"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, - SendRequestResponse::builder); - + SendRequestResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("SendRequest").withProtocolMetadata(protocolMetadata) - .withMarshaller(new SendRequestRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(sendRequestRequest)); + .execute(new ClientExecutionParams() + .withOperationName("SendRequest").withProtocolMetadata(protocolMetadata) + .withMarshaller(new SendRequestRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(sendRequestRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -148,12 +156,12 @@ public final String serviceName() { private > T init(T builder) { return builder.clientConfiguration(clientConfiguration) - .defaultServiceExceptionSupplier(BatchManagerTestException::builder).protocol(AwsJsonProtocol.REST_JSON) - .protocolVersion("1.1"); + .defaultServiceExceptionSupplier(BatchManagerTestException::builder).protocol(AwsJsonProtocol.REST_JSON) + .protocolVersion("1.1"); } private static List resolveMetricPublishers(SdkClientConfiguration clientConfiguration, - RequestOverrideConfiguration requestOverrideConfiguration) { + RequestOverrideConfiguration requestOverrideConfiguration) { List publishers = null; if (requestOverrideConfiguration != null) { publishers = requestOverrideConfiguration.metricPublishers(); @@ -197,7 +205,7 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, return configuration.build(); } BatchManagerTestServiceClientConfigurationBuilder serviceConfigBuilder = new BatchManagerTestServiceClientConfigurationBuilder( - configuration); + configuration); for (SdkPlugin plugin : plugins) { plugin.configureClient(serviceConfigBuilder); } @@ -206,12 +214,7 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, } private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata) { - return protocolFactory.createErrorResponseHandler(operationMetadata); - } - - private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { + JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { return protocolFactory.createErrorResponseHandler(operationMetadata, exceptionMetadataMapper); } diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-cbor-async-client-class.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-cbor-async-client-class.java index 308abd62a314..8b73813753cf 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-cbor-async-client-class.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-cbor-async-client-class.java @@ -135,7 +135,7 @@ final class DefaultJsonAsyncClient implements JsonAsyncClient { private static final Logger log = LoggerFactory.getLogger(DefaultJsonAsyncClient.class); private static final AwsProtocolMetadata protocolMetadata = AwsProtocolMetadata.builder() - .serviceProtocol(AwsServiceProtocol.CBOR).build(); + .serviceProtocol(AwsServiceProtocol.CBOR).build(); private final AsyncClientHandler clientHandler; @@ -188,32 +188,46 @@ public JsonUtilities utilities() { public CompletableFuture aPostOperation(APostOperationRequest aPostOperationRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(aPostOperationRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, aPostOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "APostOperation"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, APostOperationResponse::builder); - + operationMetadata, APostOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); String hostPrefix = "{StringMember}-foo."; HostnameValidator.validateHostnameCompliant(aPostOperationRequest.stringMember(), "StringMember", - "aPostOperationRequest"); + "aPostOperationRequest"); String resolvedHostExpression = String.format("%s-foo.", aPostOperationRequest.stringMember()); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("APostOperation").withProtocolMetadata(protocolMetadata) - .withMarshaller(new APostOperationRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .hostPrefixExpression(resolvedHostExpression).withInput(aPostOperationRequest)); + .execute(new ClientExecutionParams() + .withOperationName("APostOperation").withProtocolMetadata(protocolMetadata) + .withMarshaller(new APostOperationRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .hostPrefixExpression(resolvedHostExpression).withInput(aPostOperationRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -252,32 +266,46 @@ public CompletableFuture aPostOperation(APostOperationRe */ @Override public CompletableFuture aPostOperationWithOutput( - APostOperationWithOutputRequest aPostOperationWithOutputRequest) { + APostOperationWithOutputRequest aPostOperationWithOutputRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(aPostOperationWithOutputRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, aPostOperationWithOutputRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "APostOperationWithOutput"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, APostOperationWithOutputResponse::builder); - + operationMetadata, APostOperationWithOutputResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("APostOperationWithOutput").withProtocolMetadata(protocolMetadata) - .withMarshaller(new APostOperationWithOutputRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(aPostOperationWithOutputRequest)); + .execute(new ClientExecutionParams() + .withOperationName("APostOperationWithOutput").withProtocolMetadata(protocolMetadata) + .withMarshaller(new APostOperationWithOutputRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(aPostOperationWithOutputRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -311,67 +339,82 @@ public CompletableFuture aPostOperationWithOut */ @Override public CompletableFuture eventStreamOperation(EventStreamOperationRequest eventStreamOperationRequest, - Publisher requestStream, EventStreamOperationResponseHandler asyncResponseHandler) { + Publisher requestStream, EventStreamOperationResponseHandler asyncResponseHandler) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(eventStreamOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, eventStreamOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "EventStreamOperation"); eventStreamOperationRequest = applySignerOverride(eventStreamOperationRequest, EventStreamAws4Signer.create()); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = new AttachHttpMetadataResponseHandler( - protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationResponse::builder)); + protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationResponse::builder)); HttpResponseHandler voidResponseHandler = protocolFactory.createResponseHandler(JsonOperationMetadata - .builder().isPayloadJson(false).hasStreamingSuccessResponse(true).build(), VoidSdkResponse::builder); + .builder().isPayloadJson(false).hasStreamingSuccessResponse(true).build(), VoidSdkResponse::builder); HttpResponseHandler eventResponseHandler = protocolFactory.createResponseHandler( - JsonOperationMetadata.builder().isPayloadJson(true).hasStreamingSuccessResponse(false).build(), - EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventOne", EventStream::eventOneBuilder) - .putSdkPojoSupplier("EventTheSecond", EventStream::eventTheSecondBuilder) - .putSdkPojoSupplier("secondEventOne", EventStream::secondEventOneBuilder) - .putSdkPojoSupplier("eventThree", EventStream::eventThreeBuilder) - .defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN)).build()); + JsonOperationMetadata.builder().isPayloadJson(true).hasStreamingSuccessResponse(false).build(), + EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventOne", EventStream::eventOneBuilder) + .putSdkPojoSupplier("EventTheSecond", EventStream::eventTheSecondBuilder) + .putSdkPojoSupplier("secondEventOne", EventStream::secondEventOneBuilder) + .putSdkPojoSupplier("eventThree", EventStream::eventThreeBuilder) + .defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN)).build()); Function> eventstreamExceptionMetadataMapper = errorCode -> { switch (errorCode) { - default: - return Optional.empty(); + default: + return Optional.empty(); } }; HttpResponseHandler errorEventResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata, eventstreamExceptionMetadataMapper); + operationMetadata, eventstreamExceptionMetadataMapper); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); EventStreamTaggedUnionJsonMarshaller eventMarshaller = EventStreamTaggedUnionJsonMarshaller.builder() - .putMarshaller(DefaultInputEvent.class, new InputEventMarshaller(protocolFactory)).build(); + .putMarshaller(DefaultInputEvent.class, new InputEventMarshaller(protocolFactory)).build(); SdkPublisher eventPublisher = SdkPublisher.adapt(requestStream); Publisher adapted = eventPublisher.map(event -> eventMarshaller.marshall(event)).map( - AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer); + AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer); CompletableFuture future = new CompletableFuture<>(); EventStreamAsyncResponseTransformer asyncResponseTransformer = EventStreamAsyncResponseTransformer - . builder().eventStreamResponseHandler(asyncResponseHandler) - .eventResponseHandler(eventResponseHandler).initialResponseHandler(responseHandler) - .exceptionResponseHandler(errorEventResponseHandler).future(future).executor(executor) - .serviceName(serviceName()).build(); + . builder().eventStreamResponseHandler(asyncResponseHandler) + .eventResponseHandler(eventResponseHandler).initialResponseHandler(responseHandler) + .exceptionResponseHandler(errorEventResponseHandler).future(future).executor(executor) + .serviceName(serviceName()).build(); CompletableFuture executeFuture = clientHandler.execute( - new ClientExecutionParams() - .withOperationName("EventStreamOperation").withProtocolMetadata(protocolMetadata) - .withMarshaller(new EventStreamOperationRequestMarshaller(protocolFactory)) - .withAsyncRequestBody(AsyncRequestBody.fromPublisher(adapted)).withFullDuplex(true) - .withInitialRequestEvent(true).withResponseHandler(voidResponseHandler) - .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector).withInput(eventStreamOperationRequest), - asyncResponseTransformer); + new ClientExecutionParams() + .withOperationName("EventStreamOperation").withProtocolMetadata(protocolMetadata) + .withMarshaller(new EventStreamOperationRequestMarshaller(protocolFactory)) + .withAsyncRequestBody(AsyncRequestBody.fromPublisher(adapted)).withFullDuplex(true) + .withInitialRequestEvent(true).withResponseHandler(voidResponseHandler) + .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector).withInput(eventStreamOperationRequest), + asyncResponseTransformer); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { if (e != null) { try { @@ -386,7 +429,7 @@ public CompletableFuture eventStreamOperation(EventStreamOperationRequest return CompletableFutureUtils.forwardExceptionTo(future, executeFuture); } catch (Throwable t) { runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> asyncResponseHandler.exceptionOccurred(t)); + () -> asyncResponseHandler.exceptionOccurred(t)); metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); return CompletableFutureUtils.failedFuture(t); } @@ -415,42 +458,56 @@ public CompletableFuture eventStreamOperation(EventStreamOperationRequest */ @Override public CompletableFuture eventStreamOperationWithOnlyInput( - EventStreamOperationWithOnlyInputRequest eventStreamOperationWithOnlyInputRequest, - Publisher requestStream) { + EventStreamOperationWithOnlyInputRequest eventStreamOperationWithOnlyInputRequest, + Publisher requestStream) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(eventStreamOperationWithOnlyInputRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - eventStreamOperationWithOnlyInputRequest.overrideConfiguration().orElse(null)); + eventStreamOperationWithOnlyInputRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "EventStreamOperationWithOnlyInput"); eventStreamOperationWithOnlyInputRequest = applySignerOverride(eventStreamOperationWithOnlyInputRequest, - EventStreamAws4Signer.create()); + EventStreamAws4Signer.create()); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory - .createResponseHandler(operationMetadata, EventStreamOperationWithOnlyInputResponse::builder); - + .createResponseHandler(operationMetadata, EventStreamOperationWithOnlyInputResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); EventStreamTaggedUnionJsonMarshaller eventMarshaller = EventStreamTaggedUnionJsonMarshaller.builder() - .putMarshaller(DefaultInputEventOne.class, new InputEventMarshaller(protocolFactory)) - .putMarshaller(DefaultInputEventTwo.class, new InputEventTwoMarshaller(protocolFactory)).build(); + .putMarshaller(DefaultInputEventOne.class, new InputEventMarshaller(protocolFactory)) + .putMarshaller(DefaultInputEventTwo.class, new InputEventTwoMarshaller(protocolFactory)).build(); SdkPublisher eventPublisher = SdkPublisher.adapt(requestStream); Publisher adapted = eventPublisher.map(event -> eventMarshaller.marshall(event)).map( - AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer); + AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("EventStreamOperationWithOnlyInput").withProtocolMetadata(protocolMetadata) - .withMarshaller(new EventStreamOperationWithOnlyInputRequestMarshaller(protocolFactory)) - .withAsyncRequestBody(AsyncRequestBody.fromPublisher(adapted)).withInitialRequestEvent(true) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(eventStreamOperationWithOnlyInputRequest)); + .execute(new ClientExecutionParams() + .withOperationName("EventStreamOperationWithOnlyInput").withProtocolMetadata(protocolMetadata) + .withMarshaller(new EventStreamOperationWithOnlyInputRequestMarshaller(protocolFactory)) + .withAsyncRequestBody(AsyncRequestBody.fromPublisher(adapted)).withInitialRequestEvent(true) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(eventStreamOperationWithOnlyInputRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -485,60 +542,75 @@ public CompletableFuture eventStreamO */ @Override public CompletableFuture eventStreamOperationWithOnlyOutput( - EventStreamOperationWithOnlyOutputRequest eventStreamOperationWithOnlyOutputRequest, - EventStreamOperationWithOnlyOutputResponseHandler asyncResponseHandler) { + EventStreamOperationWithOnlyOutputRequest eventStreamOperationWithOnlyOutputRequest, + EventStreamOperationWithOnlyOutputResponseHandler asyncResponseHandler) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(eventStreamOperationWithOnlyOutputRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - eventStreamOperationWithOnlyOutputRequest.overrideConfiguration().orElse(null)); + eventStreamOperationWithOnlyOutputRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "EventStreamOperationWithOnlyOutput"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = new AttachHttpMetadataResponseHandler( - protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationWithOnlyOutputResponse::builder)); + protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationWithOnlyOutputResponse::builder)); HttpResponseHandler voidResponseHandler = protocolFactory.createResponseHandler(JsonOperationMetadata - .builder().isPayloadJson(false).hasStreamingSuccessResponse(true).build(), VoidSdkResponse::builder); + .builder().isPayloadJson(false).hasStreamingSuccessResponse(true).build(), VoidSdkResponse::builder); HttpResponseHandler eventResponseHandler = protocolFactory.createResponseHandler( - JsonOperationMetadata.builder().isPayloadJson(true).hasStreamingSuccessResponse(false).build(), - EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventOne", EventStream::eventOneBuilder) - .putSdkPojoSupplier("EventTheSecond", EventStream::eventTheSecondBuilder) - .putSdkPojoSupplier("secondEventOne", EventStream::secondEventOneBuilder) - .putSdkPojoSupplier("eventThree", EventStream::eventThreeBuilder) - .defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN)).build()); + JsonOperationMetadata.builder().isPayloadJson(true).hasStreamingSuccessResponse(false).build(), + EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventOne", EventStream::eventOneBuilder) + .putSdkPojoSupplier("EventTheSecond", EventStream::eventTheSecondBuilder) + .putSdkPojoSupplier("secondEventOne", EventStream::secondEventOneBuilder) + .putSdkPojoSupplier("eventThree", EventStream::eventThreeBuilder) + .defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN)).build()); Function> eventstreamExceptionMetadataMapper = errorCode -> { switch (errorCode) { - default: - return Optional.empty(); + default: + return Optional.empty(); } }; HttpResponseHandler errorEventResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata, eventstreamExceptionMetadataMapper); + operationMetadata, eventstreamExceptionMetadataMapper); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture future = new CompletableFuture<>(); EventStreamAsyncResponseTransformer asyncResponseTransformer = EventStreamAsyncResponseTransformer - . builder() - .eventStreamResponseHandler(asyncResponseHandler).eventResponseHandler(eventResponseHandler) - .initialResponseHandler(responseHandler).exceptionResponseHandler(errorEventResponseHandler).future(future) - .executor(executor).serviceName(serviceName()).build(); + . builder() + .eventStreamResponseHandler(asyncResponseHandler).eventResponseHandler(eventResponseHandler) + .initialResponseHandler(responseHandler).exceptionResponseHandler(errorEventResponseHandler).future(future) + .executor(executor).serviceName(serviceName()).build(); CompletableFuture executeFuture = clientHandler.execute( - new ClientExecutionParams() - .withOperationName("EventStreamOperationWithOnlyOutput").withProtocolMetadata(protocolMetadata) - .withMarshaller(new EventStreamOperationWithOnlyOutputRequestMarshaller(protocolFactory)) - .withResponseHandler(voidResponseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(eventStreamOperationWithOnlyOutputRequest), asyncResponseTransformer); + new ClientExecutionParams() + .withOperationName("EventStreamOperationWithOnlyOutput").withProtocolMetadata(protocolMetadata) + .withMarshaller(new EventStreamOperationWithOnlyOutputRequestMarshaller(protocolFactory)) + .withResponseHandler(voidResponseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(eventStreamOperationWithOnlyOutputRequest), asyncResponseTransformer); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { if (e != null) { try { @@ -553,7 +625,7 @@ public CompletableFuture eventStreamOperationWithOnlyOutput( return CompletableFutureUtils.forwardExceptionTo(future, executeFuture); } catch (Throwable t) { runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> asyncResponseHandler.exceptionOccurred(t)); + () -> asyncResponseHandler.exceptionOccurred(t)); metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); return CompletableFutureUtils.failedFuture(t); } @@ -585,32 +657,46 @@ public CompletableFuture eventStreamOperationWithOnlyOutput( */ @Override public CompletableFuture getWithoutRequiredMembers( - GetWithoutRequiredMembersRequest getWithoutRequiredMembersRequest) { + GetWithoutRequiredMembersRequest getWithoutRequiredMembersRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(getWithoutRequiredMembersRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, getWithoutRequiredMembersRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "GetWithoutRequiredMembers"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, GetWithoutRequiredMembersResponse::builder); - + operationMetadata, GetWithoutRequiredMembersResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("GetWithoutRequiredMembers").withProtocolMetadata(protocolMetadata) - .withMarshaller(new GetWithoutRequiredMembersRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(getWithoutRequiredMembersRequest)); + .execute(new ClientExecutionParams() + .withOperationName("GetWithoutRequiredMembers").withProtocolMetadata(protocolMetadata) + .withMarshaller(new GetWithoutRequiredMembersRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(getWithoutRequiredMembersRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -645,36 +731,50 @@ public CompletableFuture getWithoutRequiredMe */ @Override public CompletableFuture operationWithChecksumRequired( - OperationWithChecksumRequiredRequest operationWithChecksumRequiredRequest) { + OperationWithChecksumRequiredRequest operationWithChecksumRequiredRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(operationWithChecksumRequiredRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - operationWithChecksumRequiredRequest.overrideConfiguration().orElse(null)); + operationWithChecksumRequiredRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "OperationWithChecksumRequired"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OperationWithChecksumRequiredResponse::builder); - + operationMetadata, OperationWithChecksumRequiredResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("OperationWithChecksumRequired") - .withProtocolMetadata(protocolMetadata) - .withMarshaller(new OperationWithChecksumRequiredRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector) - .putExecutionAttribute(SdkInternalExecutionAttribute.HTTP_CHECKSUM_REQUIRED, - HttpChecksumRequired.create()).withInput(operationWithChecksumRequiredRequest)); + .execute(new ClientExecutionParams() + .withOperationName("OperationWithChecksumRequired") + .withProtocolMetadata(protocolMetadata) + .withMarshaller(new OperationWithChecksumRequiredRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector) + .putExecutionAttribute(SdkInternalExecutionAttribute.HTTP_CHECKSUM_REQUIRED, + HttpChecksumRequired.create()).withInput(operationWithChecksumRequiredRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -708,33 +808,47 @@ public CompletableFuture operationWithChe */ @Override public CompletableFuture operationWithNoneAuthType( - OperationWithNoneAuthTypeRequest operationWithNoneAuthTypeRequest) { + OperationWithNoneAuthTypeRequest operationWithNoneAuthTypeRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(operationWithNoneAuthTypeRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, operationWithNoneAuthTypeRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "OperationWithNoneAuthType"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OperationWithNoneAuthTypeResponse::builder); - + operationMetadata, OperationWithNoneAuthTypeResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("OperationWithNoneAuthType").withProtocolMetadata(protocolMetadata) - .withMarshaller(new OperationWithNoneAuthTypeRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .putExecutionAttribute(SdkInternalExecutionAttribute.IS_NONE_AUTH_TYPE_REQUEST, false) - .withInput(operationWithNoneAuthTypeRequest)); + .execute(new ClientExecutionParams() + .withOperationName("OperationWithNoneAuthType").withProtocolMetadata(protocolMetadata) + .withMarshaller(new OperationWithNoneAuthTypeRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .putExecutionAttribute(SdkInternalExecutionAttribute.IS_NONE_AUTH_TYPE_REQUEST, false) + .withInput(operationWithNoneAuthTypeRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -769,37 +883,51 @@ public CompletableFuture operationWithNoneAut */ @Override public CompletableFuture operationWithRequestCompression( - OperationWithRequestCompressionRequest operationWithRequestCompressionRequest) { + OperationWithRequestCompressionRequest operationWithRequestCompressionRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(operationWithRequestCompressionRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - operationWithRequestCompressionRequest.overrideConfiguration().orElse(null)); + operationWithRequestCompressionRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "OperationWithRequestCompression"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OperationWithRequestCompressionResponse::builder); - + operationMetadata, OperationWithRequestCompressionResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("OperationWithRequestCompression") - .withProtocolMetadata(protocolMetadata) - .withMarshaller(new OperationWithRequestCompressionRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector) - .putExecutionAttribute(SdkInternalExecutionAttribute.REQUEST_COMPRESSION, - RequestCompression.builder().encodings("gzip").isStreaming(false).build()) - .withInput(operationWithRequestCompressionRequest)); + .execute(new ClientExecutionParams() + .withOperationName("OperationWithRequestCompression") + .withProtocolMetadata(protocolMetadata) + .withMarshaller(new OperationWithRequestCompressionRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector) + .putExecutionAttribute(SdkInternalExecutionAttribute.REQUEST_COMPRESSION, + RequestCompression.builder().encodings("gzip").isStreaming(false).build()) + .withInput(operationWithRequestCompressionRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -834,32 +962,46 @@ public CompletableFuture operationWithR */ @Override public CompletableFuture paginatedOperationWithResultKey( - PaginatedOperationWithResultKeyRequest paginatedOperationWithResultKeyRequest) { + PaginatedOperationWithResultKeyRequest paginatedOperationWithResultKeyRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(paginatedOperationWithResultKeyRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - paginatedOperationWithResultKeyRequest.overrideConfiguration().orElse(null)); + paginatedOperationWithResultKeyRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "PaginatedOperationWithResultKey"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, PaginatedOperationWithResultKeyResponse::builder); - + operationMetadata, PaginatedOperationWithResultKeyResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("PaginatedOperationWithResultKey").withProtocolMetadata(protocolMetadata) - .withMarshaller(new PaginatedOperationWithResultKeyRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(paginatedOperationWithResultKeyRequest)); + .execute(new ClientExecutionParams() + .withOperationName("PaginatedOperationWithResultKey").withProtocolMetadata(protocolMetadata) + .withMarshaller(new PaginatedOperationWithResultKeyRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(paginatedOperationWithResultKeyRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -894,32 +1036,46 @@ public CompletableFuture paginatedOpera */ @Override public CompletableFuture paginatedOperationWithoutResultKey( - PaginatedOperationWithoutResultKeyRequest paginatedOperationWithoutResultKeyRequest) { + PaginatedOperationWithoutResultKeyRequest paginatedOperationWithoutResultKeyRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(paginatedOperationWithoutResultKeyRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - paginatedOperationWithoutResultKeyRequest.overrideConfiguration().orElse(null)); + paginatedOperationWithoutResultKeyRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "PaginatedOperationWithoutResultKey"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory - .createResponseHandler(operationMetadata, PaginatedOperationWithoutResultKeyResponse::builder); - + .createResponseHandler(operationMetadata, PaginatedOperationWithoutResultKeyResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("PaginatedOperationWithoutResultKey").withProtocolMetadata(protocolMetadata) - .withMarshaller(new PaginatedOperationWithoutResultKeyRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(paginatedOperationWithoutResultKeyRequest)); + .execute(new ClientExecutionParams() + .withOperationName("PaginatedOperationWithoutResultKey").withProtocolMetadata(protocolMetadata) + .withMarshaller(new PaginatedOperationWithoutResultKeyRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(paginatedOperationWithoutResultKeyRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -958,13 +1114,13 @@ public CompletableFuture paginatedOp */ @Override public CompletableFuture streamingInputOperation( - StreamingInputOperationRequest streamingInputOperationRequest, AsyncRequestBody requestBody) { + StreamingInputOperationRequest streamingInputOperationRequest, AsyncRequestBody requestBody) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(streamingInputOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, streamingInputOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "StreamingInputOperation"); @@ -972,25 +1128,39 @@ public CompletableFuture streamingInputOperatio streamingInputOperationRequest = applySignerOverride(streamingInputOperationRequest, AsyncAws4Signer.create()); } JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, StreamingInputOperationResponse::builder); - + operationMetadata, StreamingInputOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("StreamingInputOperation") - .withProtocolMetadata(protocolMetadata) - .withMarshaller( - AsyncStreamingRequestMarshaller.builder() - .delegateMarshaller(new StreamingInputOperationRequestMarshaller(protocolFactory)) - .asyncRequestBody(requestBody).build()).withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector).withAsyncRequestBody(requestBody) - .withInput(streamingInputOperationRequest)); + .execute(new ClientExecutionParams() + .withOperationName("StreamingInputOperation") + .withProtocolMetadata(protocolMetadata) + .withMarshaller( + AsyncStreamingRequestMarshaller.builder() + .delegateMarshaller(new StreamingInputOperationRequestMarshaller(protocolFactory)) + .asyncRequestBody(requestBody).build()).withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector).withAsyncRequestBody(requestBody) + .withInput(streamingInputOperationRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -1034,51 +1204,65 @@ public CompletableFuture streamingInputOperatio */ @Override public CompletableFuture streamingInputOutputOperation( - StreamingInputOutputOperationRequest streamingInputOutputOperationRequest, AsyncRequestBody requestBody, - AsyncResponseTransformer asyncResponseTransformer) { + StreamingInputOutputOperationRequest streamingInputOutputOperationRequest, AsyncRequestBody requestBody, + AsyncResponseTransformer asyncResponseTransformer) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(streamingInputOutputOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - streamingInputOutputOperationRequest.overrideConfiguration().orElse(null)); + streamingInputOutputOperationRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "StreamingInputOutputOperation"); Pair, CompletableFuture> pair = AsyncResponseTransformerUtils - .wrapWithEndOfStreamFuture(asyncResponseTransformer); + .wrapWithEndOfStreamFuture(asyncResponseTransformer); asyncResponseTransformer = pair.left(); CompletableFuture endOfStreamFuture = pair.right(); streamingInputOutputOperationRequest = applySignerOverride(streamingInputOutputOperationRequest, - Aws4UnsignedPayloadSigner.create()); + Aws4UnsignedPayloadSigner.create()); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(true) - .isPayloadJson(false).build(); + .isPayloadJson(false).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, StreamingInputOutputOperationResponse::builder); - + operationMetadata, StreamingInputOutputOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler.execute( - new ClientExecutionParams() - .withOperationName("StreamingInputOutputOperation") - .withProtocolMetadata(protocolMetadata) - .withMarshaller( - AsyncStreamingRequestMarshaller - .builder() - .delegateMarshaller( - new StreamingInputOutputOperationRequestMarshaller(protocolFactory)) - .asyncRequestBody(requestBody).transferEncoding(true).build()) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withAsyncRequestBody(requestBody).withInput(streamingInputOutputOperationRequest), - asyncResponseTransformer); + new ClientExecutionParams() + .withOperationName("StreamingInputOutputOperation") + .withProtocolMetadata(protocolMetadata) + .withMarshaller( + AsyncStreamingRequestMarshaller + .builder() + .delegateMarshaller( + new StreamingInputOutputOperationRequestMarshaller(protocolFactory)) + .asyncRequestBody(requestBody).transferEncoding(true).build()) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withAsyncRequestBody(requestBody).withInput(streamingInputOutputOperationRequest), + asyncResponseTransformer); AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { if (e != null) { runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(e)); + () -> finalAsyncResponseTransformer.exceptionOccurred(e)); } endOfStreamFuture.whenComplete((r2, e2) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); @@ -1089,7 +1273,7 @@ public CompletableFuture streamingInputOutputOperation( } catch (Throwable t) { AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(t)); + () -> finalAsyncResponseTransformer.exceptionOccurred(t)); metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); return CompletableFutureUtils.failedFuture(t); } @@ -1122,42 +1306,56 @@ public CompletableFuture streamingInputOutputOperation( */ @Override public CompletableFuture streamingOutputOperation( - StreamingOutputOperationRequest streamingOutputOperationRequest, - AsyncResponseTransformer asyncResponseTransformer) { + StreamingOutputOperationRequest streamingOutputOperationRequest, + AsyncResponseTransformer asyncResponseTransformer) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(streamingOutputOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, streamingOutputOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "StreamingOutputOperation"); Pair, CompletableFuture> pair = AsyncResponseTransformerUtils - .wrapWithEndOfStreamFuture(asyncResponseTransformer); + .wrapWithEndOfStreamFuture(asyncResponseTransformer); asyncResponseTransformer = pair.left(); CompletableFuture endOfStreamFuture = pair.right(); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(true) - .isPayloadJson(false).build(); + .isPayloadJson(false).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, StreamingOutputOperationResponse::builder); - + operationMetadata, StreamingOutputOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler.execute( - new ClientExecutionParams() - .withOperationName("StreamingOutputOperation").withProtocolMetadata(protocolMetadata) - .withMarshaller(new StreamingOutputOperationRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(streamingOutputOperationRequest), asyncResponseTransformer); + new ClientExecutionParams() + .withOperationName("StreamingOutputOperation").withProtocolMetadata(protocolMetadata) + .withMarshaller(new StreamingOutputOperationRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(streamingOutputOperationRequest), asyncResponseTransformer); AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { if (e != null) { runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(e)); + () -> finalAsyncResponseTransformer.exceptionOccurred(e)); } endOfStreamFuture.whenComplete((r2, e2) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); @@ -1168,7 +1366,7 @@ public CompletableFuture streamingOutputOperation( } catch (Throwable t) { AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(t)); + () -> finalAsyncResponseTransformer.exceptionOccurred(t)); metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); return CompletableFutureUtils.failedFuture(t); } @@ -1185,21 +1383,12 @@ public final String serviceName() { } private > T init(T builder) { - return builder - .clientConfiguration(clientConfiguration) - .defaultServiceExceptionSupplier(JsonException::builder) - .protocol(AwsJsonProtocol.AWS_JSON) - .protocolVersion("1.1") - .registerModeledException( - ExceptionMetadata.builder().errorCode("InvalidInputException") - .exceptionBuilderSupplier(InvalidInputException::builder).httpStatusCode(400).build()) - .registerModeledException( - ExceptionMetadata.builder().errorCode("ServiceFaultException") - .exceptionBuilderSupplier(ServiceFaultException::builder).httpStatusCode(500).build()); + return builder.clientConfiguration(clientConfiguration).defaultServiceExceptionSupplier(JsonException::builder) + .protocol(AwsJsonProtocol.AWS_JSON).protocolVersion("1.1"); } private static List resolveMetricPublishers(SdkClientConfiguration clientConfiguration, - RequestOverrideConfiguration requestOverrideConfiguration) { + RequestOverrideConfiguration requestOverrideConfiguration) { List publishers = null; if (requestOverrideConfiguration != null) { publishers = requestOverrideConfiguration.metricPublishers(); @@ -1219,8 +1408,8 @@ private T applySignerOverride(T request, Signer signer) } Consumer signerOverride = b -> b.signer(signer).build(); AwsRequestOverrideConfiguration overrideConfiguration = request.overrideConfiguration() - .map(c -> c.toBuilder().applyMutation(signerOverride).build()) - .orElse((AwsRequestOverrideConfiguration.builder().applyMutation(signerOverride).build())); + .map(c -> c.toBuilder().applyMutation(signerOverride).build()) + .orElse((AwsRequestOverrideConfiguration.builder().applyMutation(signerOverride).build())); return (T) request.toBuilder().overrideConfiguration(overrideConfiguration).build(); } @@ -1266,12 +1455,7 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, } private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata) { - return protocolFactory.createErrorResponseHandler(operationMetadata); - } - - private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { + JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { return protocolFactory.createErrorResponseHandler(operationMetadata, exceptionMetadataMapper); } diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-cbor-client-class.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-cbor-client-class.java index b6b072aad637..cfa013f73a9d 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-cbor-client-class.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-cbor-client-class.java @@ -2,7 +2,9 @@ import java.util.Collections; import java.util.List; +import java.util.Optional; import java.util.function.Consumer; +import java.util.function.Function; import software.amazon.awssdk.annotations.Generated; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.awscore.client.handler.AwsSyncClientHandler; @@ -88,7 +90,7 @@ final class DefaultJsonClient implements JsonClient { private static final Logger log = Logger.loggerFor(DefaultJsonClient.class); private static final AwsProtocolMetadata protocolMetadata = AwsProtocolMetadata.builder() - .serviceProtocol(AwsServiceProtocol.CBOR).build(); + .serviceProtocol(AwsServiceProtocol.CBOR).build(); private final SyncClientHandler clientHandler; @@ -124,34 +126,48 @@ protected DefaultJsonClient(SdkClientConfiguration clientConfiguration) { */ @Override public APostOperationResponse aPostOperation(APostOperationRequest aPostOperationRequest) throws InvalidInputException, - AwsServiceException, SdkClientException, JsonException { + AwsServiceException, SdkClientException, JsonException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, - APostOperationResponse::builder); - + APostOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(aPostOperationRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, aPostOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "APostOperation"); String hostPrefix = "{StringMember}-foo."; HostnameValidator.validateHostnameCompliant(aPostOperationRequest.stringMember(), "StringMember", - "aPostOperationRequest"); + "aPostOperationRequest"); String resolvedHostExpression = String.format("%s-foo.", aPostOperationRequest.stringMember()); return clientHandler.execute(new ClientExecutionParams() - .withOperationName("APostOperation").withProtocolMetadata(protocolMetadata) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .hostPrefixExpression(resolvedHostExpression).withRequestConfiguration(clientConfiguration) - .withInput(aPostOperationRequest).withMetricCollector(apiCallMetricCollector) - .withMarshaller(new APostOperationRequestMarshaller(protocolFactory))); + .withOperationName("APostOperation").withProtocolMetadata(protocolMetadata) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .hostPrefixExpression(resolvedHostExpression).withRequestConfiguration(clientConfiguration) + .withInput(aPostOperationRequest).withMetricCollector(apiCallMetricCollector) + .withMarshaller(new APostOperationRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } @@ -180,33 +196,47 @@ public APostOperationResponse aPostOperation(APostOperationRequest aPostOperatio */ @Override public APostOperationWithOutputResponse aPostOperationWithOutput( - APostOperationWithOutputRequest aPostOperationWithOutputRequest) throws InvalidInputException, ServiceFaultException, - AwsServiceException, SdkClientException, JsonException { + APostOperationWithOutputRequest aPostOperationWithOutputRequest) throws InvalidInputException, ServiceFaultException, + AwsServiceException, SdkClientException, JsonException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, APostOperationWithOutputResponse::builder); - + operationMetadata, APostOperationWithOutputResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(aPostOperationWithOutputRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, aPostOperationWithOutputRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "APostOperationWithOutput"); return clientHandler - .execute(new ClientExecutionParams() - .withOperationName("APostOperationWithOutput").withProtocolMetadata(protocolMetadata) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withInput(aPostOperationWithOutputRequest) - .withMetricCollector(apiCallMetricCollector) - .withMarshaller(new APostOperationWithOutputRequestMarshaller(protocolFactory))); + .execute(new ClientExecutionParams() + .withOperationName("APostOperationWithOutput").withProtocolMetadata(protocolMetadata) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withInput(aPostOperationWithOutputRequest) + .withMetricCollector(apiCallMetricCollector) + .withMarshaller(new APostOperationWithOutputRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } @@ -234,33 +264,47 @@ public APostOperationWithOutputResponse aPostOperationWithOutput( */ @Override public GetWithoutRequiredMembersResponse getWithoutRequiredMembers( - GetWithoutRequiredMembersRequest getWithoutRequiredMembersRequest) throws InvalidInputException, AwsServiceException, - SdkClientException, JsonException { + GetWithoutRequiredMembersRequest getWithoutRequiredMembersRequest) throws InvalidInputException, AwsServiceException, + SdkClientException, JsonException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, GetWithoutRequiredMembersResponse::builder); - + operationMetadata, GetWithoutRequiredMembersResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(getWithoutRequiredMembersRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, getWithoutRequiredMembersRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "GetWithoutRequiredMembers"); return clientHandler - .execute(new ClientExecutionParams() - .withOperationName("GetWithoutRequiredMembers").withProtocolMetadata(protocolMetadata) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withInput(getWithoutRequiredMembersRequest) - .withMetricCollector(apiCallMetricCollector) - .withMarshaller(new GetWithoutRequiredMembersRequestMarshaller(protocolFactory))); + .execute(new ClientExecutionParams() + .withOperationName("GetWithoutRequiredMembers").withProtocolMetadata(protocolMetadata) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withInput(getWithoutRequiredMembersRequest) + .withMetricCollector(apiCallMetricCollector) + .withMarshaller(new GetWithoutRequiredMembersRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } @@ -284,38 +328,52 @@ public GetWithoutRequiredMembersResponse getWithoutRequiredMembers( */ @Override public OperationWithChecksumRequiredResponse operationWithChecksumRequired( - OperationWithChecksumRequiredRequest operationWithChecksumRequiredRequest) throws AwsServiceException, - SdkClientException, JsonException { + OperationWithChecksumRequiredRequest operationWithChecksumRequiredRequest) throws AwsServiceException, + SdkClientException, JsonException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OperationWithChecksumRequiredResponse::builder); - + operationMetadata, OperationWithChecksumRequiredResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(operationWithChecksumRequiredRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - operationWithChecksumRequiredRequest.overrideConfiguration().orElse(null)); + operationWithChecksumRequiredRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "OperationWithChecksumRequired"); return clientHandler - .execute(new ClientExecutionParams() - .withOperationName("OperationWithChecksumRequired") - .withProtocolMetadata(protocolMetadata) - .withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration) - .withInput(operationWithChecksumRequiredRequest) - .withMetricCollector(apiCallMetricCollector) - .putExecutionAttribute(SdkInternalExecutionAttribute.HTTP_CHECKSUM_REQUIRED, - HttpChecksumRequired.create()) - .withMarshaller(new OperationWithChecksumRequiredRequestMarshaller(protocolFactory))); + .execute(new ClientExecutionParams() + .withOperationName("OperationWithChecksumRequired") + .withProtocolMetadata(protocolMetadata) + .withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration) + .withInput(operationWithChecksumRequiredRequest) + .withMetricCollector(apiCallMetricCollector) + .putExecutionAttribute(SdkInternalExecutionAttribute.HTTP_CHECKSUM_REQUIRED, + HttpChecksumRequired.create()) + .withMarshaller(new OperationWithChecksumRequiredRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } @@ -339,33 +397,47 @@ public OperationWithChecksumRequiredResponse operationWithChecksumRequired( */ @Override public OperationWithNoneAuthTypeResponse operationWithNoneAuthType( - OperationWithNoneAuthTypeRequest operationWithNoneAuthTypeRequest) throws AwsServiceException, SdkClientException, - JsonException { + OperationWithNoneAuthTypeRequest operationWithNoneAuthTypeRequest) throws AwsServiceException, SdkClientException, + JsonException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OperationWithNoneAuthTypeResponse::builder); - + operationMetadata, OperationWithNoneAuthTypeResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(operationWithNoneAuthTypeRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, operationWithNoneAuthTypeRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "OperationWithNoneAuthType"); return clientHandler - .execute(new ClientExecutionParams() - .withOperationName("OperationWithNoneAuthType").withProtocolMetadata(protocolMetadata) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withInput(operationWithNoneAuthTypeRequest) - .withMetricCollector(apiCallMetricCollector) - .withMarshaller(new OperationWithNoneAuthTypeRequestMarshaller(protocolFactory))); + .execute(new ClientExecutionParams() + .withOperationName("OperationWithNoneAuthType").withProtocolMetadata(protocolMetadata) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withInput(operationWithNoneAuthTypeRequest) + .withMetricCollector(apiCallMetricCollector) + .withMarshaller(new OperationWithNoneAuthTypeRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } @@ -389,38 +461,52 @@ public OperationWithNoneAuthTypeResponse operationWithNoneAuthType( */ @Override public OperationWithRequestCompressionResponse operationWithRequestCompression( - OperationWithRequestCompressionRequest operationWithRequestCompressionRequest) throws AwsServiceException, - SdkClientException, JsonException { + OperationWithRequestCompressionRequest operationWithRequestCompressionRequest) throws AwsServiceException, + SdkClientException, JsonException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OperationWithRequestCompressionResponse::builder); - + operationMetadata, OperationWithRequestCompressionResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(operationWithRequestCompressionRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - operationWithRequestCompressionRequest.overrideConfiguration().orElse(null)); + operationWithRequestCompressionRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "OperationWithRequestCompression"); return clientHandler - .execute(new ClientExecutionParams() - .withOperationName("OperationWithRequestCompression") - .withProtocolMetadata(protocolMetadata) - .withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration) - .withInput(operationWithRequestCompressionRequest) - .withMetricCollector(apiCallMetricCollector) - .putExecutionAttribute(SdkInternalExecutionAttribute.REQUEST_COMPRESSION, - RequestCompression.builder().encodings("gzip").isStreaming(false).build()) - .withMarshaller(new OperationWithRequestCompressionRequestMarshaller(protocolFactory))); + .execute(new ClientExecutionParams() + .withOperationName("OperationWithRequestCompression") + .withProtocolMetadata(protocolMetadata) + .withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration) + .withInput(operationWithRequestCompressionRequest) + .withMetricCollector(apiCallMetricCollector) + .putExecutionAttribute(SdkInternalExecutionAttribute.REQUEST_COMPRESSION, + RequestCompression.builder().encodings("gzip").isStreaming(false).build()) + .withMarshaller(new OperationWithRequestCompressionRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } @@ -444,33 +530,47 @@ public OperationWithRequestCompressionResponse operationWithRequestCompression( */ @Override public PaginatedOperationWithResultKeyResponse paginatedOperationWithResultKey( - PaginatedOperationWithResultKeyRequest paginatedOperationWithResultKeyRequest) throws AwsServiceException, - SdkClientException, JsonException { + PaginatedOperationWithResultKeyRequest paginatedOperationWithResultKeyRequest) throws AwsServiceException, + SdkClientException, JsonException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, PaginatedOperationWithResultKeyResponse::builder); - + operationMetadata, PaginatedOperationWithResultKeyResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(paginatedOperationWithResultKeyRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - paginatedOperationWithResultKeyRequest.overrideConfiguration().orElse(null)); + paginatedOperationWithResultKeyRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "PaginatedOperationWithResultKey"); return clientHandler - .execute(new ClientExecutionParams() - .withOperationName("PaginatedOperationWithResultKey").withProtocolMetadata(protocolMetadata) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withInput(paginatedOperationWithResultKeyRequest) - .withMetricCollector(apiCallMetricCollector) - .withMarshaller(new PaginatedOperationWithResultKeyRequestMarshaller(protocolFactory))); + .execute(new ClientExecutionParams() + .withOperationName("PaginatedOperationWithResultKey").withProtocolMetadata(protocolMetadata) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withInput(paginatedOperationWithResultKeyRequest) + .withMetricCollector(apiCallMetricCollector) + .withMarshaller(new PaginatedOperationWithResultKeyRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } @@ -494,33 +594,47 @@ public PaginatedOperationWithResultKeyResponse paginatedOperationWithResultKey( */ @Override public PaginatedOperationWithoutResultKeyResponse paginatedOperationWithoutResultKey( - PaginatedOperationWithoutResultKeyRequest paginatedOperationWithoutResultKeyRequest) throws AwsServiceException, - SdkClientException, JsonException { + PaginatedOperationWithoutResultKeyRequest paginatedOperationWithoutResultKeyRequest) throws AwsServiceException, + SdkClientException, JsonException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, PaginatedOperationWithoutResultKeyResponse::builder); - + operationMetadata, PaginatedOperationWithoutResultKeyResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(paginatedOperationWithoutResultKeyRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - paginatedOperationWithoutResultKeyRequest.overrideConfiguration().orElse(null)); + paginatedOperationWithoutResultKeyRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "PaginatedOperationWithoutResultKey"); return clientHandler - .execute(new ClientExecutionParams() - .withOperationName("PaginatedOperationWithoutResultKey").withProtocolMetadata(protocolMetadata) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withInput(paginatedOperationWithoutResultKeyRequest) - .withMetricCollector(apiCallMetricCollector) - .withMarshaller(new PaginatedOperationWithoutResultKeyRequestMarshaller(protocolFactory))); + .execute(new ClientExecutionParams() + .withOperationName("PaginatedOperationWithoutResultKey").withProtocolMetadata(protocolMetadata) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withInput(paginatedOperationWithoutResultKeyRequest) + .withMetricCollector(apiCallMetricCollector) + .withMarshaller(new PaginatedOperationWithoutResultKeyRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } @@ -555,39 +669,53 @@ public PaginatedOperationWithoutResultKeyResponse paginatedOperationWithoutResul */ @Override public StreamingInputOperationResponse streamingInputOperation(StreamingInputOperationRequest streamingInputOperationRequest, - RequestBody requestBody) throws AwsServiceException, SdkClientException, JsonException { + RequestBody requestBody) throws AwsServiceException, SdkClientException, JsonException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, StreamingInputOperationResponse::builder); - + operationMetadata, StreamingInputOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(streamingInputOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, streamingInputOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "StreamingInputOperation"); return clientHandler - .execute(new ClientExecutionParams() - .withOperationName("StreamingInputOperation") - .withProtocolMetadata(protocolMetadata) - .withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration) - .withInput(streamingInputOperationRequest) - .withMetricCollector(apiCallMetricCollector) - .withRequestBody(requestBody) - .withMarshaller( - StreamingRequestMarshaller.builder() - .delegateMarshaller(new StreamingInputOperationRequestMarshaller(protocolFactory)) - .requestBody(requestBody).build())); + .execute(new ClientExecutionParams() + .withOperationName("StreamingInputOperation") + .withProtocolMetadata(protocolMetadata) + .withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration) + .withInput(streamingInputOperationRequest) + .withMetricCollector(apiCallMetricCollector) + .withRequestBody(requestBody) + .withMarshaller( + StreamingRequestMarshaller.builder() + .delegateMarshaller(new StreamingInputOperationRequestMarshaller(protocolFactory)) + .requestBody(requestBody).build())); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } @@ -629,43 +757,57 @@ public StreamingInputOperationResponse streamingInputOperation(StreamingInputOpe */ @Override public ReturnT streamingInputOutputOperation( - StreamingInputOutputOperationRequest streamingInputOutputOperationRequest, RequestBody requestBody, - ResponseTransformer responseTransformer) throws AwsServiceException, - SdkClientException, JsonException { + StreamingInputOutputOperationRequest streamingInputOutputOperationRequest, RequestBody requestBody, + ResponseTransformer responseTransformer) throws AwsServiceException, + SdkClientException, JsonException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(true) - .isPayloadJson(false).build(); + .isPayloadJson(false).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, StreamingInputOutputOperationResponse::builder); - + operationMetadata, StreamingInputOutputOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(streamingInputOutputOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - streamingInputOutputOperationRequest.overrideConfiguration().orElse(null)); + streamingInputOutputOperationRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "StreamingInputOutputOperation"); return clientHandler.execute( - new ClientExecutionParams() - .withOperationName("StreamingInputOutputOperation") - .withProtocolMetadata(protocolMetadata) - .withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration) - .withInput(streamingInputOutputOperationRequest) - .withMetricCollector(apiCallMetricCollector) - .withRequestBody(requestBody) - .withMarshaller( - StreamingRequestMarshaller - .builder() - .delegateMarshaller( - new StreamingInputOutputOperationRequestMarshaller(protocolFactory)) - .requestBody(requestBody).transferEncoding(true).build()), responseTransformer); + new ClientExecutionParams() + .withOperationName("StreamingInputOutputOperation") + .withProtocolMetadata(protocolMetadata) + .withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration) + .withInput(streamingInputOutputOperationRequest) + .withMetricCollector(apiCallMetricCollector) + .withRequestBody(requestBody) + .withMarshaller( + StreamingRequestMarshaller + .builder() + .delegateMarshaller( + new StreamingInputOutputOperationRequestMarshaller(protocolFactory)) + .requestBody(requestBody).transferEncoding(true).build()), responseTransformer); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } @@ -696,33 +838,47 @@ public ReturnT streamingInputOutputOperation( */ @Override public ReturnT streamingOutputOperation(StreamingOutputOperationRequest streamingOutputOperationRequest, - ResponseTransformer responseTransformer) throws AwsServiceException, - SdkClientException, JsonException { + ResponseTransformer responseTransformer) throws AwsServiceException, + SdkClientException, JsonException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(true) - .isPayloadJson(false).build(); + .isPayloadJson(false).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, StreamingOutputOperationResponse::builder); - + operationMetadata, StreamingOutputOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInputException": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInputException").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "ServiceFaultException": + return Optional.of(ExceptionMetadata.builder().errorCode("ServiceFaultException").httpStatusCode(500) + .exceptionBuilderSupplier(ServiceFaultException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(streamingOutputOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, streamingOutputOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "StreamingOutputOperation"); return clientHandler.execute( - new ClientExecutionParams() - .withOperationName("StreamingOutputOperation").withProtocolMetadata(protocolMetadata) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withInput(streamingOutputOperationRequest) - .withMetricCollector(apiCallMetricCollector) - .withMarshaller(new StreamingOutputOperationRequestMarshaller(protocolFactory)), responseTransformer); + new ClientExecutionParams() + .withOperationName("StreamingOutputOperation").withProtocolMetadata(protocolMetadata) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withInput(streamingOutputOperationRequest) + .withMetricCollector(apiCallMetricCollector) + .withMarshaller(new StreamingOutputOperationRequestMarshaller(protocolFactory)), responseTransformer); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } @@ -742,7 +898,7 @@ public final String serviceName() { } private static List resolveMetricPublishers(SdkClientConfiguration clientConfiguration, - RequestOverrideConfiguration requestOverrideConfiguration) { + RequestOverrideConfiguration requestOverrideConfiguration) { List publishers = null; if (requestOverrideConfiguration != null) { publishers = requestOverrideConfiguration.metricPublishers(); @@ -757,8 +913,8 @@ private static List resolveMetricPublishers(SdkClientConfigurat } private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata) { - return protocolFactory.createErrorResponseHandler(operationMetadata); + JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { + return protocolFactory.createErrorResponseHandler(operationMetadata, exceptionMetadataMapper); } private void updateRetryStrategyClientConfiguration(SdkClientConfiguration.Builder configuration) { @@ -799,17 +955,8 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, } private > T init(T builder) { - return builder - .clientConfiguration(clientConfiguration) - .defaultServiceExceptionSupplier(JsonException::builder) - .protocol(AwsJsonProtocol.AWS_JSON) - .protocolVersion("1.1") - .registerModeledException( - ExceptionMetadata.builder().errorCode("InvalidInputException") - .exceptionBuilderSupplier(InvalidInputException::builder).httpStatusCode(400).build()) - .registerModeledException( - ExceptionMetadata.builder().errorCode("ServiceFaultException") - .exceptionBuilderSupplier(ServiceFaultException::builder).httpStatusCode(500).build()); + return builder.clientConfiguration(clientConfiguration).defaultServiceExceptionSupplier(JsonException::builder) + .protocol(AwsJsonProtocol.AWS_JSON).protocolVersion("1.1"); } @Override diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-custompackage-async.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-custompackage-async.java index d3fa60fddd28..d1740b039d14 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-custompackage-async.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-custompackage-async.java @@ -55,7 +55,7 @@ final class DefaultProtocolRestJsonWithCustomPackageAsyncClient implements Proto private static final Logger log = LoggerFactory.getLogger(DefaultProtocolRestJsonWithCustomPackageAsyncClient.class); private static final AwsProtocolMetadata protocolMetadata = AwsProtocolMetadata.builder() - .serviceProtocol(AwsServiceProtocol.REST_JSON).build(); + .serviceProtocol(AwsServiceProtocol.REST_JSON).build(); private final AsyncClientHandler clientHandler; @@ -93,28 +93,36 @@ protected DefaultProtocolRestJsonWithCustomPackageAsyncClient(SdkClientConfigura public CompletableFuture oneOperation(OneOperationRequest oneOperationRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(oneOperationRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, oneOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "AmazonProtocolRestJsonWithCustomPackage"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "OneOperation"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, - OneOperationResponse::builder); - + OneOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("OneOperation").withProtocolMetadata(protocolMetadata) - .withMarshaller(new OneOperationRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(oneOperationRequest)); + .execute(new ClientExecutionParams() + .withOperationName("OneOperation").withProtocolMetadata(protocolMetadata) + .withMarshaller(new OneOperationRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(oneOperationRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -129,7 +137,7 @@ public CompletableFuture oneOperation(OneOperationRequest @Override public final ProtocolRestJsonWithCustomPackageServiceClientConfiguration serviceClientConfiguration() { return new ProtocolRestJsonWithCustomPackageServiceClientConfigurationBuilder(this.clientConfiguration.toBuilder()) - .build(); + .build(); } @Override @@ -139,12 +147,12 @@ public final String serviceName() { private > T init(T builder) { return builder.clientConfiguration(clientConfiguration) - .defaultServiceExceptionSupplier(ProtocolRestJsonWithCustomPackageException::builder) - .protocol(AwsJsonProtocol.REST_JSON).protocolVersion("1.1"); + .defaultServiceExceptionSupplier(ProtocolRestJsonWithCustomPackageException::builder) + .protocol(AwsJsonProtocol.REST_JSON).protocolVersion("1.1"); } private static List resolveMetricPublishers(SdkClientConfiguration clientConfiguration, - RequestOverrideConfiguration requestOverrideConfiguration) { + RequestOverrideConfiguration requestOverrideConfiguration) { List publishers = null; if (requestOverrideConfiguration != null) { publishers = requestOverrideConfiguration.metricPublishers(); @@ -188,7 +196,7 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, return configuration.build(); } ProtocolRestJsonWithCustomPackageServiceClientConfigurationBuilder serviceConfigBuilder = new ProtocolRestJsonWithCustomPackageServiceClientConfigurationBuilder( - configuration); + configuration); for (SdkPlugin plugin : plugins) { plugin.configureClient(serviceConfigBuilder); } @@ -197,12 +205,7 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, } private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata) { - return protocolFactory.createErrorResponseHandler(operationMetadata); - } - - private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { + JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { return protocolFactory.createErrorResponseHandler(operationMetadata, exceptionMetadataMapper); } diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-custompackage-sync.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-custompackage-sync.java index de640533ca5a..e6bad19f4837 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-custompackage-sync.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-custompackage-sync.java @@ -7,7 +7,9 @@ import foo.bar.helloworld.transform.OneOperationRequestMarshaller; import java.util.Collections; import java.util.List; +import java.util.Optional; import java.util.function.Consumer; +import java.util.function.Function; import software.amazon.awssdk.annotations.Generated; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.awscore.client.handler.AwsSyncClientHandler; @@ -30,6 +32,7 @@ import software.amazon.awssdk.metrics.MetricCollector; import software.amazon.awssdk.metrics.MetricPublisher; import software.amazon.awssdk.metrics.NoOpMetricCollector; +import software.amazon.awssdk.protocols.core.ExceptionMetadata; import software.amazon.awssdk.protocols.json.AwsJsonProtocol; import software.amazon.awssdk.protocols.json.AwsJsonProtocolFactory; import software.amazon.awssdk.protocols.json.BaseAwsJsonProtocolFactory; @@ -86,9 +89,17 @@ public OneOperationResponse oneOperation(OneOperationRequest oneOperationRequest HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, OneOperationResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(oneOperationRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, oneOperationRequest .overrideConfiguration().orElse(null)); @@ -130,8 +141,8 @@ private static List resolveMetricPublishers(SdkClientConfigurat } private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata) { - return protocolFactory.createErrorResponseHandler(operationMetadata); + JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { + return protocolFactory.createErrorResponseHandler(operationMetadata, exceptionMetadataMapper); } private void updateRetryStrategyClientConfiguration(SdkClientConfiguration.Builder configuration) { diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-customservicemetadata-async.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-customservicemetadata-async.java index 7804faabf8e8..1ecac26efb45 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-customservicemetadata-async.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-customservicemetadata-async.java @@ -55,7 +55,7 @@ final class DefaultProtocolRestJsonWithCustomContentTypeAsyncClient implements P private static final Logger log = LoggerFactory.getLogger(DefaultProtocolRestJsonWithCustomContentTypeAsyncClient.class); private static final AwsProtocolMetadata protocolMetadata = AwsProtocolMetadata.builder() - .serviceProtocol(AwsServiceProtocol.REST_JSON).build(); + .serviceProtocol(AwsServiceProtocol.REST_JSON).build(); private final AsyncClientHandler clientHandler; @@ -93,28 +93,36 @@ protected DefaultProtocolRestJsonWithCustomContentTypeAsyncClient(SdkClientConfi public CompletableFuture oneOperation(OneOperationRequest oneOperationRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(oneOperationRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, oneOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "AmazonProtocolRestJsonWithCustomContentType"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "OneOperation"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, - OneOperationResponse::builder); - + OneOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("OneOperation").withProtocolMetadata(protocolMetadata) - .withMarshaller(new OneOperationRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(oneOperationRequest)); + .execute(new ClientExecutionParams() + .withOperationName("OneOperation").withProtocolMetadata(protocolMetadata) + .withMarshaller(new OneOperationRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(oneOperationRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -129,7 +137,7 @@ public CompletableFuture oneOperation(OneOperationRequest @Override public final ProtocolRestJsonWithCustomContentTypeServiceClientConfiguration serviceClientConfiguration() { return new ProtocolRestJsonWithCustomContentTypeServiceClientConfigurationBuilder(this.clientConfiguration.toBuilder()) - .build(); + .build(); } @Override @@ -139,12 +147,12 @@ public final String serviceName() { private > T init(T builder) { return builder.clientConfiguration(clientConfiguration) - .defaultServiceExceptionSupplier(ProtocolRestJsonWithCustomContentTypeException::builder) - .protocol(AwsJsonProtocol.REST_JSON).protocolVersion("1.1").contentType("application/json"); + .defaultServiceExceptionSupplier(ProtocolRestJsonWithCustomContentTypeException::builder) + .protocol(AwsJsonProtocol.REST_JSON).protocolVersion("1.1").contentType("application/json"); } private static List resolveMetricPublishers(SdkClientConfiguration clientConfiguration, - RequestOverrideConfiguration requestOverrideConfiguration) { + RequestOverrideConfiguration requestOverrideConfiguration) { List publishers = null; if (requestOverrideConfiguration != null) { publishers = requestOverrideConfiguration.metricPublishers(); @@ -188,7 +196,7 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, return configuration.build(); } ProtocolRestJsonWithCustomContentTypeServiceClientConfigurationBuilder serviceConfigBuilder = new ProtocolRestJsonWithCustomContentTypeServiceClientConfigurationBuilder( - configuration); + configuration); for (SdkPlugin plugin : plugins) { plugin.configureClient(serviceConfigBuilder); } @@ -197,12 +205,7 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, } private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata) { - return protocolFactory.createErrorResponseHandler(operationMetadata); - } - - private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { + JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { return protocolFactory.createErrorResponseHandler(operationMetadata, exceptionMetadataMapper); } diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-customservicemetadata-sync.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-customservicemetadata-sync.java index 9f134914f49f..b97e2307b4d0 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-customservicemetadata-sync.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-customservicemetadata-sync.java @@ -2,7 +2,9 @@ import java.util.Collections; import java.util.List; +import java.util.Optional; import java.util.function.Consumer; +import java.util.function.Function; import software.amazon.awssdk.annotations.Generated; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.awscore.client.handler.AwsSyncClientHandler; @@ -25,6 +27,7 @@ import software.amazon.awssdk.metrics.MetricCollector; import software.amazon.awssdk.metrics.MetricPublisher; import software.amazon.awssdk.metrics.NoOpMetricCollector; +import software.amazon.awssdk.protocols.core.ExceptionMetadata; import software.amazon.awssdk.protocols.json.AwsJsonProtocol; import software.amazon.awssdk.protocols.json.AwsJsonProtocolFactory; import software.amazon.awssdk.protocols.json.BaseAwsJsonProtocolFactory; @@ -86,9 +89,17 @@ public OneOperationResponse oneOperation(OneOperationRequest oneOperationRequest HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, OneOperationResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(oneOperationRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, oneOperationRequest .overrideConfiguration().orElse(null)); @@ -130,8 +141,8 @@ private static List resolveMetricPublishers(SdkClientConfigurat } private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata) { - return protocolFactory.createErrorResponseHandler(operationMetadata); + JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { + return protocolFactory.createErrorResponseHandler(operationMetadata, exceptionMetadataMapper); } private void updateRetryStrategyClientConfiguration(SdkClientConfiguration.Builder configuration) { diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-endpoint-discovery-async.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-endpoint-discovery-async.java index 2857ce0fa9d1..7324f449c25e 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-endpoint-discovery-async.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-endpoint-discovery-async.java @@ -70,7 +70,7 @@ final class DefaultEndpointDiscoveryTestAsyncClient implements EndpointDiscovery private static final Logger log = LoggerFactory.getLogger(DefaultEndpointDiscoveryTestAsyncClient.class); private static final AwsProtocolMetadata protocolMetadata = AwsProtocolMetadata.builder() - .serviceProtocol(AwsServiceProtocol.AWS_JSON).build(); + .serviceProtocol(AwsServiceProtocol.AWS_JSON).build(); private final AsyncClientHandler clientHandler; @@ -86,7 +86,7 @@ protected DefaultEndpointDiscoveryTestAsyncClient(SdkClientConfiguration clientC this.protocolFactory = init(AwsJsonProtocolFactory.builder()).build(); if (clientConfiguration.option(SdkClientOption.ENDPOINT_DISCOVERY_ENABLED)) { this.endpointDiscoveryCache = EndpointDiscoveryRefreshCache - .create(EndpointDiscoveryTestAsyncEndpointDiscoveryCacheLoader.create(this)); + .create(EndpointDiscoveryTestAsyncEndpointDiscoveryCacheLoader.create(this)); } } @@ -111,30 +111,38 @@ protected DefaultEndpointDiscoveryTestAsyncClient(SdkClientConfiguration clientC @Override public CompletableFuture describeEndpoints(DescribeEndpointsRequest describeEndpointsRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(describeEndpointsRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, describeEndpointsRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "AwsEndpointDiscoveryTest"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "DescribeEndpoints"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, DescribeEndpointsResponse::builder); - + operationMetadata, DescribeEndpointsResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("DescribeEndpoints").withProtocolMetadata(protocolMetadata) - .withMarshaller(new DescribeEndpointsRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(describeEndpointsRequest)); + .execute(new ClientExecutionParams() + .withOperationName("DescribeEndpoints").withProtocolMetadata(protocolMetadata) + .withMarshaller(new DescribeEndpointsRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(describeEndpointsRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -167,61 +175,69 @@ public CompletableFuture describeEndpoints(DescribeEn */ @Override public CompletableFuture testDiscoveryIdentifiersRequired( - TestDiscoveryIdentifiersRequiredRequest testDiscoveryIdentifiersRequiredRequest) { + TestDiscoveryIdentifiersRequiredRequest testDiscoveryIdentifiersRequiredRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(testDiscoveryIdentifiersRequiredRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - testDiscoveryIdentifiersRequiredRequest.overrideConfiguration().orElse(null)); + testDiscoveryIdentifiersRequiredRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "AwsEndpointDiscoveryTest"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "TestDiscoveryIdentifiersRequired"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory - .createResponseHandler(operationMetadata, TestDiscoveryIdentifiersRequiredResponse::builder); - + .createResponseHandler(operationMetadata, TestDiscoveryIdentifiersRequiredResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); boolean endpointDiscoveryEnabled = clientConfiguration.option(SdkClientOption.ENDPOINT_DISCOVERY_ENABLED); boolean endpointOverridden = clientConfiguration.option(SdkClientOption.CLIENT_ENDPOINT_PROVIDER) - .isEndpointOverridden(); + .isEndpointOverridden(); if (endpointOverridden) { throw new IllegalStateException( - "This operation requires endpoint discovery, but an endpoint override was specified when the client was created. This is not supported."); + "This operation requires endpoint discovery, but an endpoint override was specified when the client was created. This is not supported."); } if (!endpointDiscoveryEnabled) { throw new IllegalStateException( - "This operation requires endpoint discovery, but endpoint discovery was disabled on the client."); + "This operation requires endpoint discovery, but endpoint discovery was disabled on the client."); } CompletableFuture endpointFuture = CompletableFuture.completedFuture(null); if (endpointDiscoveryEnabled) { CompletableFuture identityFuture = testDiscoveryIdentifiersRequiredRequest - .overrideConfiguration().flatMap(AwsRequestOverrideConfiguration::credentialsIdentityProvider) - .orElseGet(() -> clientConfiguration.option(AwsClientOption.CREDENTIALS_IDENTITY_PROVIDER)) - .resolveIdentity(); + .overrideConfiguration().flatMap(AwsRequestOverrideConfiguration::credentialsIdentityProvider) + .orElseGet(() -> clientConfiguration.option(AwsClientOption.CREDENTIALS_IDENTITY_PROVIDER)) + .resolveIdentity(); endpointFuture = identityFuture.thenCompose(credentials -> { EndpointDiscoveryRequest endpointDiscoveryRequest = EndpointDiscoveryRequest - .builder() - .required(true) - .defaultEndpoint( - clientConfiguration.option(SdkClientOption.CLIENT_ENDPOINT_PROVIDER).clientEndpoint()) - .overrideConfiguration(testDiscoveryIdentifiersRequiredRequest.overrideConfiguration().orElse(null)) - .build(); + .builder() + .required(true) + .defaultEndpoint( + clientConfiguration.option(SdkClientOption.CLIENT_ENDPOINT_PROVIDER).clientEndpoint()) + .overrideConfiguration(testDiscoveryIdentifiersRequiredRequest.overrideConfiguration().orElse(null)) + .build(); return endpointDiscoveryCache.getAsync(credentials.accessKeyId(), endpointDiscoveryRequest); }); } CompletableFuture executeFuture = endpointFuture - .thenCompose(cachedEndpoint -> clientHandler - .execute(new ClientExecutionParams() - .withOperationName("TestDiscoveryIdentifiersRequired").withProtocolMetadata(protocolMetadata) - .withMarshaller(new TestDiscoveryIdentifiersRequiredRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .discoveredEndpoint(cachedEndpoint).withInput(testDiscoveryIdentifiersRequiredRequest))); + .thenCompose(cachedEndpoint -> clientHandler + .execute(new ClientExecutionParams() + .withOperationName("TestDiscoveryIdentifiersRequired").withProtocolMetadata(protocolMetadata) + .withMarshaller(new TestDiscoveryIdentifiersRequiredRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .discoveredEndpoint(cachedEndpoint).withInput(testDiscoveryIdentifiersRequiredRequest))); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -253,52 +269,60 @@ public CompletableFuture testDiscovery */ @Override public CompletableFuture testDiscoveryOptional( - TestDiscoveryOptionalRequest testDiscoveryOptionalRequest) { + TestDiscoveryOptionalRequest testDiscoveryOptionalRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(testDiscoveryOptionalRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, testDiscoveryOptionalRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "AwsEndpointDiscoveryTest"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "TestDiscoveryOptional"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, TestDiscoveryOptionalResponse::builder); - + operationMetadata, TestDiscoveryOptionalResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); boolean endpointDiscoveryEnabled = clientConfiguration.option(SdkClientOption.ENDPOINT_DISCOVERY_ENABLED); boolean endpointOverridden = clientConfiguration.option(SdkClientOption.CLIENT_ENDPOINT_PROVIDER) - .isEndpointOverridden(); + .isEndpointOverridden(); CompletableFuture endpointFuture = CompletableFuture.completedFuture(null); if (endpointDiscoveryEnabled) { CompletableFuture identityFuture = testDiscoveryOptionalRequest - .overrideConfiguration().flatMap(AwsRequestOverrideConfiguration::credentialsIdentityProvider) - .orElseGet(() -> clientConfiguration.option(AwsClientOption.CREDENTIALS_IDENTITY_PROVIDER)) - .resolveIdentity(); + .overrideConfiguration().flatMap(AwsRequestOverrideConfiguration::credentialsIdentityProvider) + .orElseGet(() -> clientConfiguration.option(AwsClientOption.CREDENTIALS_IDENTITY_PROVIDER)) + .resolveIdentity(); endpointFuture = identityFuture.thenCompose(credentials -> { EndpointDiscoveryRequest endpointDiscoveryRequest = EndpointDiscoveryRequest - .builder() - .required(false) - .defaultEndpoint( - clientConfiguration.option(SdkClientOption.CLIENT_ENDPOINT_PROVIDER).clientEndpoint()) - .overrideConfiguration(testDiscoveryOptionalRequest.overrideConfiguration().orElse(null)).build(); + .builder() + .required(false) + .defaultEndpoint( + clientConfiguration.option(SdkClientOption.CLIENT_ENDPOINT_PROVIDER).clientEndpoint()) + .overrideConfiguration(testDiscoveryOptionalRequest.overrideConfiguration().orElse(null)).build(); return endpointDiscoveryCache.getAsync(credentials.accessKeyId(), endpointDiscoveryRequest); }); } CompletableFuture executeFuture = endpointFuture - .thenCompose(cachedEndpoint -> clientHandler - .execute(new ClientExecutionParams() - .withOperationName("TestDiscoveryOptional").withProtocolMetadata(protocolMetadata) - .withMarshaller(new TestDiscoveryOptionalRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .discoveredEndpoint(cachedEndpoint).withInput(testDiscoveryOptionalRequest))); + .thenCompose(cachedEndpoint -> clientHandler + .execute(new ClientExecutionParams() + .withOperationName("TestDiscoveryOptional").withProtocolMetadata(protocolMetadata) + .withMarshaller(new TestDiscoveryOptionalRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .discoveredEndpoint(cachedEndpoint).withInput(testDiscoveryOptionalRequest))); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -330,60 +354,68 @@ public CompletableFuture testDiscoveryOptional( */ @Override public CompletableFuture testDiscoveryRequired( - TestDiscoveryRequiredRequest testDiscoveryRequiredRequest) { + TestDiscoveryRequiredRequest testDiscoveryRequiredRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(testDiscoveryRequiredRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, testDiscoveryRequiredRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "AwsEndpointDiscoveryTest"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "TestDiscoveryRequired"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, TestDiscoveryRequiredResponse::builder); - + operationMetadata, TestDiscoveryRequiredResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); boolean endpointDiscoveryEnabled = clientConfiguration.option(SdkClientOption.ENDPOINT_DISCOVERY_ENABLED); boolean endpointOverridden = clientConfiguration.option(SdkClientOption.CLIENT_ENDPOINT_PROVIDER) - .isEndpointOverridden(); + .isEndpointOverridden(); if (endpointOverridden) { throw new IllegalStateException( - "This operation requires endpoint discovery, but an endpoint override was specified when the client was created. This is not supported."); + "This operation requires endpoint discovery, but an endpoint override was specified when the client was created. This is not supported."); } if (!endpointDiscoveryEnabled) { throw new IllegalStateException( - "This operation requires endpoint discovery, but endpoint discovery was disabled on the client."); + "This operation requires endpoint discovery, but endpoint discovery was disabled on the client."); } CompletableFuture endpointFuture = CompletableFuture.completedFuture(null); if (endpointDiscoveryEnabled) { CompletableFuture identityFuture = testDiscoveryRequiredRequest - .overrideConfiguration().flatMap(AwsRequestOverrideConfiguration::credentialsIdentityProvider) - .orElseGet(() -> clientConfiguration.option(AwsClientOption.CREDENTIALS_IDENTITY_PROVIDER)) - .resolveIdentity(); + .overrideConfiguration().flatMap(AwsRequestOverrideConfiguration::credentialsIdentityProvider) + .orElseGet(() -> clientConfiguration.option(AwsClientOption.CREDENTIALS_IDENTITY_PROVIDER)) + .resolveIdentity(); endpointFuture = identityFuture.thenCompose(credentials -> { EndpointDiscoveryRequest endpointDiscoveryRequest = EndpointDiscoveryRequest - .builder() - .required(true) - .defaultEndpoint( - clientConfiguration.option(SdkClientOption.CLIENT_ENDPOINT_PROVIDER).clientEndpoint()) - .overrideConfiguration(testDiscoveryRequiredRequest.overrideConfiguration().orElse(null)).build(); + .builder() + .required(true) + .defaultEndpoint( + clientConfiguration.option(SdkClientOption.CLIENT_ENDPOINT_PROVIDER).clientEndpoint()) + .overrideConfiguration(testDiscoveryRequiredRequest.overrideConfiguration().orElse(null)).build(); return endpointDiscoveryCache.getAsync(credentials.accessKeyId(), endpointDiscoveryRequest); }); } CompletableFuture executeFuture = endpointFuture - .thenCompose(cachedEndpoint -> clientHandler - .execute(new ClientExecutionParams() - .withOperationName("TestDiscoveryRequired").withProtocolMetadata(protocolMetadata) - .withMarshaller(new TestDiscoveryRequiredRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .discoveredEndpoint(cachedEndpoint).withInput(testDiscoveryRequiredRequest))); + .thenCompose(cachedEndpoint -> clientHandler + .execute(new ClientExecutionParams() + .withOperationName("TestDiscoveryRequired").withProtocolMetadata(protocolMetadata) + .withMarshaller(new TestDiscoveryRequiredRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .discoveredEndpoint(cachedEndpoint).withInput(testDiscoveryRequiredRequest))); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -407,12 +439,12 @@ public final String serviceName() { private > T init(T builder) { return builder.clientConfiguration(clientConfiguration) - .defaultServiceExceptionSupplier(EndpointDiscoveryTestException::builder).protocol(AwsJsonProtocol.AWS_JSON) - .protocolVersion("1.1"); + .defaultServiceExceptionSupplier(EndpointDiscoveryTestException::builder).protocol(AwsJsonProtocol.AWS_JSON) + .protocolVersion("1.1"); } private static List resolveMetricPublishers(SdkClientConfiguration clientConfiguration, - RequestOverrideConfiguration requestOverrideConfiguration) { + RequestOverrideConfiguration requestOverrideConfiguration) { List publishers = null; if (requestOverrideConfiguration != null) { publishers = requestOverrideConfiguration.metricPublishers(); @@ -456,7 +488,7 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, return configuration.build(); } EndpointDiscoveryTestServiceClientConfigurationBuilder serviceConfigBuilder = new EndpointDiscoveryTestServiceClientConfigurationBuilder( - configuration); + configuration); for (SdkPlugin plugin : plugins) { plugin.configureClient(serviceConfigBuilder); } @@ -465,12 +497,7 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, } private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata) { - return protocolFactory.createErrorResponseHandler(operationMetadata); - } - - private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { + JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { return protocolFactory.createErrorResponseHandler(operationMetadata, exceptionMetadataMapper); } diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-endpoint-discovery-sync.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-endpoint-discovery-sync.java index ba55ae6d349f..a473c2090cdb 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-endpoint-discovery-sync.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-endpoint-discovery-sync.java @@ -3,8 +3,10 @@ import java.net.URI; import java.util.Collections; import java.util.List; +import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.function.Consumer; +import java.util.function.Function; import software.amazon.awssdk.annotations.Generated; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration; @@ -32,6 +34,7 @@ import software.amazon.awssdk.metrics.MetricCollector; import software.amazon.awssdk.metrics.MetricPublisher; import software.amazon.awssdk.metrics.NoOpMetricCollector; +import software.amazon.awssdk.protocols.core.ExceptionMetadata; import software.amazon.awssdk.protocols.json.AwsJsonProtocol; import software.amazon.awssdk.protocols.json.AwsJsonProtocolFactory; import software.amazon.awssdk.protocols.json.BaseAwsJsonProtocolFactory; @@ -65,7 +68,7 @@ final class DefaultEndpointDiscoveryTestClient implements EndpointDiscoveryTestC private static final Logger log = Logger.loggerFor(DefaultEndpointDiscoveryTestClient.class); private static final AwsProtocolMetadata protocolMetadata = AwsProtocolMetadata.builder() - .serviceProtocol(AwsServiceProtocol.AWS_JSON).build(); + .serviceProtocol(AwsServiceProtocol.AWS_JSON).build(); private final SyncClientHandler clientHandler; @@ -81,7 +84,7 @@ protected DefaultEndpointDiscoveryTestClient(SdkClientConfiguration clientConfig this.protocolFactory = init(AwsJsonProtocolFactory.builder()).build(); if (clientConfiguration.option(SdkClientOption.ENDPOINT_DISCOVERY_ENABLED)) { this.endpointDiscoveryCache = EndpointDiscoveryRefreshCache.create(EndpointDiscoveryTestEndpointDiscoveryCacheLoader - .create(this)); + .create(this)); } } @@ -101,31 +104,39 @@ protected DefaultEndpointDiscoveryTestClient(SdkClientConfiguration clientConfig */ @Override public DescribeEndpointsResponse describeEndpoints(DescribeEndpointsRequest describeEndpointsRequest) - throws AwsServiceException, SdkClientException, EndpointDiscoveryTestException { + throws AwsServiceException, SdkClientException, EndpointDiscoveryTestException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, - DescribeEndpointsResponse::builder); - + DescribeEndpointsResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(describeEndpointsRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, describeEndpointsRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "AwsEndpointDiscoveryTest"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "DescribeEndpoints"); return clientHandler.execute(new ClientExecutionParams() - .withOperationName("DescribeEndpoints").withProtocolMetadata(protocolMetadata) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withInput(describeEndpointsRequest) - .withMetricCollector(apiCallMetricCollector) - .withMarshaller(new DescribeEndpointsRequestMarshaller(protocolFactory))); + .withOperationName("DescribeEndpoints").withProtocolMetadata(protocolMetadata) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withInput(describeEndpointsRequest) + .withMetricCollector(apiCallMetricCollector) + .withMarshaller(new DescribeEndpointsRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } @@ -147,54 +158,62 @@ public DescribeEndpointsResponse describeEndpoints(DescribeEndpointsRequest desc */ @Override public TestDiscoveryIdentifiersRequiredResponse testDiscoveryIdentifiersRequired( - TestDiscoveryIdentifiersRequiredRequest testDiscoveryIdentifiersRequiredRequest) throws AwsServiceException, - SdkClientException, EndpointDiscoveryTestException { + TestDiscoveryIdentifiersRequiredRequest testDiscoveryIdentifiersRequiredRequest) throws AwsServiceException, + SdkClientException, EndpointDiscoveryTestException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, TestDiscoveryIdentifiersRequiredResponse::builder); - + operationMetadata, TestDiscoveryIdentifiersRequiredResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); boolean endpointDiscoveryEnabled = clientConfiguration.option(SdkClientOption.ENDPOINT_DISCOVERY_ENABLED); boolean endpointOverridden = clientConfiguration.option(SdkClientOption.CLIENT_ENDPOINT_PROVIDER).isEndpointOverridden(); if (endpointOverridden) { throw new IllegalStateException( - "This operation requires endpoint discovery, but an endpoint override was specified when the client was created. This is not supported."); + "This operation requires endpoint discovery, but an endpoint override was specified when the client was created. This is not supported."); } if (!endpointDiscoveryEnabled) { throw new IllegalStateException( - "This operation requires endpoint discovery, but endpoint discovery was disabled on the client."); + "This operation requires endpoint discovery, but endpoint discovery was disabled on the client."); } URI cachedEndpoint = null; if (endpointDiscoveryEnabled) { CompletableFuture identityFuture = testDiscoveryIdentifiersRequiredRequest - .overrideConfiguration().flatMap(AwsRequestOverrideConfiguration::credentialsIdentityProvider) - .orElseGet(() -> clientConfiguration.option(AwsClientOption.CREDENTIALS_IDENTITY_PROVIDER)).resolveIdentity(); + .overrideConfiguration().flatMap(AwsRequestOverrideConfiguration::credentialsIdentityProvider) + .orElseGet(() -> clientConfiguration.option(AwsClientOption.CREDENTIALS_IDENTITY_PROVIDER)).resolveIdentity(); String key = CompletableFutureUtils.joinLikeSync(identityFuture).accessKeyId(); EndpointDiscoveryRequest endpointDiscoveryRequest = EndpointDiscoveryRequest.builder().required(true) - .defaultEndpoint(clientConfiguration.option(SdkClientOption.CLIENT_ENDPOINT_PROVIDER).clientEndpoint()) - .overrideConfiguration(testDiscoveryIdentifiersRequiredRequest.overrideConfiguration().orElse(null)).build(); + .defaultEndpoint(clientConfiguration.option(SdkClientOption.CLIENT_ENDPOINT_PROVIDER).clientEndpoint()) + .overrideConfiguration(testDiscoveryIdentifiersRequiredRequest.overrideConfiguration().orElse(null)).build(); cachedEndpoint = endpointDiscoveryCache.get(key, endpointDiscoveryRequest); } SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(testDiscoveryIdentifiersRequiredRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - testDiscoveryIdentifiersRequiredRequest.overrideConfiguration().orElse(null)); + testDiscoveryIdentifiersRequiredRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "AwsEndpointDiscoveryTest"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "TestDiscoveryIdentifiersRequired"); return clientHandler - .execute(new ClientExecutionParams() - .withOperationName("TestDiscoveryIdentifiersRequired").withProtocolMetadata(protocolMetadata) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .discoveredEndpoint(cachedEndpoint).withRequestConfiguration(clientConfiguration) - .withInput(testDiscoveryIdentifiersRequiredRequest).withMetricCollector(apiCallMetricCollector) - .withMarshaller(new TestDiscoveryIdentifiersRequiredRequestMarshaller(protocolFactory))); + .execute(new ClientExecutionParams() + .withOperationName("TestDiscoveryIdentifiersRequired").withProtocolMetadata(protocolMetadata) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .discoveredEndpoint(cachedEndpoint).withRequestConfiguration(clientConfiguration) + .withInput(testDiscoveryIdentifiersRequiredRequest).withMetricCollector(apiCallMetricCollector) + .withMarshaller(new TestDiscoveryIdentifiersRequiredRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } @@ -216,44 +235,52 @@ public TestDiscoveryIdentifiersRequiredResponse testDiscoveryIdentifiersRequired */ @Override public TestDiscoveryOptionalResponse testDiscoveryOptional(TestDiscoveryOptionalRequest testDiscoveryOptionalRequest) - throws AwsServiceException, SdkClientException, EndpointDiscoveryTestException { + throws AwsServiceException, SdkClientException, EndpointDiscoveryTestException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, TestDiscoveryOptionalResponse::builder); - + operationMetadata, TestDiscoveryOptionalResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); boolean endpointDiscoveryEnabled = clientConfiguration.option(SdkClientOption.ENDPOINT_DISCOVERY_ENABLED); boolean endpointOverridden = clientConfiguration.option(SdkClientOption.CLIENT_ENDPOINT_PROVIDER).isEndpointOverridden(); URI cachedEndpoint = null; if (endpointDiscoveryEnabled) { CompletableFuture identityFuture = testDiscoveryOptionalRequest - .overrideConfiguration().flatMap(AwsRequestOverrideConfiguration::credentialsIdentityProvider) - .orElseGet(() -> clientConfiguration.option(AwsClientOption.CREDENTIALS_IDENTITY_PROVIDER)).resolveIdentity(); + .overrideConfiguration().flatMap(AwsRequestOverrideConfiguration::credentialsIdentityProvider) + .orElseGet(() -> clientConfiguration.option(AwsClientOption.CREDENTIALS_IDENTITY_PROVIDER)).resolveIdentity(); String key = CompletableFutureUtils.joinLikeSync(identityFuture).accessKeyId(); EndpointDiscoveryRequest endpointDiscoveryRequest = EndpointDiscoveryRequest.builder().required(false) - .defaultEndpoint(clientConfiguration.option(SdkClientOption.CLIENT_ENDPOINT_PROVIDER).clientEndpoint()) - .overrideConfiguration(testDiscoveryOptionalRequest.overrideConfiguration().orElse(null)).build(); + .defaultEndpoint(clientConfiguration.option(SdkClientOption.CLIENT_ENDPOINT_PROVIDER).clientEndpoint()) + .overrideConfiguration(testDiscoveryOptionalRequest.overrideConfiguration().orElse(null)).build(); cachedEndpoint = endpointDiscoveryCache.get(key, endpointDiscoveryRequest); } SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(testDiscoveryOptionalRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, testDiscoveryOptionalRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "AwsEndpointDiscoveryTest"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "TestDiscoveryOptional"); return clientHandler.execute(new ClientExecutionParams() - .withOperationName("TestDiscoveryOptional").withProtocolMetadata(protocolMetadata) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .discoveredEndpoint(cachedEndpoint).withRequestConfiguration(clientConfiguration) - .withInput(testDiscoveryOptionalRequest).withMetricCollector(apiCallMetricCollector) - .withMarshaller(new TestDiscoveryOptionalRequestMarshaller(protocolFactory))); + .withOperationName("TestDiscoveryOptional").withProtocolMetadata(protocolMetadata) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .discoveredEndpoint(cachedEndpoint).withRequestConfiguration(clientConfiguration) + .withInput(testDiscoveryOptionalRequest).withMetricCollector(apiCallMetricCollector) + .withMarshaller(new TestDiscoveryOptionalRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } @@ -275,52 +302,60 @@ public TestDiscoveryOptionalResponse testDiscoveryOptional(TestDiscoveryOptional */ @Override public TestDiscoveryRequiredResponse testDiscoveryRequired(TestDiscoveryRequiredRequest testDiscoveryRequiredRequest) - throws AwsServiceException, SdkClientException, EndpointDiscoveryTestException { + throws AwsServiceException, SdkClientException, EndpointDiscoveryTestException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, TestDiscoveryRequiredResponse::builder); - + operationMetadata, TestDiscoveryRequiredResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); boolean endpointDiscoveryEnabled = clientConfiguration.option(SdkClientOption.ENDPOINT_DISCOVERY_ENABLED); boolean endpointOverridden = clientConfiguration.option(SdkClientOption.CLIENT_ENDPOINT_PROVIDER).isEndpointOverridden(); if (endpointOverridden) { throw new IllegalStateException( - "This operation requires endpoint discovery, but an endpoint override was specified when the client was created. This is not supported."); + "This operation requires endpoint discovery, but an endpoint override was specified when the client was created. This is not supported."); } if (!endpointDiscoveryEnabled) { throw new IllegalStateException( - "This operation requires endpoint discovery, but endpoint discovery was disabled on the client."); + "This operation requires endpoint discovery, but endpoint discovery was disabled on the client."); } URI cachedEndpoint = null; if (endpointDiscoveryEnabled) { CompletableFuture identityFuture = testDiscoveryRequiredRequest - .overrideConfiguration().flatMap(AwsRequestOverrideConfiguration::credentialsIdentityProvider) - .orElseGet(() -> clientConfiguration.option(AwsClientOption.CREDENTIALS_IDENTITY_PROVIDER)).resolveIdentity(); + .overrideConfiguration().flatMap(AwsRequestOverrideConfiguration::credentialsIdentityProvider) + .orElseGet(() -> clientConfiguration.option(AwsClientOption.CREDENTIALS_IDENTITY_PROVIDER)).resolveIdentity(); String key = CompletableFutureUtils.joinLikeSync(identityFuture).accessKeyId(); EndpointDiscoveryRequest endpointDiscoveryRequest = EndpointDiscoveryRequest.builder().required(true) - .defaultEndpoint(clientConfiguration.option(SdkClientOption.CLIENT_ENDPOINT_PROVIDER).clientEndpoint()) - .overrideConfiguration(testDiscoveryRequiredRequest.overrideConfiguration().orElse(null)).build(); + .defaultEndpoint(clientConfiguration.option(SdkClientOption.CLIENT_ENDPOINT_PROVIDER).clientEndpoint()) + .overrideConfiguration(testDiscoveryRequiredRequest.overrideConfiguration().orElse(null)).build(); cachedEndpoint = endpointDiscoveryCache.get(key, endpointDiscoveryRequest); } SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(testDiscoveryRequiredRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, testDiscoveryRequiredRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "AwsEndpointDiscoveryTest"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "TestDiscoveryRequired"); return clientHandler.execute(new ClientExecutionParams() - .withOperationName("TestDiscoveryRequired").withProtocolMetadata(protocolMetadata) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .discoveredEndpoint(cachedEndpoint).withRequestConfiguration(clientConfiguration) - .withInput(testDiscoveryRequiredRequest).withMetricCollector(apiCallMetricCollector) - .withMarshaller(new TestDiscoveryRequiredRequestMarshaller(protocolFactory))); + .withOperationName("TestDiscoveryRequired").withProtocolMetadata(protocolMetadata) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .discoveredEndpoint(cachedEndpoint).withRequestConfiguration(clientConfiguration) + .withInput(testDiscoveryRequiredRequest).withMetricCollector(apiCallMetricCollector) + .withMarshaller(new TestDiscoveryRequiredRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } @@ -332,7 +367,7 @@ public final String serviceName() { } private static List resolveMetricPublishers(SdkClientConfiguration clientConfiguration, - RequestOverrideConfiguration requestOverrideConfiguration) { + RequestOverrideConfiguration requestOverrideConfiguration) { List publishers = null; if (requestOverrideConfiguration != null) { publishers = requestOverrideConfiguration.metricPublishers(); @@ -347,8 +382,8 @@ private static List resolveMetricPublishers(SdkClientConfigurat } private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata) { - return protocolFactory.createErrorResponseHandler(operationMetadata); + JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { + return protocolFactory.createErrorResponseHandler(operationMetadata, exceptionMetadataMapper); } private void updateRetryStrategyClientConfiguration(SdkClientConfiguration.Builder configuration) { @@ -381,7 +416,7 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, return configuration.build(); } EndpointDiscoveryTestServiceClientConfigurationBuilder serviceConfigBuilder = new EndpointDiscoveryTestServiceClientConfigurationBuilder( - configuration); + configuration); for (SdkPlugin plugin : plugins) { plugin.configureClient(serviceConfigBuilder); } @@ -391,8 +426,8 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, private > T init(T builder) { return builder.clientConfiguration(clientConfiguration) - .defaultServiceExceptionSupplier(EndpointDiscoveryTestException::builder).protocol(AwsJsonProtocol.AWS_JSON) - .protocolVersion("1.1"); + .defaultServiceExceptionSupplier(EndpointDiscoveryTestException::builder).protocol(AwsJsonProtocol.AWS_JSON) + .protocolVersion("1.1"); } @Override diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-json-async-client-class.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-json-async-client-class.java index 1dfb81af4d68..68816b5809c0 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-json-async-client-class.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-json-async-client-class.java @@ -146,7 +146,7 @@ final class DefaultJsonAsyncClient implements JsonAsyncClient { private static final Logger log = LoggerFactory.getLogger(DefaultJsonAsyncClient.class); private static final AwsProtocolMetadata protocolMetadata = AwsProtocolMetadata.builder() - .serviceProtocol(AwsServiceProtocol.REST_JSON).build(); + .serviceProtocol(AwsServiceProtocol.REST_JSON).build(); private final AsyncClientHandler clientHandler; @@ -199,32 +199,43 @@ public JsonUtilities utilities() { public CompletableFuture aPostOperation(APostOperationRequest aPostOperationRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(aPostOperationRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, aPostOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "APostOperation"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, APostOperationResponse::builder); - + operationMetadata, APostOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); String hostPrefix = "{StringMember}-foo."; HostnameValidator.validateHostnameCompliant(aPostOperationRequest.stringMember(), "StringMember", - "aPostOperationRequest"); + "aPostOperationRequest"); String resolvedHostExpression = String.format("%s-foo.", aPostOperationRequest.stringMember()); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("APostOperation").withProtocolMetadata(protocolMetadata) - .withMarshaller(new APostOperationRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .hostPrefixExpression(resolvedHostExpression).withInput(aPostOperationRequest)); + .execute(new ClientExecutionParams() + .withOperationName("APostOperation").withProtocolMetadata(protocolMetadata) + .withMarshaller(new APostOperationRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .hostPrefixExpression(resolvedHostExpression).withInput(aPostOperationRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -262,32 +273,43 @@ public CompletableFuture aPostOperation(APostOperationRe */ @Override public CompletableFuture aPostOperationWithOutput( - APostOperationWithOutputRequest aPostOperationWithOutputRequest) { + APostOperationWithOutputRequest aPostOperationWithOutputRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(aPostOperationWithOutputRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, aPostOperationWithOutputRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "APostOperationWithOutput"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, APostOperationWithOutputResponse::builder); - + operationMetadata, APostOperationWithOutputResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("APostOperationWithOutput").withProtocolMetadata(protocolMetadata) - .withMarshaller(new APostOperationWithOutputRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(aPostOperationWithOutputRequest)); + .execute(new ClientExecutionParams() + .withOperationName("APostOperationWithOutput").withProtocolMetadata(protocolMetadata) + .withMarshaller(new APostOperationWithOutputRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(aPostOperationWithOutputRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -321,33 +343,44 @@ public CompletableFuture aPostOperationWithOut */ @Override public CompletableFuture bearerAuthOperation( - BearerAuthOperationRequest bearerAuthOperationRequest) { + BearerAuthOperationRequest bearerAuthOperationRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(bearerAuthOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, bearerAuthOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "BearerAuthOperation"); bearerAuthOperationRequest = applySignerOverride(bearerAuthOperationRequest, BearerTokenSigner.create()); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, BearerAuthOperationResponse::builder); - + operationMetadata, BearerAuthOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("BearerAuthOperation").withProtocolMetadata(protocolMetadata) - .withMarshaller(new BearerAuthOperationRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .credentialType(CredentialType.TOKEN).withInput(bearerAuthOperationRequest)); + .execute(new ClientExecutionParams() + .withOperationName("BearerAuthOperation").withProtocolMetadata(protocolMetadata) + .withMarshaller(new BearerAuthOperationRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .credentialType(CredentialType.TOKEN).withInput(bearerAuthOperationRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -381,76 +414,88 @@ public CompletableFuture bearerAuthOperation( */ @Override public CompletableFuture eventStreamOperation(EventStreamOperationRequest eventStreamOperationRequest, - Publisher requestStream, EventStreamOperationResponseHandler asyncResponseHandler) { + Publisher requestStream, EventStreamOperationResponseHandler asyncResponseHandler) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(eventStreamOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, eventStreamOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "EventStreamOperation"); eventStreamOperationRequest = applySignerOverride(eventStreamOperationRequest, EventStreamAws4Signer.create()); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = new AttachHttpMetadataResponseHandler( - protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationResponse::builder)); + protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationResponse::builder)); HttpResponseHandler voidResponseHandler = protocolFactory.createResponseHandler(JsonOperationMetadata - .builder().isPayloadJson(false).hasStreamingSuccessResponse(true).build(), VoidSdkResponse::builder); + .builder().isPayloadJson(false).hasStreamingSuccessResponse(true).build(), VoidSdkResponse::builder); HttpResponseHandler eventResponseHandler = protocolFactory.createResponseHandler( - JsonOperationMetadata.builder().isPayloadJson(true).hasStreamingSuccessResponse(false).build(), - EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventOne", EventStream::eventOneBuilder) - .putSdkPojoSupplier("EventTheSecond", EventStream::eventTheSecondBuilder) - .putSdkPojoSupplier("secondEventOne", EventStream::secondEventOneBuilder) - .putSdkPojoSupplier("eventThree", EventStream::eventThreeBuilder) - .defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN)).build()); + JsonOperationMetadata.builder().isPayloadJson(true).hasStreamingSuccessResponse(false).build(), + EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventOne", EventStream::eventOneBuilder) + .putSdkPojoSupplier("EventTheSecond", EventStream::eventTheSecondBuilder) + .putSdkPojoSupplier("secondEventOne", EventStream::secondEventOneBuilder) + .putSdkPojoSupplier("eventThree", EventStream::eventThreeBuilder) + .defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN)).build()); Function> eventstreamExceptionMetadataMapper = errorCode -> { switch (errorCode) { - case "errorOne": - return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) - .exceptionBuilderSupplier(InvalidInputException::builder).build()); - case "errorTwo": - return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) - .exceptionBuilderSupplier(InvalidInputException::builder).build()); - default: - return Optional.empty(); + case "errorOne": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "errorTwo": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); } }; HttpResponseHandler errorEventResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata, eventstreamExceptionMetadataMapper); + operationMetadata, eventstreamExceptionMetadataMapper); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); EventStreamTaggedUnionJsonMarshaller eventMarshaller = EventStreamTaggedUnionJsonMarshaller.builder() - .putMarshaller(DefaultInputEvent.class, new InputEventMarshaller(protocolFactory)).build(); + .putMarshaller(DefaultInputEvent.class, new InputEventMarshaller(protocolFactory)).build(); SdkPublisher eventPublisher = SdkPublisher.adapt(requestStream); Publisher adapted = eventPublisher.map(event -> eventMarshaller.marshall(event)).map( - AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer); + AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer); CompletableFuture future = new CompletableFuture<>(); EventStreamAsyncResponseTransformer asyncResponseTransformer = EventStreamAsyncResponseTransformer - . builder().eventStreamResponseHandler(asyncResponseHandler) - .eventResponseHandler(eventResponseHandler).initialResponseHandler(responseHandler) - .exceptionResponseHandler(errorEventResponseHandler).future(future).executor(executor) - .serviceName(serviceName()).build(); + . builder().eventStreamResponseHandler(asyncResponseHandler) + .eventResponseHandler(eventResponseHandler).initialResponseHandler(responseHandler) + .exceptionResponseHandler(errorEventResponseHandler).future(future).executor(executor) + .serviceName(serviceName()).build(); RestEventStreamAsyncResponseTransformer restAsyncResponseTransformer = RestEventStreamAsyncResponseTransformer - . builder() - .eventStreamAsyncResponseTransformer(asyncResponseTransformer) - .eventStreamResponseHandler(asyncResponseHandler).build(); + . builder() + .eventStreamAsyncResponseTransformer(asyncResponseTransformer) + .eventStreamResponseHandler(asyncResponseHandler).build(); CompletableFuture executeFuture = clientHandler.execute( - new ClientExecutionParams() - .withOperationName("EventStreamOperation").withProtocolMetadata(protocolMetadata) - .withMarshaller(new EventStreamOperationRequestMarshaller(protocolFactory)) - .withAsyncRequestBody(AsyncRequestBody.fromPublisher(adapted)).withFullDuplex(true) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(eventStreamOperationRequest), restAsyncResponseTransformer); + new ClientExecutionParams() + .withOperationName("EventStreamOperation").withProtocolMetadata(protocolMetadata) + .withMarshaller(new EventStreamOperationRequestMarshaller(protocolFactory)) + .withAsyncRequestBody(AsyncRequestBody.fromPublisher(adapted)).withFullDuplex(true) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(eventStreamOperationRequest), restAsyncResponseTransformer); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { if (e != null) { try { @@ -465,7 +510,7 @@ public CompletableFuture eventStreamOperation(EventStreamOperationRequest return CompletableFutureUtils.forwardExceptionTo(future, executeFuture); } catch (Throwable t) { runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> asyncResponseHandler.exceptionOccurred(t)); + () -> asyncResponseHandler.exceptionOccurred(t)); metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); return CompletableFutureUtils.failedFuture(t); } @@ -494,41 +539,52 @@ public CompletableFuture eventStreamOperation(EventStreamOperationRequest */ @Override public CompletableFuture eventStreamOperationWithOnlyInput( - EventStreamOperationWithOnlyInputRequest eventStreamOperationWithOnlyInputRequest, - Publisher requestStream) { + EventStreamOperationWithOnlyInputRequest eventStreamOperationWithOnlyInputRequest, + Publisher requestStream) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(eventStreamOperationWithOnlyInputRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - eventStreamOperationWithOnlyInputRequest.overrideConfiguration().orElse(null)); + eventStreamOperationWithOnlyInputRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "EventStreamOperationWithOnlyInput"); eventStreamOperationWithOnlyInputRequest = applySignerOverride(eventStreamOperationWithOnlyInputRequest, - EventStreamAws4Signer.create()); + EventStreamAws4Signer.create()); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory - .createResponseHandler(operationMetadata, EventStreamOperationWithOnlyInputResponse::builder); - + .createResponseHandler(operationMetadata, EventStreamOperationWithOnlyInputResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); EventStreamTaggedUnionJsonMarshaller eventMarshaller = EventStreamTaggedUnionJsonMarshaller.builder() - .putMarshaller(DefaultInputEventOne.class, new InputEventMarshaller(protocolFactory)) - .putMarshaller(DefaultInputEventTwo.class, new InputEventTwoMarshaller(protocolFactory)).build(); + .putMarshaller(DefaultInputEventOne.class, new InputEventMarshaller(protocolFactory)) + .putMarshaller(DefaultInputEventTwo.class, new InputEventTwoMarshaller(protocolFactory)).build(); SdkPublisher eventPublisher = SdkPublisher.adapt(requestStream); Publisher adapted = eventPublisher.map(event -> eventMarshaller.marshall(event)).map( - AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer); + AwsClientHandlerUtils::encodeEventStreamRequestToByteBuffer); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("EventStreamOperationWithOnlyInput").withProtocolMetadata(protocolMetadata) - .withMarshaller(new EventStreamOperationWithOnlyInputRequestMarshaller(protocolFactory)) - .withAsyncRequestBody(AsyncRequestBody.fromPublisher(adapted)).withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector).withInput(eventStreamOperationWithOnlyInputRequest)); + .execute(new ClientExecutionParams() + .withOperationName("EventStreamOperationWithOnlyInput").withProtocolMetadata(protocolMetadata) + .withMarshaller(new EventStreamOperationWithOnlyInputRequestMarshaller(protocolFactory)) + .withAsyncRequestBody(AsyncRequestBody.fromPublisher(adapted)).withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector).withInput(eventStreamOperationWithOnlyInputRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -563,72 +619,84 @@ public CompletableFuture eventStreamO */ @Override public CompletableFuture eventStreamOperationWithOnlyOutput( - EventStreamOperationWithOnlyOutputRequest eventStreamOperationWithOnlyOutputRequest, - EventStreamOperationWithOnlyOutputResponseHandler asyncResponseHandler) { + EventStreamOperationWithOnlyOutputRequest eventStreamOperationWithOnlyOutputRequest, + EventStreamOperationWithOnlyOutputResponseHandler asyncResponseHandler) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(eventStreamOperationWithOnlyOutputRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - eventStreamOperationWithOnlyOutputRequest.overrideConfiguration().orElse(null)); + eventStreamOperationWithOnlyOutputRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "EventStreamOperationWithOnlyOutput"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = new AttachHttpMetadataResponseHandler( - protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationWithOnlyOutputResponse::builder)); + protocolFactory.createResponseHandler(operationMetadata, EventStreamOperationWithOnlyOutputResponse::builder)); HttpResponseHandler voidResponseHandler = protocolFactory.createResponseHandler(JsonOperationMetadata - .builder().isPayloadJson(false).hasStreamingSuccessResponse(true).build(), VoidSdkResponse::builder); + .builder().isPayloadJson(false).hasStreamingSuccessResponse(true).build(), VoidSdkResponse::builder); HttpResponseHandler eventResponseHandler = protocolFactory.createResponseHandler( - JsonOperationMetadata.builder().isPayloadJson(true).hasStreamingSuccessResponse(false).build(), - EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventOne", EventStream::eventOneBuilder) - .putSdkPojoSupplier("EventTheSecond", EventStream::eventTheSecondBuilder) - .putSdkPojoSupplier("secondEventOne", EventStream::secondEventOneBuilder) - .putSdkPojoSupplier("eventThree", EventStream::eventThreeBuilder) - .defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN)).build()); + JsonOperationMetadata.builder().isPayloadJson(true).hasStreamingSuccessResponse(false).build(), + EventStreamTaggedUnionPojoSupplier.builder().putSdkPojoSupplier("EventOne", EventStream::eventOneBuilder) + .putSdkPojoSupplier("EventTheSecond", EventStream::eventTheSecondBuilder) + .putSdkPojoSupplier("secondEventOne", EventStream::secondEventOneBuilder) + .putSdkPojoSupplier("eventThree", EventStream::eventThreeBuilder) + .defaultSdkPojoSupplier(() -> new SdkPojoBuilder(EventStream.UNKNOWN)).build()); Function> eventstreamExceptionMetadataMapper = errorCode -> { switch (errorCode) { - case "errorOne": - return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) - .exceptionBuilderSupplier(InvalidInputException::builder).build()); - case "errorTwo": - return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) - .exceptionBuilderSupplier(InvalidInputException::builder).build()); - default: - return Optional.empty(); + case "errorOne": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + case "errorTwo": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); } }; HttpResponseHandler errorEventResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata, eventstreamExceptionMetadataMapper); + operationMetadata, eventstreamExceptionMetadataMapper); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture future = new CompletableFuture<>(); EventStreamAsyncResponseTransformer asyncResponseTransformer = EventStreamAsyncResponseTransformer - . builder() - .eventStreamResponseHandler(asyncResponseHandler).eventResponseHandler(eventResponseHandler) - .initialResponseHandler(responseHandler).exceptionResponseHandler(errorEventResponseHandler).future(future) - .executor(executor).serviceName(serviceName()).build(); + . builder() + .eventStreamResponseHandler(asyncResponseHandler).eventResponseHandler(eventResponseHandler) + .initialResponseHandler(responseHandler).exceptionResponseHandler(errorEventResponseHandler).future(future) + .executor(executor).serviceName(serviceName()).build(); RestEventStreamAsyncResponseTransformer restAsyncResponseTransformer = RestEventStreamAsyncResponseTransformer - . builder() - .eventStreamAsyncResponseTransformer(asyncResponseTransformer) - .eventStreamResponseHandler(asyncResponseHandler).build(); + . builder() + .eventStreamAsyncResponseTransformer(asyncResponseTransformer) + .eventStreamResponseHandler(asyncResponseHandler).build(); CompletableFuture executeFuture = clientHandler - .execute( - new ClientExecutionParams() - .withOperationName("EventStreamOperationWithOnlyOutput") - .withProtocolMetadata(protocolMetadata) - .withMarshaller(new EventStreamOperationWithOnlyOutputRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(eventStreamOperationWithOnlyOutputRequest), restAsyncResponseTransformer); + .execute( + new ClientExecutionParams() + .withOperationName("EventStreamOperationWithOnlyOutput") + .withProtocolMetadata(protocolMetadata) + .withMarshaller(new EventStreamOperationWithOnlyOutputRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(eventStreamOperationWithOnlyOutputRequest), restAsyncResponseTransformer); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { if (e != null) { try { @@ -643,7 +711,7 @@ public CompletableFuture eventStreamOperationWithOnlyOutput( return CompletableFutureUtils.forwardExceptionTo(future, executeFuture); } catch (Throwable t) { runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> asyncResponseHandler.exceptionOccurred(t)); + () -> asyncResponseHandler.exceptionOccurred(t)); metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); return CompletableFutureUtils.failedFuture(t); } @@ -671,40 +739,51 @@ public CompletableFuture eventStreamOperationWithOnlyOutput( */ @Override public CompletableFuture getOperationWithChecksum( - GetOperationWithChecksumRequest getOperationWithChecksumRequest) { + GetOperationWithChecksumRequest getOperationWithChecksumRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(getOperationWithChecksumRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, getOperationWithChecksumRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "GetOperationWithChecksum"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(false).build(); + .isPayloadJson(false).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, GetOperationWithChecksumResponse::builder); - + operationMetadata, GetOperationWithChecksumResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("GetOperationWithChecksum") - .withProtocolMetadata(protocolMetadata) - .withMarshaller(new GetOperationWithChecksumRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector) - .putExecutionAttribute( - SdkInternalExecutionAttribute.HTTP_CHECKSUM, - HttpChecksum.builder().requestChecksumRequired(true).isRequestStreaming(false) - .requestAlgorithm(getOperationWithChecksumRequest.checksumAlgorithmAsString()) - .requestAlgorithmHeader("x-amz-sdk-checksum-algorithm").build()) - .withInput(getOperationWithChecksumRequest)); + .execute(new ClientExecutionParams() + .withOperationName("GetOperationWithChecksum") + .withProtocolMetadata(protocolMetadata) + .withMarshaller(new GetOperationWithChecksumRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector) + .putExecutionAttribute( + SdkInternalExecutionAttribute.HTTP_CHECKSUM, + HttpChecksum.builder().requestChecksumRequired(true).isRequestStreaming(false) + .requestAlgorithm(getOperationWithChecksumRequest.checksumAlgorithmAsString()) + .requestAlgorithmHeader("x-amz-sdk-checksum-algorithm").build()) + .withInput(getOperationWithChecksumRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -742,32 +821,43 @@ public CompletableFuture getOperationWithCheck */ @Override public CompletableFuture getWithoutRequiredMembers( - GetWithoutRequiredMembersRequest getWithoutRequiredMembersRequest) { + GetWithoutRequiredMembersRequest getWithoutRequiredMembersRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(getWithoutRequiredMembersRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, getWithoutRequiredMembersRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "GetWithoutRequiredMembers"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, GetWithoutRequiredMembersResponse::builder); - + operationMetadata, GetWithoutRequiredMembersResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("GetWithoutRequiredMembers").withProtocolMetadata(protocolMetadata) - .withMarshaller(new GetWithoutRequiredMembersRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(getWithoutRequiredMembersRequest)); + .execute(new ClientExecutionParams() + .withOperationName("GetWithoutRequiredMembers").withProtocolMetadata(protocolMetadata) + .withMarshaller(new GetWithoutRequiredMembersRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(getWithoutRequiredMembersRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -802,36 +892,47 @@ public CompletableFuture getWithoutRequiredMe */ @Override public CompletableFuture operationWithChecksumRequired( - OperationWithChecksumRequiredRequest operationWithChecksumRequiredRequest) { + OperationWithChecksumRequiredRequest operationWithChecksumRequiredRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(operationWithChecksumRequiredRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - operationWithChecksumRequiredRequest.overrideConfiguration().orElse(null)); + operationWithChecksumRequiredRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "OperationWithChecksumRequired"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OperationWithChecksumRequiredResponse::builder); - + operationMetadata, OperationWithChecksumRequiredResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("OperationWithChecksumRequired") - .withProtocolMetadata(protocolMetadata) - .withMarshaller(new OperationWithChecksumRequiredRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector) - .putExecutionAttribute(SdkInternalExecutionAttribute.HTTP_CHECKSUM_REQUIRED, - HttpChecksumRequired.create()).withInput(operationWithChecksumRequiredRequest)); + .execute(new ClientExecutionParams() + .withOperationName("OperationWithChecksumRequired") + .withProtocolMetadata(protocolMetadata) + .withMarshaller(new OperationWithChecksumRequiredRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector) + .putExecutionAttribute(SdkInternalExecutionAttribute.HTTP_CHECKSUM_REQUIRED, + HttpChecksumRequired.create()).withInput(operationWithChecksumRequiredRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -866,37 +967,48 @@ public CompletableFuture operationWithChe */ @Override public CompletableFuture operationWithRequestCompression( - OperationWithRequestCompressionRequest operationWithRequestCompressionRequest) { + OperationWithRequestCompressionRequest operationWithRequestCompressionRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(operationWithRequestCompressionRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - operationWithRequestCompressionRequest.overrideConfiguration().orElse(null)); + operationWithRequestCompressionRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "OperationWithRequestCompression"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OperationWithRequestCompressionResponse::builder); - + operationMetadata, OperationWithRequestCompressionResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("OperationWithRequestCompression") - .withProtocolMetadata(protocolMetadata) - .withMarshaller(new OperationWithRequestCompressionRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector) - .putExecutionAttribute(SdkInternalExecutionAttribute.REQUEST_COMPRESSION, - RequestCompression.builder().encodings("gzip").isStreaming(false).build()) - .withInput(operationWithRequestCompressionRequest)); + .execute(new ClientExecutionParams() + .withOperationName("OperationWithRequestCompression") + .withProtocolMetadata(protocolMetadata) + .withMarshaller(new OperationWithRequestCompressionRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector) + .putExecutionAttribute(SdkInternalExecutionAttribute.REQUEST_COMPRESSION, + RequestCompression.builder().encodings("gzip").isStreaming(false).build()) + .withInput(operationWithRequestCompressionRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -931,32 +1043,43 @@ public CompletableFuture operationWithR */ @Override public CompletableFuture paginatedOperationWithResultKey( - PaginatedOperationWithResultKeyRequest paginatedOperationWithResultKeyRequest) { + PaginatedOperationWithResultKeyRequest paginatedOperationWithResultKeyRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(paginatedOperationWithResultKeyRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - paginatedOperationWithResultKeyRequest.overrideConfiguration().orElse(null)); + paginatedOperationWithResultKeyRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "PaginatedOperationWithResultKey"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, PaginatedOperationWithResultKeyResponse::builder); - + operationMetadata, PaginatedOperationWithResultKeyResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("PaginatedOperationWithResultKey").withProtocolMetadata(protocolMetadata) - .withMarshaller(new PaginatedOperationWithResultKeyRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(paginatedOperationWithResultKeyRequest)); + .execute(new ClientExecutionParams() + .withOperationName("PaginatedOperationWithResultKey").withProtocolMetadata(protocolMetadata) + .withMarshaller(new PaginatedOperationWithResultKeyRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(paginatedOperationWithResultKeyRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -991,32 +1114,43 @@ public CompletableFuture paginatedOpera */ @Override public CompletableFuture paginatedOperationWithoutResultKey( - PaginatedOperationWithoutResultKeyRequest paginatedOperationWithoutResultKeyRequest) { + PaginatedOperationWithoutResultKeyRequest paginatedOperationWithoutResultKeyRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(paginatedOperationWithoutResultKeyRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - paginatedOperationWithoutResultKeyRequest.overrideConfiguration().orElse(null)); + paginatedOperationWithoutResultKeyRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "PaginatedOperationWithoutResultKey"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory - .createResponseHandler(operationMetadata, PaginatedOperationWithoutResultKeyResponse::builder); - + .createResponseHandler(operationMetadata, PaginatedOperationWithoutResultKeyResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("PaginatedOperationWithoutResultKey").withProtocolMetadata(protocolMetadata) - .withMarshaller(new PaginatedOperationWithoutResultKeyRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(paginatedOperationWithoutResultKeyRequest)); + .execute(new ClientExecutionParams() + .withOperationName("PaginatedOperationWithoutResultKey").withProtocolMetadata(protocolMetadata) + .withMarshaller(new PaginatedOperationWithoutResultKeyRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(paginatedOperationWithoutResultKeyRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -1068,62 +1202,73 @@ public CompletableFuture paginatedOp */ @Override public CompletableFuture putOperationWithChecksum( - PutOperationWithChecksumRequest putOperationWithChecksumRequest, AsyncRequestBody requestBody, - AsyncResponseTransformer asyncResponseTransformer) { + PutOperationWithChecksumRequest putOperationWithChecksumRequest, AsyncRequestBody requestBody, + AsyncResponseTransformer asyncResponseTransformer) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(putOperationWithChecksumRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, putOperationWithChecksumRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "PutOperationWithChecksum"); Pair, CompletableFuture> pair = AsyncResponseTransformerUtils - .wrapWithEndOfStreamFuture(asyncResponseTransformer); + .wrapWithEndOfStreamFuture(asyncResponseTransformer); asyncResponseTransformer = pair.left(); CompletableFuture endOfStreamFuture = pair.right(); if (!isSignerOverridden(clientConfiguration)) { putOperationWithChecksumRequest = applySignerOverride(putOperationWithChecksumRequest, AsyncAws4Signer.create()); } JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(true) - .isPayloadJson(false).build(); + .isPayloadJson(false).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, PutOperationWithChecksumResponse::builder); - + operationMetadata, PutOperationWithChecksumResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler.execute( - new ClientExecutionParams() - .withOperationName("PutOperationWithChecksum") - .withProtocolMetadata(protocolMetadata) - .withMarshaller( - AsyncStreamingRequestMarshaller.builder() - .delegateMarshaller(new PutOperationWithChecksumRequestMarshaller(protocolFactory)) - .asyncRequestBody(requestBody).build()) - .withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector) - .withAsyncRequestBody(requestBody) - .putExecutionAttribute( - SdkInternalExecutionAttribute.HTTP_CHECKSUM, - HttpChecksum - .builder() - .requestChecksumRequired(false) - .isRequestStreaming(true) - .requestValidationMode(putOperationWithChecksumRequest.checksumModeAsString()) - .responseAlgorithmsV2(DefaultChecksumAlgorithm.CRC32C, - DefaultChecksumAlgorithm.CRC32, DefaultChecksumAlgorithm.SHA1, - DefaultChecksumAlgorithm.SHA256).build()) - .withInput(putOperationWithChecksumRequest), asyncResponseTransformer); + new ClientExecutionParams() + .withOperationName("PutOperationWithChecksum") + .withProtocolMetadata(protocolMetadata) + .withMarshaller( + AsyncStreamingRequestMarshaller.builder() + .delegateMarshaller(new PutOperationWithChecksumRequestMarshaller(protocolFactory)) + .asyncRequestBody(requestBody).build()) + .withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector) + .withAsyncRequestBody(requestBody) + .putExecutionAttribute( + SdkInternalExecutionAttribute.HTTP_CHECKSUM, + HttpChecksum + .builder() + .requestChecksumRequired(false) + .isRequestStreaming(true) + .requestValidationMode(putOperationWithChecksumRequest.checksumModeAsString()) + .responseAlgorithmsV2(DefaultChecksumAlgorithm.CRC32C, + DefaultChecksumAlgorithm.CRC32, DefaultChecksumAlgorithm.SHA1, + DefaultChecksumAlgorithm.SHA256).build()) + .withInput(putOperationWithChecksumRequest), asyncResponseTransformer); AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { if (e != null) { runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(e)); + () -> finalAsyncResponseTransformer.exceptionOccurred(e)); } endOfStreamFuture.whenComplete((r2, e2) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); @@ -1134,7 +1279,7 @@ public CompletableFuture putOperationWithChecksum( } catch (Throwable t) { AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(t)); + () -> finalAsyncResponseTransformer.exceptionOccurred(t)); metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); return CompletableFutureUtils.failedFuture(t); } @@ -1167,13 +1312,13 @@ public CompletableFuture putOperationWithChecksum( */ @Override public CompletableFuture streamingInputOperation( - StreamingInputOperationRequest streamingInputOperationRequest, AsyncRequestBody requestBody) { + StreamingInputOperationRequest streamingInputOperationRequest, AsyncRequestBody requestBody) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(streamingInputOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, streamingInputOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "StreamingInputOperation"); @@ -1181,25 +1326,36 @@ public CompletableFuture streamingInputOperatio streamingInputOperationRequest = applySignerOverride(streamingInputOperationRequest, AsyncAws4Signer.create()); } JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, StreamingInputOperationResponse::builder); - + operationMetadata, StreamingInputOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("StreamingInputOperation") - .withProtocolMetadata(protocolMetadata) - .withMarshaller( - AsyncStreamingRequestMarshaller.builder() - .delegateMarshaller(new StreamingInputOperationRequestMarshaller(protocolFactory)) - .asyncRequestBody(requestBody).build()).withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector).withAsyncRequestBody(requestBody) - .withInput(streamingInputOperationRequest)); + .execute(new ClientExecutionParams() + .withOperationName("StreamingInputOperation") + .withProtocolMetadata(protocolMetadata) + .withMarshaller( + AsyncStreamingRequestMarshaller.builder() + .delegateMarshaller(new StreamingInputOperationRequestMarshaller(protocolFactory)) + .asyncRequestBody(requestBody).build()).withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector).withAsyncRequestBody(requestBody) + .withInput(streamingInputOperationRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -1243,51 +1399,62 @@ public CompletableFuture streamingInputOperatio */ @Override public CompletableFuture streamingInputOutputOperation( - StreamingInputOutputOperationRequest streamingInputOutputOperationRequest, AsyncRequestBody requestBody, - AsyncResponseTransformer asyncResponseTransformer) { + StreamingInputOutputOperationRequest streamingInputOutputOperationRequest, AsyncRequestBody requestBody, + AsyncResponseTransformer asyncResponseTransformer) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(streamingInputOutputOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - streamingInputOutputOperationRequest.overrideConfiguration().orElse(null)); + streamingInputOutputOperationRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "StreamingInputOutputOperation"); Pair, CompletableFuture> pair = AsyncResponseTransformerUtils - .wrapWithEndOfStreamFuture(asyncResponseTransformer); + .wrapWithEndOfStreamFuture(asyncResponseTransformer); asyncResponseTransformer = pair.left(); CompletableFuture endOfStreamFuture = pair.right(); streamingInputOutputOperationRequest = applySignerOverride(streamingInputOutputOperationRequest, - Aws4UnsignedPayloadSigner.create()); + Aws4UnsignedPayloadSigner.create()); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(true) - .isPayloadJson(false).build(); + .isPayloadJson(false).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, StreamingInputOutputOperationResponse::builder); - + operationMetadata, StreamingInputOutputOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler.execute( - new ClientExecutionParams() - .withOperationName("StreamingInputOutputOperation") - .withProtocolMetadata(protocolMetadata) - .withMarshaller( - AsyncStreamingRequestMarshaller - .builder() - .delegateMarshaller( - new StreamingInputOutputOperationRequestMarshaller(protocolFactory)) - .asyncRequestBody(requestBody).transferEncoding(true).build()) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withAsyncRequestBody(requestBody).withInput(streamingInputOutputOperationRequest), - asyncResponseTransformer); + new ClientExecutionParams() + .withOperationName("StreamingInputOutputOperation") + .withProtocolMetadata(protocolMetadata) + .withMarshaller( + AsyncStreamingRequestMarshaller + .builder() + .delegateMarshaller( + new StreamingInputOutputOperationRequestMarshaller(protocolFactory)) + .asyncRequestBody(requestBody).transferEncoding(true).build()) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withAsyncRequestBody(requestBody).withInput(streamingInputOutputOperationRequest), + asyncResponseTransformer); AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { if (e != null) { runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(e)); + () -> finalAsyncResponseTransformer.exceptionOccurred(e)); } endOfStreamFuture.whenComplete((r2, e2) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); @@ -1298,7 +1465,7 @@ public CompletableFuture streamingInputOutputOperation( } catch (Throwable t) { AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(t)); + () -> finalAsyncResponseTransformer.exceptionOccurred(t)); metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); return CompletableFutureUtils.failedFuture(t); } @@ -1331,42 +1498,53 @@ public CompletableFuture streamingInputOutputOperation( */ @Override public CompletableFuture streamingOutputOperation( - StreamingOutputOperationRequest streamingOutputOperationRequest, - AsyncResponseTransformer asyncResponseTransformer) { + StreamingOutputOperationRequest streamingOutputOperationRequest, + AsyncResponseTransformer asyncResponseTransformer) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(streamingOutputOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, streamingOutputOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "StreamingOutputOperation"); Pair, CompletableFuture> pair = AsyncResponseTransformerUtils - .wrapWithEndOfStreamFuture(asyncResponseTransformer); + .wrapWithEndOfStreamFuture(asyncResponseTransformer); asyncResponseTransformer = pair.left(); CompletableFuture endOfStreamFuture = pair.right(); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(true) - .isPayloadJson(false).build(); + .isPayloadJson(false).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, StreamingOutputOperationResponse::builder); - + operationMetadata, StreamingOutputOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler.execute( - new ClientExecutionParams() - .withOperationName("StreamingOutputOperation").withProtocolMetadata(protocolMetadata) - .withMarshaller(new StreamingOutputOperationRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(streamingOutputOperationRequest), asyncResponseTransformer); + new ClientExecutionParams() + .withOperationName("StreamingOutputOperation").withProtocolMetadata(protocolMetadata) + .withMarshaller(new StreamingOutputOperationRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(streamingOutputOperationRequest), asyncResponseTransformer); AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { if (e != null) { runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(e)); + () -> finalAsyncResponseTransformer.exceptionOccurred(e)); } endOfStreamFuture.whenComplete((r2, e2) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); @@ -1377,7 +1555,7 @@ public CompletableFuture streamingOutputOperation( } catch (Throwable t) { AsyncResponseTransformer finalAsyncResponseTransformer = asyncResponseTransformer; runAndLogError(log, "Exception thrown in exceptionOccurred callback, ignoring", - () -> finalAsyncResponseTransformer.exceptionOccurred(t)); + () -> finalAsyncResponseTransformer.exceptionOccurred(t)); metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); return CompletableFutureUtils.failedFuture(t); } @@ -1399,18 +1577,12 @@ public final String serviceName() { } private > T init(T builder) { - return builder - .clientConfiguration(clientConfiguration) - .defaultServiceExceptionSupplier(JsonException::builder) - .protocol(AwsJsonProtocol.REST_JSON) - .protocolVersion("1.1") - .registerModeledException( - ExceptionMetadata.builder().errorCode("InvalidInput") - .exceptionBuilderSupplier(InvalidInputException::builder).httpStatusCode(400).build()); + return builder.clientConfiguration(clientConfiguration).defaultServiceExceptionSupplier(JsonException::builder) + .protocol(AwsJsonProtocol.REST_JSON).protocolVersion("1.1"); } private static List resolveMetricPublishers(SdkClientConfiguration clientConfiguration, - RequestOverrideConfiguration requestOverrideConfiguration) { + RequestOverrideConfiguration requestOverrideConfiguration) { List publishers = null; if (requestOverrideConfiguration != null) { publishers = requestOverrideConfiguration.metricPublishers(); @@ -1430,8 +1602,8 @@ private T applySignerOverride(T request, Signer signer) } Consumer signerOverride = b -> b.signer(signer).build(); AwsRequestOverrideConfiguration overrideConfiguration = request.overrideConfiguration() - .map(c -> c.toBuilder().applyMutation(signerOverride).build()) - .orElse((AwsRequestOverrideConfiguration.builder().applyMutation(signerOverride).build())); + .map(c -> c.toBuilder().applyMutation(signerOverride).build()) + .orElse((AwsRequestOverrideConfiguration.builder().applyMutation(signerOverride).build())); return (T) request.toBuilder().overrideConfiguration(overrideConfiguration).build(); } @@ -1477,12 +1649,7 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, } private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata) { - return protocolFactory.createErrorResponseHandler(operationMetadata); - } - - private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { + JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { return protocolFactory.createErrorResponseHandler(operationMetadata, exceptionMetadataMapper); } diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-json-client-class.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-json-client-class.java index 8b32410d4bb9..cded3b503aec 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-json-client-class.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-json-client-class.java @@ -2,7 +2,9 @@ import java.util.Collections; import java.util.List; +import java.util.Optional; import java.util.function.Consumer; +import java.util.function.Function; import software.amazon.awssdk.annotations.Generated; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.auth.signer.Aws4UnsignedPayloadSigner; @@ -143,9 +145,20 @@ public APostOperationResponse aPostOperation(APostOperationRequest aPostOperatio HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, APostOperationResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(aPostOperationRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, aPostOperationRequest .overrideConfiguration().orElse(null)); @@ -199,9 +212,20 @@ public APostOperationWithOutputResponse aPostOperationWithOutput( HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, APostOperationWithOutputResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(aPostOperationWithOutputRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, aPostOperationWithOutputRequest @@ -249,9 +273,20 @@ public BearerAuthOperationResponse bearerAuthOperation(BearerAuthOperationReques HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, BearerAuthOperationResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(bearerAuthOperationRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, bearerAuthOperationRequest @@ -298,9 +333,20 @@ public GetOperationWithChecksumResponse getOperationWithChecksum( HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, GetOperationWithChecksumResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(getOperationWithChecksumRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, getOperationWithChecksumRequest @@ -360,9 +406,20 @@ public GetWithoutRequiredMembersResponse getWithoutRequiredMembers( HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, GetWithoutRequiredMembersResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(getWithoutRequiredMembersRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, getWithoutRequiredMembersRequest @@ -410,9 +467,20 @@ public OperationWithChecksumRequiredResponse operationWithChecksumRequired( HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, OperationWithChecksumRequiredResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(operationWithChecksumRequiredRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, @@ -465,9 +533,20 @@ public OperationWithRequestCompressionResponse operationWithRequestCompression( HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, OperationWithRequestCompressionResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(operationWithRequestCompressionRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, @@ -520,9 +599,20 @@ public PaginatedOperationWithResultKeyResponse paginatedOperationWithResultKey( HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, PaginatedOperationWithResultKeyResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(paginatedOperationWithResultKeyRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, @@ -570,9 +660,20 @@ public PaginatedOperationWithoutResultKeyResponse paginatedOperationWithoutResul HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, PaginatedOperationWithoutResultKeyResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(paginatedOperationWithoutResultKeyRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, @@ -646,9 +747,20 @@ public ReturnT putOperationWithChecksum(PutOperationWithChecksumReques HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, PutOperationWithChecksumResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(putOperationWithChecksumRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, putOperationWithChecksumRequest @@ -696,11 +808,11 @@ public ReturnT putOperationWithChecksum(PutOperationWithChecksumReques * The content to send to the service. A {@link RequestBody} can be created using one of several factory * methods for various sources of data. For example, to create a request body from a file you can do the * following. - * + * *
      * {@code RequestBody.fromFile(new File("myfile.txt"))}
      * 
- * + * * See documentation in {@link RequestBody} for additional details and which sources of data are supported. * The service documentation for the request content is as follows 'This be a stream' * @return Result of the StreamingInputOperation operation returned by the service. @@ -723,9 +835,20 @@ public StreamingInputOperationResponse streamingInputOperation(StreamingInputOpe HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, StreamingInputOperationResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(streamingInputOperationRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, streamingInputOperationRequest @@ -763,11 +886,11 @@ public StreamingInputOperationResponse streamingInputOperation(StreamingInputOpe * The content to send to the service. A {@link RequestBody} can be created using one of several factory * methods for various sources of data. For example, to create a request body from a file you can do the * following. - * + * *
      * {@code RequestBody.fromFile(new File("myfile.txt"))}
      * 
- * + * * See documentation in {@link RequestBody} for additional details and which sources of data are supported. * The service documentation for the request content is as follows 'This be a stream' * @param responseTransformer @@ -801,9 +924,20 @@ public ReturnT streamingInputOutputOperation( HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, StreamingInputOutputOperationResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(streamingInputOutputOperationRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, @@ -867,9 +1001,20 @@ public ReturnT streamingOutputOperation(StreamingOutputOperationReques HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, StreamingOutputOperationResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(streamingOutputOperationRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, streamingOutputOperationRequest @@ -932,8 +1077,8 @@ private static List resolveMetricPublishers(SdkClientConfigurat } private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata) { - return protocolFactory.createErrorResponseHandler(operationMetadata); + JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { + return protocolFactory.createErrorResponseHandler(operationMetadata, exceptionMetadataMapper); } private void updateRetryStrategyClientConfiguration(SdkClientConfiguration.Builder configuration) { @@ -974,14 +1119,8 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, } private > T init(T builder) { - return builder - .clientConfiguration(clientConfiguration) - .defaultServiceExceptionSupplier(JsonException::builder) - .protocol(AwsJsonProtocol.REST_JSON) - .protocolVersion("1.1") - .registerModeledException( - ExceptionMetadata.builder().errorCode("InvalidInput") - .exceptionBuilderSupplier(InvalidInputException::builder).httpStatusCode(400).build()); + return builder.clientConfiguration(clientConfiguration).defaultServiceExceptionSupplier(JsonException::builder) + .protocol(AwsJsonProtocol.REST_JSON).protocolVersion("1.1"); } @Override diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-rpcv2-async-client-class.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-rpcv2-async-client-class.java index f2530d02c873..3f49694dc45d 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-rpcv2-async-client-class.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-rpcv2-async-client-class.java @@ -94,7 +94,7 @@ final class DefaultSmithyRpcV2ProtocolAsyncClient implements SmithyRpcV2Protocol private static final Logger log = LoggerFactory.getLogger(DefaultSmithyRpcV2ProtocolAsyncClient.class); private static final AwsProtocolMetadata protocolMetadata = AwsProtocolMetadata.builder() - .serviceProtocol(AwsServiceProtocol.SMITHY_RPC_V2_CBOR).build(); + .serviceProtocol(AwsServiceProtocol.SMITHY_RPC_V2_CBOR).build(); private final AsyncClientHandler clientHandler; @@ -131,30 +131,47 @@ protected DefaultSmithyRpcV2ProtocolAsyncClient(SdkClientConfiguration clientCon @Override public CompletableFuture emptyInputOutput(EmptyInputOutputRequest emptyInputOutputRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(emptyInputOutputRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, emptyInputOutputRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "SmithyRpcV2Protocol"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "EmptyInputOutput"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, EmptyInputOutputResponse::builder); - + operationMetadata, EmptyInputOutputResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "ValidationException": + return Optional.of(ExceptionMetadata.builder().errorCode("ValidationException") + .exceptionBuilderSupplier(ValidationException::builder).build()); + case "InvalidGreeting": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidGreeting") + .exceptionBuilderSupplier(InvalidGreetingException::builder).build()); + case "ComplexError": + return Optional.of(ExceptionMetadata.builder().errorCode("ComplexError") + .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("EmptyInputOutput").withProtocolMetadata(protocolMetadata) - .withMarshaller(new EmptyInputOutputRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(emptyInputOutputRequest)); + .execute(new ClientExecutionParams() + .withOperationName("EmptyInputOutput").withProtocolMetadata(protocolMetadata) + .withMarshaller(new EmptyInputOutputRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(emptyInputOutputRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -190,27 +207,44 @@ public CompletableFuture emptyInputOutput(EmptyInputOu public CompletableFuture float16(Float16Request float16Request) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(float16Request, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, float16Request - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "SmithyRpcV2Protocol"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "Float16"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, - Float16Response::builder); - + Float16Response::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "ValidationException": + return Optional.of(ExceptionMetadata.builder().errorCode("ValidationException") + .exceptionBuilderSupplier(ValidationException::builder).build()); + case "InvalidGreeting": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidGreeting") + .exceptionBuilderSupplier(InvalidGreetingException::builder).build()); + case "ComplexError": + return Optional.of(ExceptionMetadata.builder().errorCode("ComplexError") + .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams().withOperationName("Float16") - .withProtocolMetadata(protocolMetadata).withMarshaller(new Float16RequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(float16Request)); + .execute(new ClientExecutionParams().withOperationName("Float16") + .withProtocolMetadata(protocolMetadata).withMarshaller(new Float16RequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(float16Request)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -245,30 +279,47 @@ public CompletableFuture float16(Float16Request float16Request) @Override public CompletableFuture fractionalSeconds(FractionalSecondsRequest fractionalSecondsRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(fractionalSecondsRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, fractionalSecondsRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "SmithyRpcV2Protocol"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "FractionalSeconds"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, FractionalSecondsResponse::builder); - + operationMetadata, FractionalSecondsResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "ValidationException": + return Optional.of(ExceptionMetadata.builder().errorCode("ValidationException") + .exceptionBuilderSupplier(ValidationException::builder).build()); + case "InvalidGreeting": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidGreeting") + .exceptionBuilderSupplier(InvalidGreetingException::builder).build()); + case "ComplexError": + return Optional.of(ExceptionMetadata.builder().errorCode("ComplexError") + .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("FractionalSeconds").withProtocolMetadata(protocolMetadata) - .withMarshaller(new FractionalSecondsRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(fractionalSecondsRequest)); + .execute(new ClientExecutionParams() + .withOperationName("FractionalSeconds").withProtocolMetadata(protocolMetadata) + .withMarshaller(new FractionalSecondsRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(fractionalSecondsRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -305,30 +356,47 @@ public CompletableFuture fractionalSeconds(Fractional @Override public CompletableFuture greetingWithErrors(GreetingWithErrorsRequest greetingWithErrorsRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(greetingWithErrorsRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, greetingWithErrorsRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "SmithyRpcV2Protocol"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "GreetingWithErrors"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, GreetingWithErrorsResponse::builder); - + operationMetadata, GreetingWithErrorsResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "ValidationException": + return Optional.of(ExceptionMetadata.builder().errorCode("ValidationException") + .exceptionBuilderSupplier(ValidationException::builder).build()); + case "InvalidGreeting": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidGreeting") + .exceptionBuilderSupplier(InvalidGreetingException::builder).build()); + case "ComplexError": + return Optional.of(ExceptionMetadata.builder().errorCode("ComplexError") + .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("GreetingWithErrors").withProtocolMetadata(protocolMetadata) - .withMarshaller(new GreetingWithErrorsRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(greetingWithErrorsRequest)); + .execute(new ClientExecutionParams() + .withOperationName("GreetingWithErrors").withProtocolMetadata(protocolMetadata) + .withMarshaller(new GreetingWithErrorsRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(greetingWithErrorsRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -364,28 +432,45 @@ public CompletableFuture greetingWithErrors(Greeting public CompletableFuture noInputOutput(NoInputOutputRequest noInputOutputRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(noInputOutputRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, noInputOutputRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "SmithyRpcV2Protocol"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "NoInputOutput"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, - NoInputOutputResponse::builder); - + NoInputOutputResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "ValidationException": + return Optional.of(ExceptionMetadata.builder().errorCode("ValidationException") + .exceptionBuilderSupplier(ValidationException::builder).build()); + case "InvalidGreeting": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidGreeting") + .exceptionBuilderSupplier(InvalidGreetingException::builder).build()); + case "ComplexError": + return Optional.of(ExceptionMetadata.builder().errorCode("ComplexError") + .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("NoInputOutput").withProtocolMetadata(protocolMetadata) - .withMarshaller(new NoInputOutputRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(noInputOutputRequest)); + .execute(new ClientExecutionParams() + .withOperationName("NoInputOutput").withProtocolMetadata(protocolMetadata) + .withMarshaller(new NoInputOutputRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(noInputOutputRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -420,32 +505,49 @@ public CompletableFuture noInputOutput(NoInputOutputReque */ @Override public CompletableFuture operationWithDefaults( - OperationWithDefaultsRequest operationWithDefaultsRequest) { + OperationWithDefaultsRequest operationWithDefaultsRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(operationWithDefaultsRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, operationWithDefaultsRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "SmithyRpcV2Protocol"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "OperationWithDefaults"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OperationWithDefaultsResponse::builder); - + operationMetadata, OperationWithDefaultsResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "ValidationException": + return Optional.of(ExceptionMetadata.builder().errorCode("ValidationException") + .exceptionBuilderSupplier(ValidationException::builder).build()); + case "InvalidGreeting": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidGreeting") + .exceptionBuilderSupplier(InvalidGreetingException::builder).build()); + case "ComplexError": + return Optional.of(ExceptionMetadata.builder().errorCode("ComplexError") + .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("OperationWithDefaults").withProtocolMetadata(protocolMetadata) - .withMarshaller(new OperationWithDefaultsRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(operationWithDefaultsRequest)); + .execute(new ClientExecutionParams() + .withOperationName("OperationWithDefaults").withProtocolMetadata(protocolMetadata) + .withMarshaller(new OperationWithDefaultsRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(operationWithDefaultsRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -479,32 +581,49 @@ public CompletableFuture operationWithDefaults( */ @Override public CompletableFuture optionalInputOutput( - OptionalInputOutputRequest optionalInputOutputRequest) { + OptionalInputOutputRequest optionalInputOutputRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(optionalInputOutputRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, optionalInputOutputRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "SmithyRpcV2Protocol"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "OptionalInputOutput"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OptionalInputOutputResponse::builder); - + operationMetadata, OptionalInputOutputResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "ValidationException": + return Optional.of(ExceptionMetadata.builder().errorCode("ValidationException") + .exceptionBuilderSupplier(ValidationException::builder).build()); + case "InvalidGreeting": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidGreeting") + .exceptionBuilderSupplier(InvalidGreetingException::builder).build()); + case "ComplexError": + return Optional.of(ExceptionMetadata.builder().errorCode("ComplexError") + .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("OptionalInputOutput").withProtocolMetadata(protocolMetadata) - .withMarshaller(new OptionalInputOutputRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(optionalInputOutputRequest)); + .execute(new ClientExecutionParams() + .withOperationName("OptionalInputOutput").withProtocolMetadata(protocolMetadata) + .withMarshaller(new OptionalInputOutputRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(optionalInputOutputRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -539,30 +658,47 @@ public CompletableFuture optionalInputOutput( @Override public CompletableFuture recursiveShapes(RecursiveShapesRequest recursiveShapesRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(recursiveShapesRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, recursiveShapesRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "SmithyRpcV2Protocol"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "RecursiveShapes"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, RecursiveShapesResponse::builder); - + operationMetadata, RecursiveShapesResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "ValidationException": + return Optional.of(ExceptionMetadata.builder().errorCode("ValidationException") + .exceptionBuilderSupplier(ValidationException::builder).build()); + case "InvalidGreeting": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidGreeting") + .exceptionBuilderSupplier(InvalidGreetingException::builder).build()); + case "ComplexError": + return Optional.of(ExceptionMetadata.builder().errorCode("ComplexError") + .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("RecursiveShapes").withProtocolMetadata(protocolMetadata) - .withMarshaller(new RecursiveShapesRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(recursiveShapesRequest)); + .execute(new ClientExecutionParams() + .withOperationName("RecursiveShapes").withProtocolMetadata(protocolMetadata) + .withMarshaller(new RecursiveShapesRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(recursiveShapesRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -598,30 +734,47 @@ public CompletableFuture recursiveShapes(RecursiveShape @Override public CompletableFuture rpcV2CborDenseMaps(RpcV2CborDenseMapsRequest rpcV2CborDenseMapsRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(rpcV2CborDenseMapsRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, rpcV2CborDenseMapsRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "SmithyRpcV2Protocol"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "RpcV2CborDenseMaps"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, RpcV2CborDenseMapsResponse::builder); - + operationMetadata, RpcV2CborDenseMapsResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "ValidationException": + return Optional.of(ExceptionMetadata.builder().errorCode("ValidationException") + .exceptionBuilderSupplier(ValidationException::builder).build()); + case "InvalidGreeting": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidGreeting") + .exceptionBuilderSupplier(InvalidGreetingException::builder).build()); + case "ComplexError": + return Optional.of(ExceptionMetadata.builder().errorCode("ComplexError") + .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("RpcV2CborDenseMaps").withProtocolMetadata(protocolMetadata) - .withMarshaller(new RpcV2CborDenseMapsRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(rpcV2CborDenseMapsRequest)); + .execute(new ClientExecutionParams() + .withOperationName("RpcV2CborDenseMaps").withProtocolMetadata(protocolMetadata) + .withMarshaller(new RpcV2CborDenseMapsRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(rpcV2CborDenseMapsRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -658,28 +811,45 @@ public CompletableFuture rpcV2CborDenseMaps(RpcV2Cbo public CompletableFuture rpcV2CborLists(RpcV2CborListsRequest rpcV2CborListsRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(rpcV2CborListsRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, rpcV2CborListsRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "SmithyRpcV2Protocol"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "RpcV2CborLists"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, RpcV2CborListsResponse::builder); - + operationMetadata, RpcV2CborListsResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "ValidationException": + return Optional.of(ExceptionMetadata.builder().errorCode("ValidationException") + .exceptionBuilderSupplier(ValidationException::builder).build()); + case "InvalidGreeting": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidGreeting") + .exceptionBuilderSupplier(InvalidGreetingException::builder).build()); + case "ComplexError": + return Optional.of(ExceptionMetadata.builder().errorCode("ComplexError") + .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("RpcV2CborLists").withProtocolMetadata(protocolMetadata) - .withMarshaller(new RpcV2CborListsRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(rpcV2CborListsRequest)); + .execute(new ClientExecutionParams() + .withOperationName("RpcV2CborLists").withProtocolMetadata(protocolMetadata) + .withMarshaller(new RpcV2CborListsRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(rpcV2CborListsRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -714,32 +884,49 @@ public CompletableFuture rpcV2CborLists(RpcV2CborListsRe */ @Override public CompletableFuture rpcV2CborSparseMaps( - RpcV2CborSparseMapsRequest rpcV2CborSparseMapsRequest) { + RpcV2CborSparseMapsRequest rpcV2CborSparseMapsRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(rpcV2CborSparseMapsRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, rpcV2CborSparseMapsRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "SmithyRpcV2Protocol"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "RpcV2CborSparseMaps"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, RpcV2CborSparseMapsResponse::builder); - + operationMetadata, RpcV2CborSparseMapsResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "ValidationException": + return Optional.of(ExceptionMetadata.builder().errorCode("ValidationException") + .exceptionBuilderSupplier(ValidationException::builder).build()); + case "InvalidGreeting": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidGreeting") + .exceptionBuilderSupplier(InvalidGreetingException::builder).build()); + case "ComplexError": + return Optional.of(ExceptionMetadata.builder().errorCode("ComplexError") + .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("RpcV2CborSparseMaps").withProtocolMetadata(protocolMetadata) - .withMarshaller(new RpcV2CborSparseMapsRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(rpcV2CborSparseMapsRequest)); + .execute(new ClientExecutionParams() + .withOperationName("RpcV2CborSparseMaps").withProtocolMetadata(protocolMetadata) + .withMarshaller(new RpcV2CborSparseMapsRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(rpcV2CborSparseMapsRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -773,32 +960,49 @@ public CompletableFuture rpcV2CborSparseMaps( */ @Override public CompletableFuture simpleScalarProperties( - SimpleScalarPropertiesRequest simpleScalarPropertiesRequest) { + SimpleScalarPropertiesRequest simpleScalarPropertiesRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(simpleScalarPropertiesRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, simpleScalarPropertiesRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "SmithyRpcV2Protocol"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "SimpleScalarProperties"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, SimpleScalarPropertiesResponse::builder); - + operationMetadata, SimpleScalarPropertiesResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "ValidationException": + return Optional.of(ExceptionMetadata.builder().errorCode("ValidationException") + .exceptionBuilderSupplier(ValidationException::builder).build()); + case "InvalidGreeting": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidGreeting") + .exceptionBuilderSupplier(InvalidGreetingException::builder).build()); + case "ComplexError": + return Optional.of(ExceptionMetadata.builder().errorCode("ComplexError") + .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("SimpleScalarProperties").withProtocolMetadata(protocolMetadata) - .withMarshaller(new SimpleScalarPropertiesRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(simpleScalarPropertiesRequest)); + .execute(new ClientExecutionParams() + .withOperationName("SimpleScalarProperties").withProtocolMetadata(protocolMetadata) + .withMarshaller(new SimpleScalarPropertiesRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(simpleScalarPropertiesRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -832,32 +1036,49 @@ public CompletableFuture simpleScalarProperties( */ @Override public CompletableFuture sparseNullsOperation( - SparseNullsOperationRequest sparseNullsOperationRequest) { + SparseNullsOperationRequest sparseNullsOperationRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(sparseNullsOperationRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, sparseNullsOperationRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "SmithyRpcV2Protocol"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "SparseNullsOperation"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, SparseNullsOperationResponse::builder); - + operationMetadata, SparseNullsOperationResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "ValidationException": + return Optional.of(ExceptionMetadata.builder().errorCode("ValidationException") + .exceptionBuilderSupplier(ValidationException::builder).build()); + case "InvalidGreeting": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidGreeting") + .exceptionBuilderSupplier(InvalidGreetingException::builder).build()); + case "ComplexError": + return Optional.of(ExceptionMetadata.builder().errorCode("ComplexError") + .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("SparseNullsOperation").withProtocolMetadata(protocolMetadata) - .withMarshaller(new SparseNullsOperationRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(sparseNullsOperationRequest)); + .execute(new ClientExecutionParams() + .withOperationName("SparseNullsOperation").withProtocolMetadata(protocolMetadata) + .withMarshaller(new SparseNullsOperationRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(sparseNullsOperationRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -880,24 +1101,13 @@ public final String serviceName() { } private > T init(T builder) { - return builder - .clientConfiguration(clientConfiguration) - .defaultServiceExceptionSupplier(SmithyRpcV2ProtocolException::builder) - .protocol(AwsJsonProtocol.SMITHY_RPC_V2_CBOR) - .protocolVersion("1.1") - .registerModeledException( - ExceptionMetadata.builder().errorCode("ValidationException") - .exceptionBuilderSupplier(ValidationException::builder).build()) - .registerModeledException( - ExceptionMetadata.builder().errorCode("InvalidGreeting") - .exceptionBuilderSupplier(InvalidGreetingException::builder).build()) - .registerModeledException( - ExceptionMetadata.builder().errorCode("ComplexError") - .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + return builder.clientConfiguration(clientConfiguration) + .defaultServiceExceptionSupplier(SmithyRpcV2ProtocolException::builder) + .protocol(AwsJsonProtocol.SMITHY_RPC_V2_CBOR).protocolVersion("1.1"); } private static List resolveMetricPublishers(SdkClientConfiguration clientConfiguration, - RequestOverrideConfiguration requestOverrideConfiguration) { + RequestOverrideConfiguration requestOverrideConfiguration) { List publishers = null; if (requestOverrideConfiguration != null) { publishers = requestOverrideConfiguration.metricPublishers(); @@ -941,7 +1151,7 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, return configuration.build(); } SmithyRpcV2ProtocolServiceClientConfigurationBuilder serviceConfigBuilder = new SmithyRpcV2ProtocolServiceClientConfigurationBuilder( - configuration); + configuration); for (SdkPlugin plugin : plugins) { plugin.configureClient(serviceConfigBuilder); } @@ -950,12 +1160,7 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, } private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata) { - return protocolFactory.createErrorResponseHandler(operationMetadata); - } - - private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { + JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { return protocolFactory.createErrorResponseHandler(operationMetadata, exceptionMetadataMapper); } diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-rpcv2-sync.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-rpcv2-sync.java index c02466218908..fe4cc2f7b0eb 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-rpcv2-sync.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-rpcv2-sync.java @@ -2,7 +2,9 @@ import java.util.Collections; import java.util.List; +import java.util.Optional; import java.util.function.Consumer; +import java.util.function.Function; import software.amazon.awssdk.annotations.Generated; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.awscore.client.handler.AwsSyncClientHandler; @@ -126,9 +128,26 @@ public EmptyInputOutputResponse emptyInputOutput(EmptyInputOutputRequest emptyIn HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, EmptyInputOutputResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "ValidationException": + return Optional.of(ExceptionMetadata.builder().errorCode("ValidationException") + .exceptionBuilderSupplier(ValidationException::builder).build()); + case "InvalidGreeting": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidGreeting") + .exceptionBuilderSupplier(InvalidGreetingException::builder).build()); + case "ComplexError": + return Optional.of(ExceptionMetadata.builder().errorCode("ComplexError") + .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(emptyInputOutputRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, emptyInputOutputRequest @@ -174,9 +193,26 @@ public Float16Response float16(Float16Request float16Request) throws AwsServiceE HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, Float16Response::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "ValidationException": + return Optional.of(ExceptionMetadata.builder().errorCode("ValidationException") + .exceptionBuilderSupplier(ValidationException::builder).build()); + case "InvalidGreeting": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidGreeting") + .exceptionBuilderSupplier(InvalidGreetingException::builder).build()); + case "ComplexError": + return Optional.of(ExceptionMetadata.builder().errorCode("ComplexError") + .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(float16Request, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, float16Request .overrideConfiguration().orElse(null)); @@ -220,9 +256,26 @@ public FractionalSecondsResponse fractionalSeconds(FractionalSecondsRequest frac HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, FractionalSecondsResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "ValidationException": + return Optional.of(ExceptionMetadata.builder().errorCode("ValidationException") + .exceptionBuilderSupplier(ValidationException::builder).build()); + case "InvalidGreeting": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidGreeting") + .exceptionBuilderSupplier(InvalidGreetingException::builder).build()); + case "ComplexError": + return Optional.of(ExceptionMetadata.builder().errorCode("ComplexError") + .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(fractionalSecondsRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, fractionalSecondsRequest @@ -271,9 +324,26 @@ public GreetingWithErrorsResponse greetingWithErrors(GreetingWithErrorsRequest g HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, GreetingWithErrorsResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "ValidationException": + return Optional.of(ExceptionMetadata.builder().errorCode("ValidationException") + .exceptionBuilderSupplier(ValidationException::builder).build()); + case "InvalidGreeting": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidGreeting") + .exceptionBuilderSupplier(InvalidGreetingException::builder).build()); + case "ComplexError": + return Optional.of(ExceptionMetadata.builder().errorCode("ComplexError") + .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(greetingWithErrorsRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, greetingWithErrorsRequest @@ -319,9 +389,26 @@ public NoInputOutputResponse noInputOutput(NoInputOutputRequest noInputOutputReq HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, NoInputOutputResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "ValidationException": + return Optional.of(ExceptionMetadata.builder().errorCode("ValidationException") + .exceptionBuilderSupplier(ValidationException::builder).build()); + case "InvalidGreeting": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidGreeting") + .exceptionBuilderSupplier(InvalidGreetingException::builder).build()); + case "ComplexError": + return Optional.of(ExceptionMetadata.builder().errorCode("ComplexError") + .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(noInputOutputRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, noInputOutputRequest .overrideConfiguration().orElse(null)); @@ -367,9 +454,26 @@ public OperationWithDefaultsResponse operationWithDefaults(OperationWithDefaults HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, OperationWithDefaultsResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "ValidationException": + return Optional.of(ExceptionMetadata.builder().errorCode("ValidationException") + .exceptionBuilderSupplier(ValidationException::builder).build()); + case "InvalidGreeting": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidGreeting") + .exceptionBuilderSupplier(InvalidGreetingException::builder).build()); + case "ComplexError": + return Optional.of(ExceptionMetadata.builder().errorCode("ComplexError") + .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(operationWithDefaultsRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, operationWithDefaultsRequest @@ -415,9 +519,26 @@ public OptionalInputOutputResponse optionalInputOutput(OptionalInputOutputReques HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, OptionalInputOutputResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "ValidationException": + return Optional.of(ExceptionMetadata.builder().errorCode("ValidationException") + .exceptionBuilderSupplier(ValidationException::builder).build()); + case "InvalidGreeting": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidGreeting") + .exceptionBuilderSupplier(InvalidGreetingException::builder).build()); + case "ComplexError": + return Optional.of(ExceptionMetadata.builder().errorCode("ComplexError") + .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(optionalInputOutputRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, optionalInputOutputRequest @@ -463,9 +584,26 @@ public RecursiveShapesResponse recursiveShapes(RecursiveShapesRequest recursiveS HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, RecursiveShapesResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "ValidationException": + return Optional.of(ExceptionMetadata.builder().errorCode("ValidationException") + .exceptionBuilderSupplier(ValidationException::builder).build()); + case "InvalidGreeting": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidGreeting") + .exceptionBuilderSupplier(InvalidGreetingException::builder).build()); + case "ComplexError": + return Optional.of(ExceptionMetadata.builder().errorCode("ComplexError") + .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(recursiveShapesRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, recursiveShapesRequest @@ -512,9 +650,26 @@ public RpcV2CborDenseMapsResponse rpcV2CborDenseMaps(RpcV2CborDenseMapsRequest r HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, RpcV2CborDenseMapsResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "ValidationException": + return Optional.of(ExceptionMetadata.builder().errorCode("ValidationException") + .exceptionBuilderSupplier(ValidationException::builder).build()); + case "InvalidGreeting": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidGreeting") + .exceptionBuilderSupplier(InvalidGreetingException::builder).build()); + case "ComplexError": + return Optional.of(ExceptionMetadata.builder().errorCode("ComplexError") + .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(rpcV2CborDenseMapsRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, rpcV2CborDenseMapsRequest @@ -561,9 +716,26 @@ public RpcV2CborListsResponse rpcV2CborLists(RpcV2CborListsRequest rpcV2CborList HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, RpcV2CborListsResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "ValidationException": + return Optional.of(ExceptionMetadata.builder().errorCode("ValidationException") + .exceptionBuilderSupplier(ValidationException::builder).build()); + case "InvalidGreeting": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidGreeting") + .exceptionBuilderSupplier(InvalidGreetingException::builder).build()); + case "ComplexError": + return Optional.of(ExceptionMetadata.builder().errorCode("ComplexError") + .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(rpcV2CborListsRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, rpcV2CborListsRequest .overrideConfiguration().orElse(null)); @@ -609,9 +781,26 @@ public RpcV2CborSparseMapsResponse rpcV2CborSparseMaps(RpcV2CborSparseMapsReques HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, RpcV2CborSparseMapsResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "ValidationException": + return Optional.of(ExceptionMetadata.builder().errorCode("ValidationException") + .exceptionBuilderSupplier(ValidationException::builder).build()); + case "InvalidGreeting": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidGreeting") + .exceptionBuilderSupplier(InvalidGreetingException::builder).build()); + case "ComplexError": + return Optional.of(ExceptionMetadata.builder().errorCode("ComplexError") + .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(rpcV2CborSparseMapsRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, rpcV2CborSparseMapsRequest @@ -657,9 +846,26 @@ public SimpleScalarPropertiesResponse simpleScalarProperties(SimpleScalarPropert HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, SimpleScalarPropertiesResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "ValidationException": + return Optional.of(ExceptionMetadata.builder().errorCode("ValidationException") + .exceptionBuilderSupplier(ValidationException::builder).build()); + case "InvalidGreeting": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidGreeting") + .exceptionBuilderSupplier(InvalidGreetingException::builder).build()); + case "ComplexError": + return Optional.of(ExceptionMetadata.builder().errorCode("ComplexError") + .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(simpleScalarPropertiesRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, simpleScalarPropertiesRequest @@ -706,9 +912,26 @@ public SparseNullsOperationResponse sparseNullsOperation(SparseNullsOperationReq HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, SparseNullsOperationResponse::builder); - + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "ValidationException": + return Optional.of(ExceptionMetadata.builder().errorCode("ValidationException") + .exceptionBuilderSupplier(ValidationException::builder).build()); + case "InvalidGreeting": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidGreeting") + .exceptionBuilderSupplier(InvalidGreetingException::builder).build()); + case "ComplexError": + return Optional.of(ExceptionMetadata.builder().errorCode("ComplexError") + .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(sparseNullsOperationRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, sparseNullsOperationRequest @@ -751,8 +974,8 @@ private static List resolveMetricPublishers(SdkClientConfigurat } private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata) { - return protocolFactory.createErrorResponseHandler(operationMetadata); + JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { + return protocolFactory.createErrorResponseHandler(operationMetadata, exceptionMetadataMapper); } private void updateRetryStrategyClientConfiguration(SdkClientConfiguration.Builder configuration) { @@ -794,20 +1017,9 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, } private > T init(T builder) { - return builder - .clientConfiguration(clientConfiguration) + return builder.clientConfiguration(clientConfiguration) .defaultServiceExceptionSupplier(SmithyRpcV2ProtocolException::builder) - .protocol(AwsJsonProtocol.SMITHY_RPC_V2_CBOR) - .protocolVersion("1.1") - .registerModeledException( - ExceptionMetadata.builder().errorCode("ValidationException") - .exceptionBuilderSupplier(ValidationException::builder).build()) - .registerModeledException( - ExceptionMetadata.builder().errorCode("InvalidGreeting") - .exceptionBuilderSupplier(InvalidGreetingException::builder).build()) - .registerModeledException( - ExceptionMetadata.builder().errorCode("ComplexError") - .exceptionBuilderSupplier(ComplexErrorException::builder).build()); + .protocol(AwsJsonProtocol.SMITHY_RPC_V2_CBOR).protocolVersion("1.1"); } @Override diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-unsigned-payload-trait-async-client-class.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-unsigned-payload-trait-async-client-class.java index 4a8589dead07..81f2c6ffd395 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-unsigned-payload-trait-async-client-class.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-unsigned-payload-trait-async-client-class.java @@ -92,7 +92,7 @@ final class DefaultDatabaseAsyncClient implements DatabaseAsyncClient { private static final Logger log = LoggerFactory.getLogger(DefaultDatabaseAsyncClient.class); private static final AwsProtocolMetadata protocolMetadata = AwsProtocolMetadata.builder() - .serviceProtocol(AwsServiceProtocol.REST_JSON).build(); + .serviceProtocol(AwsServiceProtocol.REST_JSON).build(); private final AsyncClientHandler clientHandler; @@ -134,27 +134,38 @@ protected DefaultDatabaseAsyncClient(SdkClientConfiguration clientConfiguration) public CompletableFuture deleteRow(DeleteRowRequest deleteRowRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(deleteRowRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, deleteRowRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Database Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "DeleteRow"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, - DeleteRowResponse::builder); - + DeleteRowResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams().withOperationName("DeleteRow") - .withProtocolMetadata(protocolMetadata) - .withMarshaller(new DeleteRowRequestMarshaller(protocolFactory)).withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector).withInput(deleteRowRequest)); + .execute(new ClientExecutionParams().withOperationName("DeleteRow") + .withProtocolMetadata(protocolMetadata) + .withMarshaller(new DeleteRowRequestMarshaller(protocolFactory)).withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) + .withMetricCollector(apiCallMetricCollector).withInput(deleteRowRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -194,27 +205,38 @@ public CompletableFuture deleteRow(DeleteRowRequest deleteRow public CompletableFuture getRow(GetRowRequest getRowRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(getRowRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, getRowRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Database Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "GetRow"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, - GetRowResponse::builder); - + GetRowResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams().withOperationName("GetRow") - .withProtocolMetadata(protocolMetadata).withMarshaller(new GetRowRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(getRowRequest)); + .execute(new ClientExecutionParams().withOperationName("GetRow") + .withProtocolMetadata(protocolMetadata).withMarshaller(new GetRowRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(getRowRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -254,32 +276,43 @@ public CompletableFuture getRow(GetRowRequest getRowRequest) { */ @Override public CompletableFuture opWithSigv4AndSigv4aUnSignedPayload( - OpWithSigv4AndSigv4AUnSignedPayloadRequest opWithSigv4AndSigv4AUnSignedPayloadRequest) { + OpWithSigv4AndSigv4AUnSignedPayloadRequest opWithSigv4AndSigv4AUnSignedPayloadRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(opWithSigv4AndSigv4AUnSignedPayloadRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - opWithSigv4AndSigv4AUnSignedPayloadRequest.overrideConfiguration().orElse(null)); + opWithSigv4AndSigv4AUnSignedPayloadRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Database Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "opWithSigv4AndSigv4aUnSignedPayload"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory - .createResponseHandler(operationMetadata, OpWithSigv4AndSigv4AUnSignedPayloadResponse::builder); - + .createResponseHandler(operationMetadata, OpWithSigv4AndSigv4AUnSignedPayloadResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("opWithSigv4AndSigv4aUnSignedPayload").withProtocolMetadata(protocolMetadata) - .withMarshaller(new OpWithSigv4AndSigv4AUnSignedPayloadRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(opWithSigv4AndSigv4AUnSignedPayloadRequest)); + .execute(new ClientExecutionParams() + .withOperationName("opWithSigv4AndSigv4aUnSignedPayload").withProtocolMetadata(protocolMetadata) + .withMarshaller(new OpWithSigv4AndSigv4AUnSignedPayloadRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(opWithSigv4AndSigv4AUnSignedPayloadRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -317,32 +350,43 @@ public CompletableFuture opWithSigv */ @Override public CompletableFuture opWithSigv4SignedPayload( - OpWithSigv4SignedPayloadRequest opWithSigv4SignedPayloadRequest) { + OpWithSigv4SignedPayloadRequest opWithSigv4SignedPayloadRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(opWithSigv4SignedPayloadRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, opWithSigv4SignedPayloadRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Database Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "opWithSigv4SignedPayload"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OpWithSigv4SignedPayloadResponse::builder); - + operationMetadata, OpWithSigv4SignedPayloadResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("opWithSigv4SignedPayload").withProtocolMetadata(protocolMetadata) - .withMarshaller(new OpWithSigv4SignedPayloadRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(opWithSigv4SignedPayloadRequest)); + .execute(new ClientExecutionParams() + .withOperationName("opWithSigv4SignedPayload").withProtocolMetadata(protocolMetadata) + .withMarshaller(new OpWithSigv4SignedPayloadRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(opWithSigv4SignedPayloadRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -380,32 +424,43 @@ public CompletableFuture opWithSigv4SignedPayl */ @Override public CompletableFuture opWithSigv4UnSignedPayload( - OpWithSigv4UnSignedPayloadRequest opWithSigv4UnSignedPayloadRequest) { + OpWithSigv4UnSignedPayloadRequest opWithSigv4UnSignedPayloadRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(opWithSigv4UnSignedPayloadRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, opWithSigv4UnSignedPayloadRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Database Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "opWithSigv4UnSignedPayload"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OpWithSigv4UnSignedPayloadResponse::builder); - + operationMetadata, OpWithSigv4UnSignedPayloadResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("opWithSigv4UnSignedPayload").withProtocolMetadata(protocolMetadata) - .withMarshaller(new OpWithSigv4UnSignedPayloadRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(opWithSigv4UnSignedPayloadRequest)); + .execute(new ClientExecutionParams() + .withOperationName("opWithSigv4UnSignedPayload").withProtocolMetadata(protocolMetadata) + .withMarshaller(new OpWithSigv4UnSignedPayloadRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(opWithSigv4UnSignedPayloadRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -450,47 +505,58 @@ public CompletableFuture opWithSigv4UnSigned */ @Override public CompletableFuture opWithSigv4UnSignedPayloadAndStreaming( - OpWithSigv4UnSignedPayloadAndStreamingRequest opWithSigv4UnSignedPayloadAndStreamingRequest, - AsyncRequestBody requestBody) { + OpWithSigv4UnSignedPayloadAndStreamingRequest opWithSigv4UnSignedPayloadAndStreamingRequest, + AsyncRequestBody requestBody) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(opWithSigv4UnSignedPayloadAndStreamingRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - opWithSigv4UnSignedPayloadAndStreamingRequest.overrideConfiguration().orElse(null)); + opWithSigv4UnSignedPayloadAndStreamingRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Database Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "opWithSigv4UnSignedPayloadAndStreaming"); if (!isSignerOverridden(clientConfiguration)) { opWithSigv4UnSignedPayloadAndStreamingRequest = applySignerOverride( - opWithSigv4UnSignedPayloadAndStreamingRequest, AsyncAws4Signer.create()); + opWithSigv4UnSignedPayloadAndStreamingRequest, AsyncAws4Signer.create()); } JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory - .createResponseHandler(operationMetadata, OpWithSigv4UnSignedPayloadAndStreamingResponse::builder); - + .createResponseHandler(operationMetadata, OpWithSigv4UnSignedPayloadAndStreamingResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("opWithSigv4UnSignedPayloadAndStreaming") - .withProtocolMetadata(protocolMetadata) - .withMarshaller( - AsyncStreamingRequestMarshaller - .builder() - .delegateMarshaller( - new OpWithSigv4UnSignedPayloadAndStreamingRequestMarshaller(protocolFactory)) - .asyncRequestBody(requestBody).transferEncoding(true).build()).withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) - .withMetricCollector(apiCallMetricCollector).withAsyncRequestBody(requestBody) - .withInput(opWithSigv4UnSignedPayloadAndStreamingRequest)); + .execute(new ClientExecutionParams() + .withOperationName("opWithSigv4UnSignedPayloadAndStreaming") + .withProtocolMetadata(protocolMetadata) + .withMarshaller( + AsyncStreamingRequestMarshaller + .builder() + .delegateMarshaller( + new OpWithSigv4UnSignedPayloadAndStreamingRequestMarshaller(protocolFactory)) + .asyncRequestBody(requestBody).transferEncoding(true).build()) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withAsyncRequestBody(requestBody).withInput(opWithSigv4UnSignedPayloadAndStreamingRequest)); CompletableFuture whenCompleted = executeFuture - .whenComplete((r, e) -> { - metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); - }); + .whenComplete((r, e) -> { + metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); + }); executeFuture = CompletableFutureUtils.forwardExceptionTo(whenCompleted, executeFuture); return executeFuture; } catch (Throwable t) { @@ -525,32 +591,43 @@ public CompletableFuture opWithS */ @Override public CompletableFuture opWithSigv4aSignedPayload( - OpWithSigv4ASignedPayloadRequest opWithSigv4ASignedPayloadRequest) { + OpWithSigv4ASignedPayloadRequest opWithSigv4ASignedPayloadRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(opWithSigv4ASignedPayloadRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, opWithSigv4ASignedPayloadRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Database Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "opWithSigv4aSignedPayload"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OpWithSigv4ASignedPayloadResponse::builder); - + operationMetadata, OpWithSigv4ASignedPayloadResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("opWithSigv4aSignedPayload").withProtocolMetadata(protocolMetadata) - .withMarshaller(new OpWithSigv4ASignedPayloadRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(opWithSigv4ASignedPayloadRequest)); + .execute(new ClientExecutionParams() + .withOperationName("opWithSigv4aSignedPayload").withProtocolMetadata(protocolMetadata) + .withMarshaller(new OpWithSigv4ASignedPayloadRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(opWithSigv4ASignedPayloadRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -588,32 +665,43 @@ public CompletableFuture opWithSigv4aSignedPa */ @Override public CompletableFuture opWithSigv4aUnSignedPayload( - OpWithSigv4AUnSignedPayloadRequest opWithSigv4AUnSignedPayloadRequest) { + OpWithSigv4AUnSignedPayloadRequest opWithSigv4AUnSignedPayloadRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(opWithSigv4AUnSignedPayloadRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, opWithSigv4AUnSignedPayloadRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Database Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "opWithSigv4aUnSignedPayload"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OpWithSigv4AUnSignedPayloadResponse::builder); - + operationMetadata, OpWithSigv4AUnSignedPayloadResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("opWithSigv4aUnSignedPayload").withProtocolMetadata(protocolMetadata) - .withMarshaller(new OpWithSigv4AUnSignedPayloadRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(opWithSigv4AUnSignedPayloadRequest)); + .execute(new ClientExecutionParams() + .withOperationName("opWithSigv4aUnSignedPayload").withProtocolMetadata(protocolMetadata) + .withMarshaller(new OpWithSigv4AUnSignedPayloadRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(opWithSigv4AUnSignedPayloadRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -653,32 +741,43 @@ public CompletableFuture opWithSigv4aUnSign */ @Override public CompletableFuture opsWithSigv4andSigv4aSignedPayload( - OpsWithSigv4AndSigv4ASignedPayloadRequest opsWithSigv4AndSigv4ASignedPayloadRequest) { + OpsWithSigv4AndSigv4ASignedPayloadRequest opsWithSigv4AndSigv4ASignedPayloadRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(opsWithSigv4AndSigv4ASignedPayloadRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - opsWithSigv4AndSigv4ASignedPayloadRequest.overrideConfiguration().orElse(null)); + opsWithSigv4AndSigv4ASignedPayloadRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Database Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "opsWithSigv4andSigv4aSignedPayload"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory - .createResponseHandler(operationMetadata, OpsWithSigv4AndSigv4ASignedPayloadResponse::builder); - + .createResponseHandler(operationMetadata, OpsWithSigv4AndSigv4ASignedPayloadResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("opsWithSigv4andSigv4aSignedPayload").withProtocolMetadata(protocolMetadata) - .withMarshaller(new OpsWithSigv4AndSigv4ASignedPayloadRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(opsWithSigv4AndSigv4ASignedPayloadRequest)); + .execute(new ClientExecutionParams() + .withOperationName("opsWithSigv4andSigv4aSignedPayload").withProtocolMetadata(protocolMetadata) + .withMarshaller(new OpsWithSigv4AndSigv4ASignedPayloadRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(opsWithSigv4AndSigv4ASignedPayloadRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -718,27 +817,38 @@ public CompletableFuture opsWithSigv public CompletableFuture putRow(PutRowRequest putRowRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(putRowRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, putRowRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Database Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "PutRow"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, - PutRowResponse::builder); - + PutRowResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams().withOperationName("PutRow") - .withProtocolMetadata(protocolMetadata).withMarshaller(new PutRowRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(putRowRequest)); + .execute(new ClientExecutionParams().withOperationName("PutRow") + .withProtocolMetadata(protocolMetadata).withMarshaller(new PutRowRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(putRowRequest)); CompletableFuture whenCompleted = executeFuture.whenComplete((r, e) -> { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); }); @@ -778,36 +888,47 @@ public CompletableFuture putRow(PutRowRequest putRowRequest) { */ @Override public CompletableFuture secondOpsWithSigv4andSigv4aSignedPayload( - SecondOpsWithSigv4AndSigv4ASignedPayloadRequest secondOpsWithSigv4AndSigv4ASignedPayloadRequest) { + SecondOpsWithSigv4AndSigv4ASignedPayloadRequest secondOpsWithSigv4AndSigv4ASignedPayloadRequest) { SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration( - secondOpsWithSigv4AndSigv4ASignedPayloadRequest, this.clientConfiguration); + secondOpsWithSigv4AndSigv4ASignedPayloadRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - secondOpsWithSigv4AndSigv4ASignedPayloadRequest.overrideConfiguration().orElse(null)); + secondOpsWithSigv4AndSigv4ASignedPayloadRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Database Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "secondOpsWithSigv4andSigv4aSignedPayload"); JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory - .createResponseHandler(operationMetadata, SecondOpsWithSigv4AndSigv4ASignedPayloadResponse::builder); - + .createResponseHandler(operationMetadata, SecondOpsWithSigv4AndSigv4ASignedPayloadResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); CompletableFuture executeFuture = clientHandler - .execute(new ClientExecutionParams() - .withOperationName("secondOpsWithSigv4andSigv4aSignedPayload").withProtocolMetadata(protocolMetadata) - .withMarshaller(new SecondOpsWithSigv4AndSigv4ASignedPayloadRequestMarshaller(protocolFactory)) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) - .withInput(secondOpsWithSigv4AndSigv4ASignedPayloadRequest)); + .execute(new ClientExecutionParams() + .withOperationName("secondOpsWithSigv4andSigv4aSignedPayload").withProtocolMetadata(protocolMetadata) + .withMarshaller(new SecondOpsWithSigv4AndSigv4ASignedPayloadRequestMarshaller(protocolFactory)) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withMetricCollector(apiCallMetricCollector) + .withInput(secondOpsWithSigv4AndSigv4ASignedPayloadRequest)); CompletableFuture whenCompleted = executeFuture - .whenComplete((r, e) -> { - metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); - }); + .whenComplete((r, e) -> { + metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); + }); executeFuture = CompletableFutureUtils.forwardExceptionTo(whenCompleted, executeFuture); return executeFuture; } catch (Throwable t) { @@ -827,18 +948,12 @@ public final String serviceName() { } private > T init(T builder) { - return builder - .clientConfiguration(clientConfiguration) - .defaultServiceExceptionSupplier(DatabaseException::builder) - .protocol(AwsJsonProtocol.REST_JSON) - .protocolVersion("1.1") - .registerModeledException( - ExceptionMetadata.builder().errorCode("InvalidInput") - .exceptionBuilderSupplier(InvalidInputException::builder).httpStatusCode(400).build()); + return builder.clientConfiguration(clientConfiguration).defaultServiceExceptionSupplier(DatabaseException::builder) + .protocol(AwsJsonProtocol.REST_JSON).protocolVersion("1.1"); } private static List resolveMetricPublishers(SdkClientConfiguration clientConfiguration, - RequestOverrideConfiguration requestOverrideConfiguration) { + RequestOverrideConfiguration requestOverrideConfiguration) { List publishers = null; if (requestOverrideConfiguration != null) { publishers = requestOverrideConfiguration.metricPublishers(); @@ -858,8 +973,8 @@ private T applySignerOverride(T request, Signer sign } Consumer signerOverride = b -> b.signer(signer).build(); AwsRequestOverrideConfiguration overrideConfiguration = request.overrideConfiguration() - .map(c -> c.toBuilder().applyMutation(signerOverride).build()) - .orElse((AwsRequestOverrideConfiguration.builder().applyMutation(signerOverride).build())); + .map(c -> c.toBuilder().applyMutation(signerOverride).build()) + .orElse((AwsRequestOverrideConfiguration.builder().applyMutation(signerOverride).build())); return (T) request.toBuilder().overrideConfiguration(overrideConfiguration).build(); } @@ -897,7 +1012,7 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, return configuration.build(); } DatabaseServiceClientConfigurationBuilder serviceConfigBuilder = new DatabaseServiceClientConfigurationBuilder( - configuration); + configuration); for (SdkPlugin plugin : plugins) { plugin.configureClient(serviceConfigBuilder); } @@ -906,12 +1021,7 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, } private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata) { - return protocolFactory.createErrorResponseHandler(operationMetadata); - } - - private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { + JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { return protocolFactory.createErrorResponseHandler(operationMetadata, exceptionMetadataMapper); } diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-unsigned-payload-trait-sync-client-class.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-unsigned-payload-trait-sync-client-class.java index 2fa866188c5f..656846b6a453 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-unsigned-payload-trait-sync-client-class.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-unsigned-payload-trait-sync-client-class.java @@ -2,7 +2,9 @@ import java.util.Collections; import java.util.List; +import java.util.Optional; import java.util.function.Consumer; +import java.util.function.Function; import software.amazon.awssdk.annotations.Generated; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.awscore.client.handler.AwsSyncClientHandler; @@ -82,7 +84,7 @@ final class DefaultDatabaseClient implements DatabaseClient { private static final Logger log = Logger.loggerFor(DefaultDatabaseClient.class); private static final AwsProtocolMetadata protocolMetadata = AwsProtocolMetadata.builder() - .serviceProtocol(AwsServiceProtocol.REST_JSON).build(); + .serviceProtocol(AwsServiceProtocol.REST_JSON).build(); private final SyncClientHandler clientHandler; @@ -118,29 +120,40 @@ protected DefaultDatabaseClient(SdkClientConfiguration clientConfiguration) { */ @Override public DeleteRowResponse deleteRow(DeleteRowRequest deleteRowRequest) throws InvalidInputException, AwsServiceException, - SdkClientException, DatabaseException { + SdkClientException, DatabaseException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, - DeleteRowResponse::builder); - + DeleteRowResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(deleteRowRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, deleteRowRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Database Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "DeleteRow"); return clientHandler.execute(new ClientExecutionParams() - .withOperationName("DeleteRow").withProtocolMetadata(protocolMetadata).withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) - .withInput(deleteRowRequest).withMetricCollector(apiCallMetricCollector) - .withMarshaller(new DeleteRowRequestMarshaller(protocolFactory))); + .withOperationName("DeleteRow").withProtocolMetadata(protocolMetadata).withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) + .withInput(deleteRowRequest).withMetricCollector(apiCallMetricCollector) + .withMarshaller(new DeleteRowRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } @@ -168,29 +181,40 @@ public DeleteRowResponse deleteRow(DeleteRowRequest deleteRowRequest) throws Inv */ @Override public GetRowResponse getRow(GetRowRequest getRowRequest) throws InvalidInputException, AwsServiceException, - SdkClientException, DatabaseException { + SdkClientException, DatabaseException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, - GetRowResponse::builder); - + GetRowResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(getRowRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, getRowRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Database Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "GetRow"); return clientHandler.execute(new ClientExecutionParams().withOperationName("GetRow") - .withProtocolMetadata(protocolMetadata).withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) - .withInput(getRowRequest).withMetricCollector(apiCallMetricCollector) - .withMarshaller(new GetRowRequestMarshaller(protocolFactory))); + .withProtocolMetadata(protocolMetadata).withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) + .withInput(getRowRequest).withMetricCollector(apiCallMetricCollector) + .withMarshaller(new GetRowRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } @@ -218,29 +242,40 @@ public GetRowResponse getRow(GetRowRequest getRowRequest) throws InvalidInputExc */ @Override public PutRowResponse putRow(PutRowRequest putRowRequest) throws InvalidInputException, AwsServiceException, - SdkClientException, DatabaseException { + SdkClientException, DatabaseException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, - PutRowResponse::builder); - + PutRowResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(putRowRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, putRowRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Database Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "PutRow"); return clientHandler.execute(new ClientExecutionParams().withOperationName("PutRow") - .withProtocolMetadata(protocolMetadata).withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) - .withInput(putRowRequest).withMetricCollector(apiCallMetricCollector) - .withMarshaller(new PutRowRequestMarshaller(protocolFactory))); + .withProtocolMetadata(protocolMetadata).withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) + .withInput(putRowRequest).withMetricCollector(apiCallMetricCollector) + .withMarshaller(new PutRowRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } @@ -269,33 +304,44 @@ public PutRowResponse putRow(PutRowRequest putRowRequest) throws InvalidInputExc */ @Override public OpWithSigv4AndSigv4AUnSignedPayloadResponse opWithSigv4AndSigv4aUnSignedPayload( - OpWithSigv4AndSigv4AUnSignedPayloadRequest opWithSigv4AndSigv4AUnSignedPayloadRequest) throws InvalidInputException, - AwsServiceException, SdkClientException, DatabaseException { + OpWithSigv4AndSigv4AUnSignedPayloadRequest opWithSigv4AndSigv4AUnSignedPayloadRequest) throws InvalidInputException, + AwsServiceException, SdkClientException, DatabaseException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OpWithSigv4AndSigv4AUnSignedPayloadResponse::builder); - + operationMetadata, OpWithSigv4AndSigv4AUnSignedPayloadResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(opWithSigv4AndSigv4AUnSignedPayloadRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - opWithSigv4AndSigv4AUnSignedPayloadRequest.overrideConfiguration().orElse(null)); + opWithSigv4AndSigv4AUnSignedPayloadRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Database Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "opWithSigv4AndSigv4aUnSignedPayload"); return clientHandler - .execute(new ClientExecutionParams() - .withOperationName("opWithSigv4AndSigv4aUnSignedPayload").withProtocolMetadata(protocolMetadata) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withInput(opWithSigv4AndSigv4AUnSignedPayloadRequest) - .withMetricCollector(apiCallMetricCollector) - .withMarshaller(new OpWithSigv4AndSigv4AUnSignedPayloadRequestMarshaller(protocolFactory))); + .execute(new ClientExecutionParams() + .withOperationName("opWithSigv4AndSigv4aUnSignedPayload").withProtocolMetadata(protocolMetadata) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withInput(opWithSigv4AndSigv4AUnSignedPayloadRequest) + .withMetricCollector(apiCallMetricCollector) + .withMarshaller(new OpWithSigv4AndSigv4AUnSignedPayloadRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } @@ -323,33 +369,44 @@ public OpWithSigv4AndSigv4AUnSignedPayloadResponse opWithSigv4AndSigv4aUnSignedP */ @Override public OpWithSigv4SignedPayloadResponse opWithSigv4SignedPayload( - OpWithSigv4SignedPayloadRequest opWithSigv4SignedPayloadRequest) throws InvalidInputException, AwsServiceException, - SdkClientException, DatabaseException { + OpWithSigv4SignedPayloadRequest opWithSigv4SignedPayloadRequest) throws InvalidInputException, AwsServiceException, + SdkClientException, DatabaseException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OpWithSigv4SignedPayloadResponse::builder); - + operationMetadata, OpWithSigv4SignedPayloadResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(opWithSigv4SignedPayloadRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, opWithSigv4SignedPayloadRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Database Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "opWithSigv4SignedPayload"); return clientHandler - .execute(new ClientExecutionParams() - .withOperationName("opWithSigv4SignedPayload").withProtocolMetadata(protocolMetadata) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withInput(opWithSigv4SignedPayloadRequest) - .withMetricCollector(apiCallMetricCollector) - .withMarshaller(new OpWithSigv4SignedPayloadRequestMarshaller(protocolFactory))); + .execute(new ClientExecutionParams() + .withOperationName("opWithSigv4SignedPayload").withProtocolMetadata(protocolMetadata) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withInput(opWithSigv4SignedPayloadRequest) + .withMetricCollector(apiCallMetricCollector) + .withMarshaller(new OpWithSigv4SignedPayloadRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } @@ -377,33 +434,44 @@ public OpWithSigv4SignedPayloadResponse opWithSigv4SignedPayload( */ @Override public OpWithSigv4UnSignedPayloadResponse opWithSigv4UnSignedPayload( - OpWithSigv4UnSignedPayloadRequest opWithSigv4UnSignedPayloadRequest) throws InvalidInputException, - AwsServiceException, SdkClientException, DatabaseException { + OpWithSigv4UnSignedPayloadRequest opWithSigv4UnSignedPayloadRequest) throws InvalidInputException, + AwsServiceException, SdkClientException, DatabaseException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OpWithSigv4UnSignedPayloadResponse::builder); - + operationMetadata, OpWithSigv4UnSignedPayloadResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(opWithSigv4UnSignedPayloadRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, opWithSigv4UnSignedPayloadRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Database Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "opWithSigv4UnSignedPayload"); return clientHandler - .execute(new ClientExecutionParams() - .withOperationName("opWithSigv4UnSignedPayload").withProtocolMetadata(protocolMetadata) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withInput(opWithSigv4UnSignedPayloadRequest) - .withMetricCollector(apiCallMetricCollector) - .withMarshaller(new OpWithSigv4UnSignedPayloadRequestMarshaller(protocolFactory))); + .execute(new ClientExecutionParams() + .withOperationName("opWithSigv4UnSignedPayload").withProtocolMetadata(protocolMetadata) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withInput(opWithSigv4UnSignedPayloadRequest) + .withMetricCollector(apiCallMetricCollector) + .withMarshaller(new OpWithSigv4UnSignedPayloadRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } @@ -443,42 +511,53 @@ public OpWithSigv4UnSignedPayloadResponse opWithSigv4UnSignedPayload( */ @Override public OpWithSigv4UnSignedPayloadAndStreamingResponse opWithSigv4UnSignedPayloadAndStreaming( - OpWithSigv4UnSignedPayloadAndStreamingRequest opWithSigv4UnSignedPayloadAndStreamingRequest, RequestBody requestBody) - throws InvalidInputException, AwsServiceException, SdkClientException, DatabaseException { + OpWithSigv4UnSignedPayloadAndStreamingRequest opWithSigv4UnSignedPayloadAndStreamingRequest, RequestBody requestBody) + throws InvalidInputException, AwsServiceException, SdkClientException, DatabaseException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory - .createResponseHandler(operationMetadata, OpWithSigv4UnSignedPayloadAndStreamingResponse::builder); - + .createResponseHandler(operationMetadata, OpWithSigv4UnSignedPayloadAndStreamingResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(opWithSigv4UnSignedPayloadAndStreamingRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - opWithSigv4UnSignedPayloadAndStreamingRequest.overrideConfiguration().orElse(null)); + opWithSigv4UnSignedPayloadAndStreamingRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Database Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "opWithSigv4UnSignedPayloadAndStreaming"); return clientHandler - .execute(new ClientExecutionParams() - .withOperationName("opWithSigv4UnSignedPayloadAndStreaming") - .withProtocolMetadata(protocolMetadata) - .withResponseHandler(responseHandler) - .withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration) - .withInput(opWithSigv4UnSignedPayloadAndStreamingRequest) - .withMetricCollector(apiCallMetricCollector) - .withRequestBody(requestBody) - .withMarshaller( - StreamingRequestMarshaller - .builder() - .delegateMarshaller( - new OpWithSigv4UnSignedPayloadAndStreamingRequestMarshaller(protocolFactory)) - .requestBody(requestBody).transferEncoding(true).build())); + .execute(new ClientExecutionParams() + .withOperationName("opWithSigv4UnSignedPayloadAndStreaming") + .withProtocolMetadata(protocolMetadata) + .withResponseHandler(responseHandler) + .withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration) + .withInput(opWithSigv4UnSignedPayloadAndStreamingRequest) + .withMetricCollector(apiCallMetricCollector) + .withRequestBody(requestBody) + .withMarshaller( + StreamingRequestMarshaller + .builder() + .delegateMarshaller( + new OpWithSigv4UnSignedPayloadAndStreamingRequestMarshaller(protocolFactory)) + .requestBody(requestBody).transferEncoding(true).build())); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } @@ -506,33 +585,44 @@ public OpWithSigv4UnSignedPayloadAndStreamingResponse opWithSigv4UnSignedPayload */ @Override public OpWithSigv4ASignedPayloadResponse opWithSigv4aSignedPayload( - OpWithSigv4ASignedPayloadRequest opWithSigv4ASignedPayloadRequest) throws InvalidInputException, AwsServiceException, - SdkClientException, DatabaseException { + OpWithSigv4ASignedPayloadRequest opWithSigv4ASignedPayloadRequest) throws InvalidInputException, AwsServiceException, + SdkClientException, DatabaseException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OpWithSigv4ASignedPayloadResponse::builder); - + operationMetadata, OpWithSigv4ASignedPayloadResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(opWithSigv4ASignedPayloadRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, opWithSigv4ASignedPayloadRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Database Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "opWithSigv4aSignedPayload"); return clientHandler - .execute(new ClientExecutionParams() - .withOperationName("opWithSigv4aSignedPayload").withProtocolMetadata(protocolMetadata) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withInput(opWithSigv4ASignedPayloadRequest) - .withMetricCollector(apiCallMetricCollector) - .withMarshaller(new OpWithSigv4ASignedPayloadRequestMarshaller(protocolFactory))); + .execute(new ClientExecutionParams() + .withOperationName("opWithSigv4aSignedPayload").withProtocolMetadata(protocolMetadata) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withInput(opWithSigv4ASignedPayloadRequest) + .withMetricCollector(apiCallMetricCollector) + .withMarshaller(new OpWithSigv4ASignedPayloadRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } @@ -560,33 +650,44 @@ public OpWithSigv4ASignedPayloadResponse opWithSigv4aSignedPayload( */ @Override public OpWithSigv4AUnSignedPayloadResponse opWithSigv4aUnSignedPayload( - OpWithSigv4AUnSignedPayloadRequest opWithSigv4AUnSignedPayloadRequest) throws InvalidInputException, - AwsServiceException, SdkClientException, DatabaseException { + OpWithSigv4AUnSignedPayloadRequest opWithSigv4AUnSignedPayloadRequest) throws InvalidInputException, + AwsServiceException, SdkClientException, DatabaseException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OpWithSigv4AUnSignedPayloadResponse::builder); - + operationMetadata, OpWithSigv4AUnSignedPayloadResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(opWithSigv4AUnSignedPayloadRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, opWithSigv4AUnSignedPayloadRequest - .overrideConfiguration().orElse(null)); + .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Database Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "opWithSigv4aUnSignedPayload"); return clientHandler - .execute(new ClientExecutionParams() - .withOperationName("opWithSigv4aUnSignedPayload").withProtocolMetadata(protocolMetadata) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withInput(opWithSigv4AUnSignedPayloadRequest) - .withMetricCollector(apiCallMetricCollector) - .withMarshaller(new OpWithSigv4AUnSignedPayloadRequestMarshaller(protocolFactory))); + .execute(new ClientExecutionParams() + .withOperationName("opWithSigv4aUnSignedPayload").withProtocolMetadata(protocolMetadata) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withInput(opWithSigv4AUnSignedPayloadRequest) + .withMetricCollector(apiCallMetricCollector) + .withMarshaller(new OpWithSigv4AUnSignedPayloadRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } @@ -615,33 +716,44 @@ public OpWithSigv4AUnSignedPayloadResponse opWithSigv4aUnSignedPayload( */ @Override public OpsWithSigv4AndSigv4ASignedPayloadResponse opsWithSigv4andSigv4aSignedPayload( - OpsWithSigv4AndSigv4ASignedPayloadRequest opsWithSigv4AndSigv4ASignedPayloadRequest) throws InvalidInputException, - AwsServiceException, SdkClientException, DatabaseException { + OpsWithSigv4AndSigv4ASignedPayloadRequest opsWithSigv4AndSigv4ASignedPayloadRequest) throws InvalidInputException, + AwsServiceException, SdkClientException, DatabaseException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( - operationMetadata, OpsWithSigv4AndSigv4ASignedPayloadResponse::builder); - + operationMetadata, OpsWithSigv4AndSigv4ASignedPayloadResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(opsWithSigv4AndSigv4ASignedPayloadRequest, - this.clientConfiguration); + this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - opsWithSigv4AndSigv4ASignedPayloadRequest.overrideConfiguration().orElse(null)); + opsWithSigv4AndSigv4ASignedPayloadRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Database Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "opsWithSigv4andSigv4aSignedPayload"); return clientHandler - .execute(new ClientExecutionParams() - .withOperationName("opsWithSigv4andSigv4aSignedPayload").withProtocolMetadata(protocolMetadata) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration).withInput(opsWithSigv4AndSigv4ASignedPayloadRequest) - .withMetricCollector(apiCallMetricCollector) - .withMarshaller(new OpsWithSigv4AndSigv4ASignedPayloadRequestMarshaller(protocolFactory))); + .execute(new ClientExecutionParams() + .withOperationName("opsWithSigv4andSigv4aSignedPayload").withProtocolMetadata(protocolMetadata) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration).withInput(opsWithSigv4AndSigv4ASignedPayloadRequest) + .withMetricCollector(apiCallMetricCollector) + .withMarshaller(new OpsWithSigv4AndSigv4ASignedPayloadRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } @@ -670,34 +782,45 @@ public OpsWithSigv4AndSigv4ASignedPayloadResponse opsWithSigv4andSigv4aSignedPay */ @Override public SecondOpsWithSigv4AndSigv4ASignedPayloadResponse secondOpsWithSigv4andSigv4aSignedPayload( - SecondOpsWithSigv4AndSigv4ASignedPayloadRequest secondOpsWithSigv4AndSigv4ASignedPayloadRequest) - throws InvalidInputException, AwsServiceException, SdkClientException, DatabaseException { + SecondOpsWithSigv4AndSigv4ASignedPayloadRequest secondOpsWithSigv4AndSigv4ASignedPayloadRequest) + throws InvalidInputException, AwsServiceException, SdkClientException, DatabaseException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) - .isPayloadJson(true).build(); + .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory - .createResponseHandler(operationMetadata, SecondOpsWithSigv4AndSigv4ASignedPayloadResponse::builder); - + .createResponseHandler(operationMetadata, SecondOpsWithSigv4AndSigv4ASignedPayloadResponse::builder); + Function> exceptionMetadataMapper = errorCode -> { + if (errorCode == null) { + return Optional.empty(); + } + switch (errorCode) { + case "InvalidInput": + return Optional.of(ExceptionMetadata.builder().errorCode("InvalidInput").httpStatusCode(400) + .exceptionBuilderSupplier(InvalidInputException::builder).build()); + default: + return Optional.empty(); + } + }; HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, - operationMetadata); + operationMetadata, exceptionMetadataMapper); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration( - secondOpsWithSigv4AndSigv4ASignedPayloadRequest, this.clientConfiguration); + secondOpsWithSigv4AndSigv4ASignedPayloadRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, - secondOpsWithSigv4AndSigv4ASignedPayloadRequest.overrideConfiguration().orElse(null)); + secondOpsWithSigv4AndSigv4ASignedPayloadRequest.overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector - .create("ApiCall"); + .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Database Service"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "secondOpsWithSigv4andSigv4aSignedPayload"); return clientHandler - .execute(new ClientExecutionParams() - .withOperationName("secondOpsWithSigv4andSigv4aSignedPayload").withProtocolMetadata(protocolMetadata) - .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) - .withRequestConfiguration(clientConfiguration) - .withInput(secondOpsWithSigv4AndSigv4ASignedPayloadRequest) - .withMetricCollector(apiCallMetricCollector) - .withMarshaller(new SecondOpsWithSigv4AndSigv4ASignedPayloadRequestMarshaller(protocolFactory))); + .execute(new ClientExecutionParams() + .withOperationName("secondOpsWithSigv4andSigv4aSignedPayload").withProtocolMetadata(protocolMetadata) + .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) + .withRequestConfiguration(clientConfiguration) + .withInput(secondOpsWithSigv4AndSigv4ASignedPayloadRequest) + .withMetricCollector(apiCallMetricCollector) + .withMarshaller(new SecondOpsWithSigv4AndSigv4ASignedPayloadRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } @@ -709,7 +832,7 @@ public final String serviceName() { } private static List resolveMetricPublishers(SdkClientConfiguration clientConfiguration, - RequestOverrideConfiguration requestOverrideConfiguration) { + RequestOverrideConfiguration requestOverrideConfiguration) { List publishers = null; if (requestOverrideConfiguration != null) { publishers = requestOverrideConfiguration.metricPublishers(); @@ -724,8 +847,8 @@ private static List resolveMetricPublishers(SdkClientConfigurat } private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory, - JsonOperationMetadata operationMetadata) { - return protocolFactory.createErrorResponseHandler(operationMetadata); + JsonOperationMetadata operationMetadata, Function> exceptionMetadataMapper) { + return protocolFactory.createErrorResponseHandler(operationMetadata, exceptionMetadataMapper); } private void updateRetryStrategyClientConfiguration(SdkClientConfiguration.Builder configuration) { @@ -758,7 +881,7 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, return configuration.build(); } DatabaseServiceClientConfigurationBuilder serviceConfigBuilder = new DatabaseServiceClientConfigurationBuilder( - configuration); + configuration); for (SdkPlugin plugin : plugins) { plugin.configureClient(serviceConfigBuilder); } @@ -767,14 +890,8 @@ private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, } private > T init(T builder) { - return builder - .clientConfiguration(clientConfiguration) - .defaultServiceExceptionSupplier(DatabaseException::builder) - .protocol(AwsJsonProtocol.REST_JSON) - .protocolVersion("1.1") - .registerModeledException( - ExceptionMetadata.builder().errorCode("InvalidInput") - .exceptionBuilderSupplier(InvalidInputException::builder).httpStatusCode(400).build()); + return builder.clientConfiguration(clientConfiguration).defaultServiceExceptionSupplier(DatabaseException::builder) + .protocol(AwsJsonProtocol.REST_JSON).protocolVersion("1.1"); } @Override diff --git a/services/kinesis/src/test/java/software/amazon/awssdk/services/kinesis/KinesisExceptionTest.java b/services/kinesis/src/test/java/software/amazon/awssdk/services/kinesis/KinesisExceptionTest.java new file mode 100644 index 000000000000..528878c1f0d9 --- /dev/null +++ b/services/kinesis/src/test/java/software/amazon/awssdk/services/kinesis/KinesisExceptionTest.java @@ -0,0 +1,108 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package software.amazon.awssdk.services.kinesis; + +import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.client.WireMock; +import com.github.tomakehurst.wiremock.core.WireMockConfiguration; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.kinesis.model.GetRecordsRequest; +import software.amazon.awssdk.services.kinesis.model.InvalidArgumentException; +import software.amazon.awssdk.services.kinesis.model.ResourceNotFoundException; + +import java.net.URI; + +import static org.junit.Assert.assertEquals; + +public class KinesisExceptionTest { + private static final Logger logger = LoggerFactory.getLogger(KinesisExceptionTest.class); + private WireMockServer wireMock; + private KinesisClient client; + + @Before + public void setup() { + wireMock = new WireMockServer(WireMockConfiguration.wireMockConfig().dynamicPort()); + wireMock.start(); + + client = KinesisClient.builder() + .endpointOverride(URI.create(wireMock.baseUrl())) + .region(Region.US_WEST_2) + .credentialsProvider(StaticCredentialsProvider.create( + AwsBasicCredentials.create("fake-key", "fake-secret"))) + .build(); + } + + @After + public void tearDown() { + wireMock.stop(); + } + + @Test + public void testInvalidArgumentException() { + wireMock.stubFor(WireMock.post(WireMock.urlPathEqualTo("/")) + .willReturn(WireMock.aResponse() + .withStatus(400) + .withHeader("x-amzn-ErrorType", "InvalidArgumentException") + .withBody("{\"__type\":\"InvalidArgumentException\",\"message\":\"Invalid shard iterator\"}"))); + + try { + GetRecordsRequest request = GetRecordsRequest.builder() + .shardIterator("Invalid-Shard-Iterator") + .build(); + + client.getRecords(request); + } catch (InvalidArgumentException e) { + logger.info("Caught expected exception: {}", e.getClass().getSimpleName()); + logger.info("Status Code: {}", e.statusCode()); + logger.info("Error Code: {}", e.awsErrorDetails().errorCode()); + logger.info("Error Message: {}", e.awsErrorDetails().errorMessage()); + + assertEquals(400, e.statusCode()); + assertEquals("InvalidArgumentException", e.awsErrorDetails().errorCode()); + } + } + + @Test + public void testResourceNotFoundException() { + wireMock.stubFor(WireMock.post(WireMock.urlPathEqualTo("/")) + .willReturn(WireMock.aResponse() + .withStatus(400) + .withHeader("x-amzn-ErrorType", "ResourceNotFoundException"))); + + try { + GetRecordsRequest request = GetRecordsRequest.builder() + .shardIterator("NonExistent-Shard-Iterator") + .build(); + + client.getRecords(request); + } catch (ResourceNotFoundException e) { + logger.info("Caught expected exception: {}", e.getClass().getSimpleName()); + logger.info("Status Code: {}", e.statusCode()); + logger.info("Error Code: {}", e.awsErrorDetails().errorCode()); + logger.info("Error Message: {}", e.awsErrorDetails().errorMessage()); + + assertEquals(400, e.statusCode()); + assertEquals("ResourceNotFoundException", e.awsErrorDetails().errorCode()); + } + } +} From e774cc8ebb1dae8277edb37027ee63bbec864df9 Mon Sep 17 00:00:00 2001 From: Saranya Somepalli Date: Wed, 21 May 2025 08:42:22 -0700 Subject: [PATCH 2/2] Updating tests --- .../kinesis/KinesisExceptionTest.java | 59 +++++++++---------- 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/services/kinesis/src/test/java/software/amazon/awssdk/services/kinesis/KinesisExceptionTest.java b/services/kinesis/src/test/java/software/amazon/awssdk/services/kinesis/KinesisExceptionTest.java index 528878c1f0d9..2c52ee033bcc 100644 --- a/services/kinesis/src/test/java/software/amazon/awssdk/services/kinesis/KinesisExceptionTest.java +++ b/services/kinesis/src/test/java/software/amazon/awssdk/services/kinesis/KinesisExceptionTest.java @@ -33,6 +33,8 @@ import java.net.URI; import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class KinesisExceptionTest { private static final Logger logger = LoggerFactory.getLogger(KinesisExceptionTest.class); @@ -63,25 +65,22 @@ public void testInvalidArgumentException() { .willReturn(WireMock.aResponse() .withStatus(400) .withHeader("x-amzn-ErrorType", "InvalidArgumentException") - .withBody("{\"__type\":\"InvalidArgumentException\",\"message\":\"Invalid shard iterator\"}"))); - - try { - GetRecordsRequest request = GetRecordsRequest.builder() - .shardIterator("Invalid-Shard-Iterator") - .build(); - - client.getRecords(request); - } catch (InvalidArgumentException e) { - logger.info("Caught expected exception: {}", e.getClass().getSimpleName()); - logger.info("Status Code: {}", e.statusCode()); - logger.info("Error Code: {}", e.awsErrorDetails().errorCode()); - logger.info("Error Message: {}", e.awsErrorDetails().errorMessage()); - - assertEquals(400, e.statusCode()); - assertEquals("InvalidArgumentException", e.awsErrorDetails().errorCode()); - } + .withHeader("Content-Type", "application/json"))); + + GetRecordsRequest request = GetRecordsRequest.builder() + .shardIterator("Invalid-Shard-Iterator") + .build(); + + assertThatThrownBy(() -> client.getRecords(request)) + .isInstanceOf(InvalidArgumentException.class) + .satisfies(e -> { + InvalidArgumentException exception = (InvalidArgumentException) e; + assertThat(exception.statusCode()).isEqualTo(400); + assertThat(exception.awsErrorDetails().errorCode()).isEqualTo("InvalidArgumentException"); + }); } + @Test public void testResourceNotFoundException() { wireMock.stubFor(WireMock.post(WireMock.urlPathEqualTo("/")) @@ -89,20 +88,16 @@ public void testResourceNotFoundException() { .withStatus(400) .withHeader("x-amzn-ErrorType", "ResourceNotFoundException"))); - try { - GetRecordsRequest request = GetRecordsRequest.builder() - .shardIterator("NonExistent-Shard-Iterator") - .build(); - - client.getRecords(request); - } catch (ResourceNotFoundException e) { - logger.info("Caught expected exception: {}", e.getClass().getSimpleName()); - logger.info("Status Code: {}", e.statusCode()); - logger.info("Error Code: {}", e.awsErrorDetails().errorCode()); - logger.info("Error Message: {}", e.awsErrorDetails().errorMessage()); - - assertEquals(400, e.statusCode()); - assertEquals("ResourceNotFoundException", e.awsErrorDetails().errorCode()); - } + GetRecordsRequest request = GetRecordsRequest.builder() + .shardIterator("NonExistent-Shard-Iterator") + .build(); + + assertThatThrownBy(() -> client.getRecords(request)) + .isInstanceOf(ResourceNotFoundException.class) + .satisfies(e -> { + ResourceNotFoundException exception = (ResourceNotFoundException) e; + assertThat(exception.statusCode()).isEqualTo(400); + assertThat(exception.awsErrorDetails().errorCode()).isEqualTo("ResourceNotFoundException"); + }); } }