Skip to content

Commit

Permalink
Improve endpoints error when missing traits
Browse files Browse the repository at this point in the history
Old error was a generic NoSuchElementException and was thrown even if a
sigv4 trait was defined but service was not.

Now we only throw if both are undefined and the exception explains that
the traits are required.

```
No aws.auth#sigv4 or aws.api#service trait, required for Endpoint default signing name
```
  • Loading branch information
Caleb ツ Everett authored and srchase committed Dec 6, 2022
1 parent abd04f9 commit 4060172
Showing 1 changed file with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import java.nio.file.Paths;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import software.amazon.smithy.aws.traits.ServiceTrait;
import software.amazon.smithy.aws.traits.auth.SigV4Trait;
import software.amazon.smithy.model.Model;
Expand Down Expand Up @@ -110,12 +112,7 @@ private void generateEndpointParameters() {
ruleSet.getObjectMember("parameters").ifPresent(parameters -> {
parameters.accept(new RuleSetParametersVisitor(writer, true));
});
ServiceTrait serviceTrait = service.getTrait(ServiceTrait.class).get();
writer.write(
"defaultSigningName: \"$L\",",
service.getTrait(SigV4Trait.class).map(SigV4Trait::getName)
.orElse(serviceTrait.getArnNamespace())
);
writer.write("defaultSigningName: \"$L\",", defaultSigningName());
});
}
);
Expand All @@ -135,6 +132,21 @@ private void generateEndpointParameters() {
);
}

private String defaultSigningName() {
Optional<SigV4Trait> sigv4Trait = service.getTrait(SigV4Trait.class);
if (sigv4Trait.isPresent()) {
return sigv4Trait.get().getName();
}
Optional<ServiceTrait> serviceTrait = service.getTrait(ServiceTrait.class);
if (serviceTrait.isPresent()) {
return serviceTrait.get().getArnNamespace();
}
throw new NoSuchElementException(String.format(
"No %s or %s trait, required for Endpoint default signing name",
SigV4Trait.ID,
ServiceTrait.ID));
}

/**
* Generate the resolver function for this service.
*/
Expand Down

0 comments on commit 4060172

Please sign in to comment.