21
21
import com .squareup .javapoet .MethodSpec ;
22
22
import com .squareup .javapoet .ParameterizedTypeName ;
23
23
import com .squareup .javapoet .TypeName ;
24
- import java .util .HashSet ;
25
24
import java .util .Optional ;
26
- import java .util .Set ;
27
25
import java .util .concurrent .CompletableFuture ;
28
- import java .util .function .Function ;
29
26
import javax .lang .model .element .Modifier ;
30
27
import software .amazon .awssdk .awscore .exception .AwsServiceException ;
31
28
import software .amazon .awssdk .codegen .model .intermediate .IntermediateModel ;
32
29
import software .amazon .awssdk .codegen .model .intermediate .OperationModel ;
33
- import software .amazon .awssdk .codegen .model .intermediate .ShapeModel ;
34
30
import software .amazon .awssdk .codegen .poet .PoetExtension ;
35
31
import software .amazon .awssdk .codegen .poet .auth .scheme .AuthSchemeSpecUtils ;
36
32
import software .amazon .awssdk .codegen .poet .client .traits .HttpChecksumRequiredTrait ;
40
36
import software .amazon .awssdk .core .async .AsyncResponseTransformer ;
41
37
import software .amazon .awssdk .core .client .handler .ClientExecutionParams ;
42
38
import software .amazon .awssdk .core .http .HttpResponseHandler ;
43
- import software .amazon .awssdk .protocols .core .ExceptionMetadata ;
44
39
import software .amazon .awssdk .protocols .query .AwsQueryProtocolFactory ;
45
40
import software .amazon .awssdk .utils .CompletableFutureUtils ;
46
41
@@ -74,6 +69,8 @@ public MethodSpec initProtocolFactory(IntermediateModel model) {
74
69
75
70
methodSpec .addCode ("return $T.builder()\n " , protocolFactoryClass ());
76
71
72
+ registerModeledExceptions (model , poetExtensions ).forEach (methodSpec ::addCode );
73
+
77
74
methodSpec .addCode (".clientConfiguration(clientConfiguration)\n "
78
75
+ ".defaultServiceExceptionSupplier($T::builder)\n " ,
79
76
poetExtensions .getModelClass (model .getSdkModeledExceptionBaseClassName ()));
@@ -97,41 +94,11 @@ public CodeBlock responseHandler(IntermediateModel model,
97
94
98
95
@ Override
99
96
public Optional <CodeBlock > errorResponseHandler (OperationModel opModel ) {
100
- CodeBlock .Builder builder = CodeBlock .builder ();
101
- ParameterizedTypeName metadataMapperType = ParameterizedTypeName .get (
102
- ClassName .get (Function .class ),
103
- ClassName .get (String .class ),
104
- ParameterizedTypeName .get (Optional .class , ExceptionMetadata .class ));
105
-
106
- builder .add ("\n $T exceptionMetadataMapper = errorCode -> {\n " , metadataMapperType );
107
- builder .add ("switch (errorCode) {\n " );
108
- Set <String > processedExceptions = new HashSet <>();
109
-
110
- opModel .getExceptions ().forEach (exception -> {
111
- String exceptionName = exception .getExceptionName ();
112
- if (!processedExceptions .add (exceptionName )) {
113
- return ;
114
- }
115
- ShapeModel exceptionShape = intermediateModel .getShapes ().get (exceptionName );
116
- String errorCode = exceptionShape .getErrorCode ();
117
- builder .add ("case $S:\n " , errorCode );
118
- builder .add ("return $T.of($T.builder()\n " , Optional .class , ExceptionMetadata .class )
119
- .add (".errorCode($S)\n " , errorCode );
120
- builder .add (populateHttpStatusCode (exceptionShape , intermediateModel ));
121
- builder .add (".exceptionBuilderSupplier($T::builder)\n " ,
122
- poetExtensions .getModelClassFromShape (exceptionShape ))
123
- .add (".build());\n " );
124
- });
125
-
126
- builder .add ("default: return $T.empty();\n " , Optional .class );
127
- builder .add ("}\n " );
128
- builder .add ("};\n \n " );
129
-
130
- builder .add ("$T<$T> errorResponseHandler = protocolFactory.createErrorResponseHandler(exceptionMetadataMapper);" ,
131
- HttpResponseHandler .class ,
132
- AwsServiceException .class );
133
-
134
- return Optional .of (builder .build ());
97
+ return Optional .of (
98
+ CodeBlock .builder ()
99
+ .add ("\n \n $T errorResponseHandler = protocolFactory.createErrorResponseHandler();" ,
100
+ ParameterizedTypeName .get (HttpResponseHandler .class , AwsServiceException .class ))
101
+ .build ());
135
102
}
136
103
137
104
@ Override
@@ -226,18 +193,6 @@ public CodeBlock asyncExecutionHandler(IntermediateModel intermediateModel, Oper
226
193
227
194
@ Override
228
195
public Optional <MethodSpec > createErrorResponseHandler () {
229
- ParameterizedTypeName mapperType = ParameterizedTypeName .get (
230
- ClassName .get (Function .class ),
231
- ClassName .get (String .class ),
232
- ParameterizedTypeName .get (Optional .class , ExceptionMetadata .class ));
233
-
234
- return Optional .of (MethodSpec .methodBuilder ("createErrorResponseHandler" )
235
- .addModifiers (Modifier .PRIVATE )
236
- .returns (ParameterizedTypeName .get (ClassName .get (HttpResponseHandler .class ),
237
- ClassName .get (AwsServiceException .class )))
238
- .addParameter (mapperType , "exceptionMetadataMapper" )
239
- .addStatement ("return protocolFactory.createErrorResponseHandler(exceptionMetadataMapper)" )
240
- .build ());
196
+ return Optional .empty ();
241
197
}
242
-
243
- }
198
+ }
0 commit comments