-
Notifications
You must be signed in to change notification settings - Fork 105
Add generated error types for convenience #298
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.smithy.typescript.codegen; | ||
|
||
import software.amazon.smithy.codegen.core.Symbol; | ||
import software.amazon.smithy.codegen.core.SymbolProvider; | ||
import software.amazon.smithy.model.Model; | ||
import software.amazon.smithy.model.shapes.StructureShape; | ||
import software.amazon.smithy.model.traits.ErrorTrait; | ||
|
||
/** | ||
* Generates convenience error types for servers, since service developers will throw a modeled exception directly, | ||
* while clients typically care only about catching them. | ||
*/ | ||
final class ServerErrorGenerator implements Runnable { | ||
|
||
private final TypeScriptSettings settings; | ||
private final Model model; | ||
private final StructureShape errorShape; | ||
private final SymbolProvider symbolProvider; | ||
private final TypeScriptWriter writer; | ||
|
||
ServerErrorGenerator(TypeScriptSettings settings, | ||
Model model, | ||
StructureShape errorShape, | ||
SymbolProvider symbolProvider, | ||
TypeScriptWriter writer) { | ||
this.settings = settings; | ||
this.model = model; | ||
this.errorShape = errorShape; | ||
this.symbolProvider = symbolProvider; | ||
this.writer = writer; | ||
} | ||
|
||
|
||
@Override | ||
public void run() { | ||
Symbol symbol = symbolProvider.toSymbol(errorShape); | ||
|
||
// Did not add this as a symbol to the error shape since these should not be used by anyone but the user. | ||
String typeName = symbol.getName() + "Error"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is suffixing errors being used to get around having to alias the error interface? It would probably be better to construct a SymbolReference symbol = SymbolReference.builder()
.symbol(errorInterfaceSymbol)
.alias("__" + errorInterfaceSymbol.getName())
.build(); Of course you could also do: String errorInterfaceAlias = "__" + errorInterfaceSymbol.getName();
writer.addImport(errorInterfaceSymbol, errorInterfaceAlias, SymbolReference.ContextOption.USE); And then manually pass around the name string, which is effectively what the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is to work around the fact that we can't export the type from |
||
|
||
writer.openBlock("export class $L implements $T {", "}", | ||
typeName, | ||
symbol, | ||
() -> { | ||
|
||
writer.write("readonly name = $S;", errorShape.getId().getName()); | ||
writer.write("readonly $$fault = $S;", errorShape.expectTrait(ErrorTrait.class).getValue()); | ||
//TODO: remove me when we fix where $metadata goes | ||
writer.write("readonly $$metadata = {};"); | ||
if (!errorShape.members().isEmpty()) { | ||
writer.write(""); | ||
StructuredMemberWriter structuredMemberWriter = new StructuredMemberWriter( | ||
model, symbolProvider, errorShape.getAllMembers().values()); | ||
structuredMemberWriter.writeMembers(writer, errorShape); | ||
writer.write(""); | ||
structuredMemberWriter.writeConstructor(writer, errorShape); | ||
} | ||
}); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should add a comment explaining why this is here, particularly when it's not being done for clients.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay, I added it