Skip to content

Commit cd40b1e

Browse files
committed
handle RPC error deser when body does not contain error code
1 parent f198929 commit cd40b1e

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -926,9 +926,9 @@ private String getNumberOutputParam(Location bindingType, String dataSource, Sha
926926
*
927927
* <p>Two variables will be in scope:
928928
* <ul>
929-
* <li>{@code errorOutput} or {@code parsedOutput}: a value of the HttpResponse type.
929+
* <li>{@code output} or {@code parsedOutput}: a value of the HttpResponse type.
930930
* <ul>
931-
* <li>{@code errorOutput} is a raw HttpResponse, available when {@code isErrorCodeInBody} is set to
931+
* <li>{@code output} is a raw HttpResponse, available when {@code isErrorCodeInBody} is set to
932932
* {@code false}</li>
933933
* <li>{@code parsedOutput} is a HttpResponse type with body parsed to JavaScript object, available
934934
* when {@code isErrorCodeInBody} is set to {@code true}</li>

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpProtocolGeneratorUtils.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ static void generateCollectBody(GenerationContext context) {
129129

130130
/**
131131
* Writes a function converting the low-level response body stream to utf-8 encoded string. It depends on
132-
* response body stream collector{@link #generateCollectBody(GenerationContext)}.
132+
* response body stream collector {@link #generateCollectBody(GenerationContext)}.
133133
*
134134
* @param context The generation context
135135
*/
@@ -180,14 +180,14 @@ static Set<StructureShape> generateErrorDispatcher(
180180
+ "): Promise<$T> {", "}", errorMethodName, responseType, outputType, () -> {
181181
// Prepare error response for parsing error code. If error code needs to be parsed from response body
182182
// then we collect body and parse it to JS object, otherwise leave the response body as is.
183-
String outputReference = shouldParseErrorBody ? "parsedOutput" : "errorOutput";
184-
writer.openBlock(
185-
"const $L: any = {", "};", outputReference,
186-
() -> {
187-
writer.write("...output,");
188-
writer.write("body: $L,",
189-
shouldParseErrorBody ? "await parseBody(output.body, context)" : "output.body");
190-
});
183+
if (shouldParseErrorBody) {
184+
writer.openBlock(
185+
"const parsedOutput: any = {", "};",
186+
() -> {
187+
writer.write("...output,");
188+
writer.write("body: await parseBody(output.body, context)");
189+
});
190+
}
191191

192192
// Error responses must be at least SmithyException and MetadataBearer implementations.
193193
writer.addImport("SmithyException", "__SmithyException",
@@ -209,7 +209,7 @@ static Set<StructureShape> generateErrorDispatcher(
209209
writer.openBlock("case $S:\ncase $S:", " break;", errorId.getName(), errorId.toString(), () -> {
210210
// Dispatch to the error deserialization function.
211211
writer.write("response = await $L($L, context);",
212-
errorDeserMethodName, outputReference);
212+
errorDeserMethodName, shouldParseErrorBody ? "parsedOutput" : "output");
213213
});
214214
});
215215

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpRpcProtocolGenerator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ private void generateErrorDeserializer(GenerationContext context, StructureShape
291291
writer.write("const body = $L.body", outputReference);
292292
} else {
293293
// If error node not in body, error body is not parsed in dispatcher.
294-
writer.write("const body = parsedBody($L.body, context);", outputReference);
294+
writer.write("const body = parseBody($L.body, context);", outputReference);
295295
}
296296
writer.write("const deserialized: any = $L(body, context);",
297297
ProtocolGenerator.getDeserFunctionName(errorSymbol, context.getProtocolName()));
@@ -333,9 +333,9 @@ private void readResponseBody(GenerationContext context, OperationShape operatio
333333
*
334334
* <p>Two variables will be in scope:
335335
* <ul>
336-
* <li>{@code errorOutput} or {@code parsedOutput}: a value of the HttpResponse type.
336+
* <li>{@code output} or {@code parsedOutput}: a value of the HttpResponse type.
337337
* <ul>
338-
* <li>{@code errorOutput} is a raw HttpResponse, available when {@code isErrorCodeInBody} is set to
338+
* <li>{@code output} is a raw HttpResponse, available when {@code isErrorCodeInBody} is set to
339339
* {@code false}</li>
340340
* <li>{@code parsedOutput} is a HttpResponse type with body parsed to JavaScript object, available
341341
* when {@code isErrorCodeInBody} is set to {@code true}</li>

0 commit comments

Comments
 (0)