16
16
package software .amazon .smithy .typescript .codegen .integration ;
17
17
18
18
import java .util .List ;
19
- import java .util .Map ;
20
19
import java .util .Optional ;
21
20
import java .util .Set ;
22
- import java .util .TreeMap ;
23
21
import java .util .TreeSet ;
24
22
import java .util .function .BiFunction ;
25
23
import java .util .function .Consumer ;
26
- import java .util .function .Function ;
27
24
import java .util .logging .Logger ;
28
- import java .util .stream .Collectors ;
29
25
import software .amazon .smithy .codegen .core .CodegenException ;
30
26
import software .amazon .smithy .codegen .core .Symbol ;
31
27
import software .amazon .smithy .codegen .core .SymbolProvider ;
32
28
import software .amazon .smithy .codegen .core .SymbolReference ;
33
29
import software .amazon .smithy .model .knowledge .HttpBinding .Location ;
30
+ import software .amazon .smithy .model .knowledge .OperationIndex ;
34
31
import software .amazon .smithy .model .pattern .SmithyPattern ;
35
32
import software .amazon .smithy .model .shapes .MemberShape ;
36
33
import software .amazon .smithy .model .shapes .OperationShape ;
@@ -314,7 +311,6 @@ public static void writeRetryableTrait(TypeScriptWriter writer, StructureShape e
314
311
* @param errorCodeGenerator A consumer
315
312
* @param shouldParseErrorBody Flag indicating whether need to parse response body in this dispatcher function
316
313
* @param bodyErrorLocationModifier A function that returns the location of an error in a body given a data source.
317
- * @param operationErrorsToShapes A map of error names to their {@link ShapeId}.
318
314
* @return A set of all error structure shapes for the operation that were dispatched to.
319
315
*/
320
316
static Set <StructureShape > generateErrorDispatcher (
@@ -323,11 +319,11 @@ static Set<StructureShape> generateErrorDispatcher(
323
319
SymbolReference responseType ,
324
320
Consumer <GenerationContext > errorCodeGenerator ,
325
321
boolean shouldParseErrorBody ,
326
- BiFunction <GenerationContext , String , String > bodyErrorLocationModifier ,
327
- BiFunction <GenerationContext , OperationShape , Map <String , ShapeId >> operationErrorsToShapes
322
+ BiFunction <GenerationContext , String , String > bodyErrorLocationModifier
328
323
) {
329
324
TypeScriptWriter writer = context .getWriter ();
330
325
SymbolProvider symbolProvider = context .getSymbolProvider ();
326
+ OperationIndex operationIndex = OperationIndex .of (context .getModel ());
331
327
Set <StructureShape > errorShapes = new TreeSet <>();
332
328
333
329
Symbol symbol = symbolProvider .toSymbol (operation );
@@ -359,14 +355,14 @@ static Set<StructureShape> generateErrorDispatcher(
359
355
errorCodeGenerator .accept (context );
360
356
writer .openBlock ("switch (errorCode) {" , "}" , () -> {
361
357
// Generate the case statement for each error, invoking the specific deserializer.
362
- operationErrorsToShapes . apply ( context , operation ) .forEach (( name , errorId ) -> {
363
- StructureShape error = context . getModel (). expectShape ( errorId ). asStructureShape (). get ();
358
+ new TreeSet <>( operationIndex . getErrors ( operation , context . getService ())) .forEach (error -> {
359
+ final ShapeId errorId = error . getId ();
364
360
// Track errors bound to the operation so their deserializers may be generated.
365
361
errorShapes .add (error );
366
362
Symbol errorSymbol = symbolProvider .toSymbol (error );
367
363
String errorDeserMethodName = ProtocolGenerator .getDeserFunctionName (errorSymbol ,
368
364
context .getProtocolName ()) + "Response" ;
369
- writer .openBlock ("case $S:\n case $S:" , " break;" , name , errorId .toString (), () -> {
365
+ writer .openBlock ("case $S:\n case $S:" , " break;" , errorId . getName () , errorId .toString (), () -> {
370
366
// Dispatch to the error deserialization function.
371
367
String outputParam = shouldParseErrorBody ? "parsedOutput" : "output" ;
372
368
writer .openBlock ("response = {" , "}" , () -> {
@@ -448,24 +444,4 @@ static void writeHostPrefix(GenerationContext context, OperationShape operation)
448
444
});
449
445
});
450
446
}
451
-
452
- /**
453
- * Returns a map of error names to their {@link ShapeId}.
454
- *
455
- * @param context the generation context
456
- * @param operation the operation shape to retrieve errors for
457
- * @return map of error names to {@link ShapeId}
458
- */
459
- public static Map <String , ShapeId > getOperationErrors (GenerationContext context , OperationShape operation ) {
460
- return operation .getErrors ().stream ()
461
- .collect (Collectors .toMap (
462
- shapeId -> shapeId .getName (context .getService ()),
463
- Function .identity (),
464
- (x , y ) -> {
465
- if (!x .equals (y )) {
466
- throw new CodegenException (String .format ("conflicting error shape ids: %s, %s" , x , y ));
467
- }
468
- return x ;
469
- }, TreeMap ::new ));
470
- }
471
447
}
0 commit comments