|
| 1 | +/* |
| 2 | + * Copyright 2022 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.integration; |
| 17 | + |
| 18 | +import java.nio.file.Paths; |
| 19 | +import java.util.function.BiConsumer; |
| 20 | +import java.util.function.Consumer; |
| 21 | +import software.amazon.smithy.codegen.core.Symbol; |
| 22 | +import software.amazon.smithy.codegen.core.SymbolProvider; |
| 23 | +import software.amazon.smithy.codegen.core.SymbolReference; |
| 24 | +import software.amazon.smithy.model.Model; |
| 25 | +import software.amazon.smithy.model.shapes.ServiceShape; |
| 26 | +import software.amazon.smithy.model.traits.ErrorTrait; |
| 27 | +import software.amazon.smithy.typescript.codegen.CodegenUtils; |
| 28 | +import software.amazon.smithy.typescript.codegen.TypeScriptDependency; |
| 29 | +import software.amazon.smithy.typescript.codegen.TypeScriptSettings; |
| 30 | +import software.amazon.smithy.typescript.codegen.TypeScriptWriter; |
| 31 | +import software.amazon.smithy.utils.SmithyInternalApi; |
| 32 | + |
| 33 | +/** |
| 34 | + * Generate the base ServiceException class. |
| 35 | + */ |
| 36 | +@SmithyInternalApi |
| 37 | +public final class AddBaseServiceExceptionClass implements TypeScriptIntegration { |
| 38 | + |
| 39 | + @Override |
| 40 | + public void writeAdditionalFiles( |
| 41 | + TypeScriptSettings settings, |
| 42 | + Model model, |
| 43 | + SymbolProvider symbolProvider, |
| 44 | + BiConsumer<String, Consumer<TypeScriptWriter>> writerFactory |
| 45 | + ) { |
| 46 | + boolean isClientSdk = settings.generateClient(); |
| 47 | + if (isClientSdk) { |
| 48 | + String serviceName = getServiceName(settings, model, symbolProvider); |
| 49 | + String serviceExceptionName = getServiceExceptionName(serviceName); |
| 50 | + writerFactory.accept( |
| 51 | + Paths.get(CodegenUtils.SOURCE_FOLDER, "models", serviceExceptionName + ".ts").toString(), |
| 52 | + writer -> { |
| 53 | + writer.addImport("ServiceException", "__ServiceException", |
| 54 | + TypeScriptDependency.AWS_SMITHY_CLIENT.packageName); |
| 55 | + writer.addImport("ServiceExceptionOptions", "__ServiceExceptionOptions", |
| 56 | + TypeScriptDependency.AWS_SMITHY_CLIENT.packageName); |
| 57 | + writer.writeDocs("Base exception class for all service exceptions from " |
| 58 | + + serviceName + " service."); |
| 59 | + writer.openBlock("export class $L extends __ServiceException {", serviceExceptionName); |
| 60 | + writer.writeDocs("@internal"); |
| 61 | + writer.openBlock("constructor(options: __ServiceExceptionOptions) {"); |
| 62 | + writer.write("super(options);"); |
| 63 | + writer.write("Object.setPrototypeOf(this, $L.prototype);", serviceExceptionName); |
| 64 | + writer.closeBlock("}"); // constructor |
| 65 | + writer.closeBlock("}"); // class |
| 66 | + }); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void writeAdditionalExports( |
| 72 | + TypeScriptSettings settings, |
| 73 | + Model model, |
| 74 | + SymbolProvider symbolProvider, |
| 75 | + TypeScriptWriter writer |
| 76 | + ) { |
| 77 | + boolean isClientSdk = settings.generateClient(); |
| 78 | + if (isClientSdk) { |
| 79 | + String serviceName = getServiceName(settings, model, symbolProvider); |
| 80 | + String serviceExceptionName = getServiceExceptionName(serviceName); |
| 81 | + writer.write("export { $1L } from \"./models/$1L\";", serviceExceptionName); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * For any error shape, add the reference of the base error class to the |
| 87 | + * error symbol's references. In client SDK, the base error class is the |
| 88 | + * service-specific service exception class. In server SDK, the base error |
| 89 | + * class is the ServiceException class from server-common package. |
| 90 | + */ |
| 91 | + @Override |
| 92 | + public SymbolProvider decorateSymbolProvider( |
| 93 | + TypeScriptSettings settings, |
| 94 | + Model model, |
| 95 | + SymbolProvider symbolProvider |
| 96 | + ) { |
| 97 | + return shape -> { |
| 98 | + Symbol symbol = symbolProvider.toSymbol(shape); |
| 99 | + if (shape.hasTrait(ErrorTrait.class)) { |
| 100 | + String serviceName = getServiceName(settings, model, symbolProvider); |
| 101 | + String baseExceptionAlias = "__BaseException"; |
| 102 | + SymbolReference reference; |
| 103 | + if (settings.generateClient()) { |
| 104 | + String serviceExceptionName = getServiceExceptionName(serviceName); |
| 105 | + String namespace = Paths.get(".", "src", "models", serviceExceptionName).toString(); |
| 106 | + Symbol serviceExceptionSymbol = Symbol.builder() |
| 107 | + .name(serviceExceptionName) |
| 108 | + .namespace(namespace, "/") |
| 109 | + .definitionFile(namespace + ".ts").build(); |
| 110 | + reference = SymbolReference.builder() |
| 111 | + .options(SymbolReference.ContextOption.USE) |
| 112 | + .alias(baseExceptionAlias) |
| 113 | + .symbol(serviceExceptionSymbol) |
| 114 | + .build(); |
| 115 | + } else { |
| 116 | + reference = SymbolReference.builder() |
| 117 | + .options(SymbolReference.ContextOption.USE) |
| 118 | + .alias(baseExceptionAlias) |
| 119 | + .symbol(TypeScriptDependency.SERVER_COMMON.createSymbol("ServiceException")) |
| 120 | + .build(); |
| 121 | + } |
| 122 | + return symbol.toBuilder().addReference(reference).build(); |
| 123 | + } |
| 124 | + return symbol; |
| 125 | + }; |
| 126 | + } |
| 127 | + |
| 128 | + private String getServiceName( |
| 129 | + TypeScriptSettings settings, |
| 130 | + Model model, |
| 131 | + SymbolProvider symbolProvider |
| 132 | + ) { |
| 133 | + ServiceShape service = settings.getService(model); |
| 134 | + return symbolProvider.toSymbol(service).getName().replaceAll("(Client)$", ""); |
| 135 | + } |
| 136 | + |
| 137 | + private String getServiceExceptionName(String serviceName) { |
| 138 | + return serviceName + "ServiceException"; |
| 139 | + } |
| 140 | +} |
0 commit comments