Skip to content

Commit c413f7d

Browse files
committed
Convert exceptions to ModelValidations
1 parent d9c547c commit c413f7d

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

codegen/src/main/java/software/amazon/awssdk/codegen/customization/processors/EventStreamSharedEventProcessor.java

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
import software.amazon.awssdk.codegen.model.service.Member;
2929
import software.amazon.awssdk.codegen.model.service.ServiceModel;
3030
import software.amazon.awssdk.codegen.model.service.Shape;
31+
import software.amazon.awssdk.codegen.validation.ModelInvalidException;
32+
import software.amazon.awssdk.codegen.validation.ValidationEntry;
33+
import software.amazon.awssdk.codegen.validation.ValidationErrorId;
34+
import software.amazon.awssdk.codegen.validation.ValidationErrorSeverity;
3135

3236
/**
3337
* Processor for eventstreams with shared events. This Processor does two things: 1. Apply the duplicateAndRenameSharedEvents
@@ -60,28 +64,34 @@ public void preprocess(ServiceModel serviceModel) {
6064
Member eventMemberToModify = eventStreamMembers.get(eventEntry.getKey());
6165

6266
if (eventMemberToModify == null) {
63-
throw new IllegalStateException(
67+
throw ModelInvalidException.fromEntry(ValidationEntry.create(
68+
ValidationErrorId.INVALID_CODEGEN_CUSTOMIZATION,
69+
ValidationErrorSeverity.DANGER,
6470
String.format("Cannot find event member [%s] in the eventstream [%s] when processing "
6571
+ "customization config duplicateAndRenameSharedEvents.%s",
66-
eventEntry.getKey(), eventStreamName, eventStreamName));
72+
eventEntry.getKey(), eventStreamName, eventStreamName)));
6773
}
6874

6975
String shapeToDuplicate = eventMemberToModify.getShape();
7076
Shape eventMemberShape = serviceModel.getShape(shapeToDuplicate);
7177

7278
if (eventMemberShape == null || !eventMemberShape.isEvent()) {
73-
throw new IllegalStateException(
79+
throw ModelInvalidException.fromEntry(ValidationEntry.create(
80+
ValidationErrorId.INVALID_CODEGEN_CUSTOMIZATION,
81+
ValidationErrorSeverity.DANGER,
7482
String.format("Error: [%s] must be an Event shape when processing "
7583
+ "customization config duplicateAndRenameSharedEvents.%s",
76-
eventEntry.getKey(), eventStreamName));
84+
eventEntry.getKey(), eventStreamName)));
7785
}
7886

7987
String newShapeName = eventEntry.getValue();
8088
if (serviceModel.getShapes().containsKey(newShapeName)) {
81-
throw new IllegalStateException(
89+
throw ModelInvalidException.fromEntry(ValidationEntry.create(
90+
ValidationErrorId.INVALID_CODEGEN_CUSTOMIZATION,
91+
ValidationErrorSeverity.DANGER,
8292
String.format("Error: [%s] is already in the model when processing "
8393
+ "customization config duplicateAndRenameSharedEvents.%s",
84-
newShapeName, eventStreamName));
94+
newShapeName, eventStreamName)));
8595
}
8696
serviceModel.getShapes().put(newShapeName, duplicateShape(eventMemberShape));
8797
eventMemberToModify.setShape(newShapeName);
@@ -105,14 +115,18 @@ private Shape duplicateShape(Shape shape) {
105115

106116
private static void validateIsEventStream(Shape shape, String name) {
107117
if (shape == null) {
108-
throw new IllegalStateException(
118+
throw ModelInvalidException.fromEntry(ValidationEntry.create(
119+
ValidationErrorId.INVALID_CODEGEN_CUSTOMIZATION,
120+
ValidationErrorSeverity.DANGER,
109121
String.format("Cannot find eventstream shape [%s] in the model when processing "
110-
+ "customization config duplicateAndRenameSharedEvents.%s", name, name));
122+
+ "customization config duplicateAndRenameSharedEvents.%s", name, name)));
111123
}
112124
if (!shape.isEventstream()) {
113-
throw new IllegalStateException(
125+
throw ModelInvalidException.fromEntry(ValidationEntry.create(
126+
ValidationErrorId.INVALID_CODEGEN_CUSTOMIZATION,
127+
ValidationErrorSeverity.DANGER,
114128
String.format("Error: [%s] must be an EventStream when processing "
115-
+ "customization config duplicateAndRenameSharedEvents.%s", name, name));
129+
+ "customization config duplicateAndRenameSharedEvents.%s", name, name)));
116130
}
117131
}
118132

@@ -129,10 +143,13 @@ public void postprocess(IntermediateModel intermediateModel) {
129143
if (memberShape != null && memberShape.isEvent()) {
130144
if (seenEvents.containsKey(memberShape.getShapeName())
131145
&& !seenEvents.get(memberShape.getShapeName()).equals(shapeModel.getShapeName())) {
132-
throw new IllegalStateException(
146+
throw ModelInvalidException.fromEntry(ValidationEntry.create(
147+
ValidationErrorId.SHARED_EVENTSTREAM_EVENT,
148+
ValidationErrorSeverity.DANGER,
133149
String.format("Event shape `%s` is shared between multiple EventStreams. Apply the "
134150
+ "duplicateAndRenameSharedEvents customization to resolve the issue.",
135-
memberShape.getShapeName()));
151+
memberShape.getShapeName())
152+
));
136153
}
137154
seenEvents.put(memberShape.getShapeName(), shapeModel.getShapeName());
138155
}

codegen/src/main/java/software/amazon/awssdk/codegen/validation/ValidationErrorId.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ public enum ValidationErrorId {
2626
INVALID_CODEGEN_CUSTOMIZATION("A customization is enabled for this service that cannot be applied for the given service "
2727
+ "model."),
2828
UNKNOWN_OPERATION("The model references an unknown operation."),
29-
INVALID_IDENTIFIER_NAME("The model contains an invalid or non-idiomatic name or identifier.");
29+
INVALID_IDENTIFIER_NAME("The model contains an invalid or non-idiomatic name or identifier."),
30+
SHARED_EVENTSTREAM_EVENT("Event shape shared between multiple EventStreams. "
31+
+ "Missing duplicateAndRenameSharedEvents customization.");
3032

3133
private final String description;
3234

codegen/src/test/java/software/amazon/awssdk/codegen/customization/processors/EventStreamSharedEventProcessorTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import software.amazon.awssdk.codegen.model.service.ServiceModel;
3232
import software.amazon.awssdk.codegen.model.service.Shape;
3333
import software.amazon.awssdk.codegen.utils.ModelLoaderUtils;
34+
import software.amazon.awssdk.codegen.validation.ModelInvalidException;
3435
import software.amazon.awssdk.utils.ImmutableMap;
3536

3637
public class EventStreamSharedEventProcessorTest {
@@ -76,7 +77,7 @@ public void modelWithSharedEvents_raises() {
7677
.serviceModel(serviceModel)
7778
.customizationConfig(emptyConfig)
7879
.build()).build())
79-
.isInstanceOf(IllegalStateException.class)
80+
.isInstanceOf(ModelInvalidException.class)
8081
.hasMessageContaining("Event shape `Payload` is shared between multiple EventStreams");
8182
}
8283

@@ -87,7 +88,7 @@ public void invalidCustomization_missingShape() {
8788
EventStreamSharedEventProcessor processor =
8889
new EventStreamSharedEventProcessor(duplicateAndRenameSharedEvents);
8990
assertThatThrownBy(() -> processor.preprocess(serviceModel))
90-
.isInstanceOf(IllegalStateException.class)
91+
.isInstanceOf(ModelInvalidException.class)
9192
.hasMessageContaining("Cannot find eventstream shape [MissingShape]");
9293
}
9394

@@ -98,7 +99,7 @@ public void invalidCustomization_notEventStream() {
9899
EventStreamSharedEventProcessor processor =
99100
new EventStreamSharedEventProcessor(duplicateAndRenameSharedEvents);
100101
assertThatThrownBy(() -> processor.preprocess(serviceModel))
101-
.isInstanceOf(IllegalStateException.class)
102+
.isInstanceOf(ModelInvalidException.class)
102103
.hasMessageContaining("Error: [Payload] must be an EventStream");
103104
}
104105

@@ -110,7 +111,7 @@ public void invalidCustomization_invalidMember() {
110111
EventStreamSharedEventProcessor processor =
111112
new EventStreamSharedEventProcessor(duplicateAndRenameSharedEvents);
112113
assertThatThrownBy(() -> processor.preprocess(serviceModel))
113-
.isInstanceOf(IllegalStateException.class)
114+
.isInstanceOf(ModelInvalidException.class)
114115
.hasMessageContaining("Cannot find event member [InvalidMember] in the eventstream [StreamB]");
115116
}
116117

@@ -122,7 +123,7 @@ public void invalidCustomization_shapeAlreadyExists() {
122123
EventStreamSharedEventProcessor processor =
123124
new EventStreamSharedEventProcessor(duplicateAndRenameSharedEvents);
124125
assertThatThrownBy(() -> processor.preprocess(serviceModel))
125-
.isInstanceOf(IllegalStateException.class)
126+
.isInstanceOf(ModelInvalidException.class)
126127
.hasMessageContaining("Error: [Payload] is already in the model");
127128
}
128129
}

0 commit comments

Comments
 (0)