Skip to content

Commit ee827e1

Browse files
authored
Throw ModelInvalidException for bearerAuth issues (#6229)
* 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. * Review comments
1 parent 29e1869 commit ee827e1

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "feature",
3+
"category": "AWS SDK for Java v2",
4+
"contributor": "",
5+
"description": "Add validation for invalid usages of the `enableEnvironmentBearerToken` codegen customization."
6+
}

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

Lines changed: 17 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,21 @@ 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 useSraAuth customization but it is disabled.");
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,
337+
ValidationErrorSeverity.DANGER,
338+
"The enableEnvironmentBearerToken customization requires the service to model"
339+
+ " and support smithy.api#httpBearerAuth.");
340+
341+
throw ModelInvalidException.fromEntry(entry);
328342
}
329343

330344
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)