Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add thrown exceptions to command documentation #715

Merged
merged 4 commits into from
Mar 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.traits.DocumentationTrait;
import software.amazon.smithy.model.traits.ErrorTrait;
import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait;
import software.amazon.smithy.typescript.codegen.endpointsV2.EndpointsParamNameMap;
import software.amazon.smithy.typescript.codegen.endpointsV2.RuleSetParameterFinder;
Expand Down Expand Up @@ -120,8 +123,20 @@ private void generateClientCommand() {
writer.addImport("MiddlewareStack", "MiddlewareStack", "@aws-sdk/types");

String name = symbol.getName();
writer.writeShapeDocs(operation, shapeDoc -> shapeDoc + "\n" + getCommandExample(serviceSymbol.getName(),
configType, name, inputType.getName(), outputType.getName()));

StringBuilder additionalDocs = new StringBuilder()
.append("\n")
.append(getCommandExample(
serviceSymbol.getName(), configType, name, inputType.getName(), outputType.getName()
))
.append("\n")
.append(getThrownExceptions());

writer.writeShapeDocs(
operation,
shapeDoc -> shapeDoc + additionalDocs
);

writer.openBlock(
"export class $L extends $$Command<$T, $T, $L> {", "}",
name, inputType, outputType,
Expand Down Expand Up @@ -169,6 +184,26 @@ private String getCommandExample(String serviceName, String configName, String c
+ String.format("@see {@link %s | config} for %s's `config` shape.%n", configName, serviceName);
}

private String getThrownExceptions() {
List<ShapeId> errors = operation.getErrors();
StringBuilder buffer = new StringBuilder();
for (ShapeId error : errors) {
Shape errorShape = model.getShape(error).get();
Optional<DocumentationTrait> doc = errorShape.getTrait(DocumentationTrait.class);
ErrorTrait errorTrait = errorShape.getTrait(ErrorTrait.class).get();

if (doc.isPresent()) {
buffer.append(String.format("@throws {@link %s} (%s fault)%n %s",
error.getName(), errorTrait.getValue(), doc.get().getValue()));
} else {
buffer.append(String.format("@throws {@link %s} (%s fault)",
error.getName(), errorTrait.getValue()));
}
buffer.append("\n\n");
}
return buffer.toString();
}

private void generateCommandConstructor() {
writer.openBlock("constructor(readonly input: $T) {", "}", inputType, () -> {
// The constructor can be intercepted and changed.
Expand Down