Skip to content

Commit 41de58c

Browse files
authored
Revert support delegation of determining errors for an operation (#494)
1 parent 0486ca3 commit 41de58c

File tree

4 files changed

+8
-44
lines changed

4 files changed

+8
-44
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2044,7 +2044,7 @@ private void generateOperationResponseDeserializer(
20442044
// Write out the error deserialization dispatcher.
20452045
Set<StructureShape> errorShapes = HttpProtocolGeneratorUtils.generateErrorDispatcher(
20462046
context, operation, responseType, this::writeErrorCodeParser,
2047-
isErrorCodeInBody, this::getErrorBodyLocation, this::getOperationErrors);
2047+
isErrorCodeInBody, this::getErrorBodyLocation);
20482048
deserializingErrorShapes.addAll(errorShapes);
20492049
}
20502050

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpProtocolGeneratorUtils.java

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,18 @@
1616
package software.amazon.smithy.typescript.codegen.integration;
1717

1818
import java.util.List;
19-
import java.util.Map;
2019
import java.util.Optional;
2120
import java.util.Set;
22-
import java.util.TreeMap;
2321
import java.util.TreeSet;
2422
import java.util.function.BiFunction;
2523
import java.util.function.Consumer;
26-
import java.util.function.Function;
2724
import java.util.logging.Logger;
28-
import java.util.stream.Collectors;
2925
import software.amazon.smithy.codegen.core.CodegenException;
3026
import software.amazon.smithy.codegen.core.Symbol;
3127
import software.amazon.smithy.codegen.core.SymbolProvider;
3228
import software.amazon.smithy.codegen.core.SymbolReference;
3329
import software.amazon.smithy.model.knowledge.HttpBinding.Location;
30+
import software.amazon.smithy.model.knowledge.OperationIndex;
3431
import software.amazon.smithy.model.pattern.SmithyPattern;
3532
import software.amazon.smithy.model.shapes.MemberShape;
3633
import software.amazon.smithy.model.shapes.OperationShape;
@@ -314,7 +311,6 @@ public static void writeRetryableTrait(TypeScriptWriter writer, StructureShape e
314311
* @param errorCodeGenerator A consumer
315312
* @param shouldParseErrorBody Flag indicating whether need to parse response body in this dispatcher function
316313
* @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}.
318314
* @return A set of all error structure shapes for the operation that were dispatched to.
319315
*/
320316
static Set<StructureShape> generateErrorDispatcher(
@@ -323,11 +319,11 @@ static Set<StructureShape> generateErrorDispatcher(
323319
SymbolReference responseType,
324320
Consumer<GenerationContext> errorCodeGenerator,
325321
boolean shouldParseErrorBody,
326-
BiFunction<GenerationContext, String, String> bodyErrorLocationModifier,
327-
BiFunction<GenerationContext, OperationShape, Map<String, ShapeId>> operationErrorsToShapes
322+
BiFunction<GenerationContext, String, String> bodyErrorLocationModifier
328323
) {
329324
TypeScriptWriter writer = context.getWriter();
330325
SymbolProvider symbolProvider = context.getSymbolProvider();
326+
OperationIndex operationIndex = OperationIndex.of(context.getModel());
331327
Set<StructureShape> errorShapes = new TreeSet<>();
332328

333329
Symbol symbol = symbolProvider.toSymbol(operation);
@@ -359,14 +355,14 @@ static Set<StructureShape> generateErrorDispatcher(
359355
errorCodeGenerator.accept(context);
360356
writer.openBlock("switch (errorCode) {", "}", () -> {
361357
// 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();
364360
// Track errors bound to the operation so their deserializers may be generated.
365361
errorShapes.add(error);
366362
Symbol errorSymbol = symbolProvider.toSymbol(error);
367363
String errorDeserMethodName = ProtocolGenerator.getDeserFunctionName(errorSymbol,
368364
context.getProtocolName()) + "Response";
369-
writer.openBlock("case $S:\ncase $S:", " break;", name, errorId.toString(), () -> {
365+
writer.openBlock("case $S:\ncase $S:", " break;", errorId.getName(), errorId.toString(), () -> {
370366
// Dispatch to the error deserialization function.
371367
String outputParam = shouldParseErrorBody ? "parsedOutput" : "output";
372368
writer.openBlock("response = {", "}", () -> {
@@ -448,24 +444,4 @@ static void writeHostPrefix(GenerationContext context, OperationShape operation)
448444
});
449445
});
450446
}
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-
}
471447
}

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpRpcProtocolGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ private void generateOperationDeserializer(GenerationContext context, OperationS
373373
// Write out the error deserialization dispatcher.
374374
Set<StructureShape> errorShapes = HttpProtocolGeneratorUtils.generateErrorDispatcher(
375375
context, operation, responseType, this::writeErrorCodeParser,
376-
isErrorCodeInBody, this::getErrorBodyLocation, this::getOperationErrors);
376+
isErrorCodeInBody, this::getErrorBodyLocation);
377377
deserializingErrorShapes.addAll(errorShapes);
378378
}
379379

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/ProtocolGenerator.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import java.util.Collection;
1919
import java.util.List;
20-
import java.util.Map;
2120
import java.util.function.Supplier;
2221
import java.util.stream.Collectors;
2322
import software.amazon.smithy.codegen.core.CodegenException;
@@ -265,17 +264,6 @@ static String getSerdeFunctionSymbolComponent(Symbol symbol, Shape shape) {
265264
}
266265
}
267266

268-
/**
269-
* Returns a map of error names to their {@link ShapeId}.
270-
*
271-
* @param context the generation context
272-
* @param operation the operation shape to retrieve errors for
273-
* @return map of error names to {@link ShapeId}
274-
*/
275-
default Map<String, ShapeId> getOperationErrors(GenerationContext context, OperationShape operation) {
276-
return HttpProtocolGeneratorUtils.getOperationErrors(context, operation);
277-
}
278-
279267
/**
280268
* Context object used for service serialization and deserialization.
281269
*/

0 commit comments

Comments
 (0)