|
| 1 | +/* |
| 2 | + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.smithy.typescript.codegen; |
| 17 | + |
| 18 | +import software.amazon.smithy.codegen.core.Symbol; |
| 19 | +import software.amazon.smithy.codegen.core.SymbolProvider; |
| 20 | +import software.amazon.smithy.model.Model; |
| 21 | +import software.amazon.smithy.model.shapes.StructureShape; |
| 22 | +import software.amazon.smithy.model.traits.ErrorTrait; |
| 23 | + |
| 24 | +/** |
| 25 | + * Generates convenience error types for servers, since service developers will throw a modeled exception directly, |
| 26 | + * while clients typically care only about catching them. |
| 27 | + */ |
| 28 | +final class ServerErrorGenerator implements Runnable { |
| 29 | + |
| 30 | + private final TypeScriptSettings settings; |
| 31 | + private final Model model; |
| 32 | + private final StructureShape errorShape; |
| 33 | + private final SymbolProvider symbolProvider; |
| 34 | + private final TypeScriptWriter writer; |
| 35 | + |
| 36 | + ServerErrorGenerator(TypeScriptSettings settings, |
| 37 | + Model model, |
| 38 | + StructureShape errorShape, |
| 39 | + SymbolProvider symbolProvider, |
| 40 | + TypeScriptWriter writer) { |
| 41 | + this.settings = settings; |
| 42 | + this.model = model; |
| 43 | + this.errorShape = errorShape; |
| 44 | + this.symbolProvider = symbolProvider; |
| 45 | + this.writer = writer; |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + @Override |
| 50 | + public void run() { |
| 51 | + Symbol symbol = symbolProvider.toSymbol(errorShape); |
| 52 | + |
| 53 | + // Did not add this as a symbol to the error shape since these should not be used by anyone but the user. |
| 54 | + String typeName = symbol.getName() + "Error"; |
| 55 | + |
| 56 | + writer.openBlock("export class $L implements $T {", "}", |
| 57 | + typeName, |
| 58 | + symbol, |
| 59 | + () -> { |
| 60 | + |
| 61 | + writer.write("readonly name = $S;", errorShape.getId().getName()); |
| 62 | + writer.write("readonly $$fault = $S;", errorShape.expectTrait(ErrorTrait.class).getValue()); |
| 63 | + //TODO: remove me when we fix where $metadata goes |
| 64 | + writer.write("readonly $$metadata = {};"); |
| 65 | + if (!errorShape.members().isEmpty()) { |
| 66 | + writer.write(""); |
| 67 | + StructuredMemberWriter structuredMemberWriter = new StructuredMemberWriter( |
| 68 | + model, symbolProvider, errorShape.getAllMembers().values()); |
| 69 | + structuredMemberWriter.writeMembers(writer, errorShape); |
| 70 | + writer.write(""); |
| 71 | + structuredMemberWriter.writeConstructor(writer, errorShape); |
| 72 | + } |
| 73 | + }); |
| 74 | + } |
| 75 | +} |
0 commit comments