Skip to content

Commit dab57bd

Browse files
committed
Throw ModelInvalidException for bearerAuth issues
Change from IllegalStateException to ModelInvalidException for cases where the enableEnvironmentBearerToken customization is enabled but the service model does not support it. This commit adds a new error ID `INVALID_CODEGEN_CUSTOMIZATION` for these cases.
1 parent 4166fa1 commit dab57bd

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/BaseClientBuilderClass.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363
import software.amazon.awssdk.codegen.poet.rules.EndpointParamsKnowledgeIndex;
6464
import software.amazon.awssdk.codegen.poet.rules.EndpointRulesSpecUtils;
6565
import software.amazon.awssdk.codegen.utils.AuthUtils;
66+
import software.amazon.awssdk.codegen.validation.ModelInvalidException;
67+
import software.amazon.awssdk.codegen.validation.ValidationEntry;
68+
import software.amazon.awssdk.codegen.validation.ValidationErrorId;
69+
import software.amazon.awssdk.codegen.validation.ValidationErrorSeverity;
6670
import software.amazon.awssdk.core.SdkPlugin;
6771
import software.amazon.awssdk.core.checksums.RequestChecksumCalculation;
6872
import software.amazon.awssdk.core.checksums.RequestChecksumCalculationResolver;
@@ -320,11 +324,20 @@ private MethodSpec mergeServiceDefaultsMethod() {
320324

321325
private void configureEnvironmentBearerToken(MethodSpec.Builder builder) {
322326
if (!authSchemeSpecUtils.useSraAuth()) {
323-
throw new IllegalStateException("The enableEnvironmentBearerToken customization requires SRA Auth.");
327+
ValidationEntry entry = ValidationEntry.create(ValidationErrorId.INVALID_CODEGEN_CUSTOMIZATION,
328+
ValidationErrorSeverity.DANGER,
329+
"The enableEnvironmentBearerToken customization requires"
330+
+ " the SRA Auth customization.");
331+
332+
throw ModelInvalidException.fromEntry(entry);
324333
}
325334
if (!AuthUtils.usesBearerAuth(model)) {
326-
throw new IllegalStateException("The enableEnvironmentBearerToken customization requires the service to model and "
327-
+ "support smithy.api#httpBearerAuth.");
335+
ValidationEntry entry =
336+
ValidationEntry.create( ValidationErrorId.INVALID_CODEGEN_CUSTOMIZATION, ValidationErrorSeverity.DANGER,
337+
"The enableEnvironmentBearerToken customization requires the service to model"
338+
+ " and support smithy.api#httpBearerAuth.");
339+
340+
throw ModelInvalidException.fromEntry(entry);
328341
}
329342

330343
builder.addStatement("$T tokenFromEnv = new $T().getStringValue()",

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public static Builder builder() {
3838
return new Builder();
3939
}
4040

41+
public static ModelInvalidException fromEntry(ValidationEntry entry) {
42+
return builder().validationEntries(Collections.singletonList(entry)).build();
43+
}
44+
4145
public static class Builder {
4246
private List<ValidationEntry> validationEntries;
4347

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public ValidationEntry withDetailMessage(String detailMessage) {
6161
return this;
6262
}
6363

64+
public static ValidationEntry create(ValidationErrorId errorId, ValidationErrorSeverity severity, String detailMessage) {
65+
return new ValidationEntry().withErrorId(errorId).withSeverity(severity).withDetailMessage(detailMessage);
66+
}
67+
6468
@Override
6569
public String toString() {
6670
return ToString.builder("ValidationEntry")

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public enum ValidationErrorId {
2222
),
2323
UNKNOWN_SHAPE_MEMBER("The model references an unknown shape member."),
2424
REQUEST_URI_NOT_FOUND("The request URI does not exist."),
25+
26+
INVALID_CODEGEN_CUSTOMIZATION("A customization is enabled for this service that cannot be applied for the given service "
27+
+ "model.")
2528
;
2629

2730
private final String description;

0 commit comments

Comments
 (0)