Skip to content

Commit 8fe1b54

Browse files
feat(codegen): add query compatible header error code handling for JSON protocols (#3972)
1 parent 6a7fea4 commit 8fe1b54

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

codegen/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ allprojects {
3131
version = "0.12.0"
3232
}
3333

34-
extra["smithyVersion"] = "[1.25.0,1.26.0["
34+
extra["smithyVersion"] = "[1.25.2,1.26.0["
3535

3636
// The root project doesn't produce a JAR.
3737
tasks["jar"].enabled = false

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsProtocolUtils.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import software.amazon.smithy.typescript.codegen.integration.HttpProtocolGeneratorUtils;
4444
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator;
4545
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator.GenerationContext;
46+
import software.amazon.smithy.utils.IoUtils;
4647
import software.amazon.smithy.utils.SmithyInternalApi;
4748

4849
/**
@@ -117,6 +118,13 @@ static void generateJsonParseBody(GenerationContext context) {
117118
writer.write("");
118119
}
119120

121+
static void generateJsonParseBodyWithQueryHeader(GenerationContext context) {
122+
TypeScriptWriter writer = context.getWriter();
123+
writer.addImport("HeaderBag", "__HeaderBag", "@aws-sdk/types");
124+
writer.write(IoUtils.readUtf8Resource(
125+
AwsProtocolUtils.class, "populate-body-with-query-compatibility-code-stub.ts"));
126+
}
127+
120128
/**
121129
* Writes a response body parser function for JSON errors. This
122130
* will populate message field in parsed object, if it's not present.

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/JsonRpcProtocolGenerator.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package software.amazon.smithy.aws.typescript.codegen;
1717

1818
import java.util.Set;
19+
import software.amazon.smithy.aws.traits.protocols.AwsQueryCompatibleTrait;
1920
import software.amazon.smithy.model.shapes.OperationShape;
2021
import software.amazon.smithy.model.shapes.ServiceShape;
2122
import software.amazon.smithy.model.shapes.Shape;
@@ -86,6 +87,10 @@ public void generateSharedComponents(GenerationContext context) {
8687
TypeScriptWriter writer = context.getWriter();
8788
writer.addUseImports(getApplicationProtocol().getResponseType());
8889
writer.write(IoUtils.readUtf8Resource(getClass(), "load-json-error-code-stub.ts"));
90+
91+
if (context.getService().hasTrait(AwsQueryCompatibleTrait.class)) {
92+
AwsProtocolUtils.generateJsonParseBodyWithQueryHeader(context);
93+
}
8994
}
9095

9196
@Override
@@ -128,6 +133,14 @@ protected boolean writeUndefinedInputBody(GenerationContext context, OperationSh
128133
protected void writeErrorCodeParser(GenerationContext context) {
129134
TypeScriptWriter writer = context.getWriter();
130135

136+
if (context.getService().hasTrait(AwsQueryCompatibleTrait.class)) {
137+
// Populate parsedOutput.body with 'Code' and 'Type' fields
138+
// "x-amzn-query-error" header is available when AwsQueryCompatibleTrait is applied to a service
139+
// The header value contains query error Code and Type joined by ';'
140+
// E.g. "MalformedInput;Sender" or "InternalFailure;Receiver"
141+
writer.write("populateBodyWithQueryCompatibility(parsedOutput, output.headers);");
142+
}
143+
131144
// Outsource error code parsing since it's complex for this protocol.
132145
writer.write("const errorCode = loadRestJsonErrorCode(output, parsedOutput.body);");
133146
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const populateBodyWithQueryCompatibility = (parsedOutput: any, headers: __HeaderBag) => {
2+
const queryErrorHeader = headers["x-amzn-query-error"];
3+
if (parsedOutput.body !== undefined && queryErrorHeader != null) {
4+
const codeAndType = queryErrorHeader.split(";");
5+
parsedOutput.body.Code = codeAndType[0];
6+
parsedOutput.body.Type = codeAndType[1];
7+
}
8+
};

0 commit comments

Comments
 (0)