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