in codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/StructureGenerator.java [137:193]
private void renderErrorStructure() {
Symbol structureSymbol = symbolProvider.toSymbol(shape);
writer.addUseImports(SmithyGoDependency.SMITHY);
writer.addUseImports(SmithyGoDependency.FMT);
ErrorTrait errorTrait = shape.expectTrait(ErrorTrait.class);
// Write out a struct to hold the error data.
writer.writeShapeDocs(shape);
writer.openBlock("type $L struct {", "}", structureSymbol.getName(), () -> {
// The message is the only part of the standard APIError interface that isn't known ahead of time.
// Message is a pointer mostly for the sake of consistency.
writer.write("Message *string").write("");
writer.write("ErrorCodeOverride *string").write("");
for (MemberShape member : shape.getAllMembers().values()) {
String memberName = symbolProvider.toMemberName(member);
// error messages are represented under Message for consistency
if (!ERROR_MEMBER_NAMES.contains(memberName)) {
writer.write("$L $P", memberName, symbolProvider.toSymbol(member));
}
}
writer.write("");
writer.write("$L", ProtocolDocumentGenerator.NO_DOCUMENT_SERDE_TYPE_NAME);
}).write("");
// write the Error method to satisfy the standard error interface
writer.openBlock("func (e *$L) Error() string {", "}", structureSymbol.getName(), () -> {
writer.write("return fmt.Sprintf(\"%s: %s\", e.ErrorCode(), e.ErrorMessage())");
});
// Write out methods to satisfy the APIError interface. All but the message are known ahead of time,
// and for those we just encode the information in the method itself.
writer.openBlock("func (e *$L) ErrorMessage() string {", "}", structureSymbol.getName(), () -> {
writer.openBlock("if e.Message == nil {", "}", () -> {
writer.write("return \"\"");
});
writer.write("return *e.Message");
});
String errorCode = protocolGenerator == null ? shape.getId().getName(service)
: protocolGenerator.getErrorCode(service, shape);
writer.openBlock("func (e *$L) ErrorCode() string {", "}", structureSymbol.getName(), () -> {
writer.openBlock("if e == nil || e.ErrorCodeOverride == nil {", "}", () -> {
writer.write("return $S", errorCode);
});
writer.write("return *e.ErrorCodeOverride");
});
String fault = "smithy.FaultUnknown";
if (errorTrait.isClientError()) {
fault = "smithy.FaultClient";
} else if (errorTrait.isServerError()) {
fault = "smithy.FaultServer";
}
writer.write("func (e *$L) ErrorFault() smithy.ErrorFault { return $L }", structureSymbol.getName(), fault);
}