Skip to content

Commit

Permalink
feat(codegen): add query compatible header error code handling for JS…
Browse files Browse the repository at this point in the history
…ON protocols
  • Loading branch information
AndrewFossAWS committed Sep 27, 2022
1 parent 7f19c17 commit 1086d6f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion codegen/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ allprojects {
version = "0.12.0"
}

extra["smithyVersion"] = "[1.25.0,1.26.0["
extra["smithyVersion"] = "[1.25.1,1.26.0["

// The root project doesn't produce a JAR.
tasks["jar"].enabled = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import software.amazon.smithy.typescript.codegen.integration.HttpProtocolGeneratorUtils;
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator;
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator.GenerationContext;
import software.amazon.smithy.utils.IoUtils;
import software.amazon.smithy.utils.SmithyInternalApi;

/**
Expand Down Expand Up @@ -117,6 +118,15 @@ static void generateJsonParseBody(GenerationContext context) {
writer.write("");
}

static void generateJsonParseBodyWithQueryHeader(GenerationContext context) {
TypeScriptWriter writer = context.getWriter();
writer.addImport("HeaderBag", "__HeaderBag", "@aws-sdk/types");
writer.addImport("HttpResponse", "__HttpResponse", "@aws-sdk/protocol-http");
writer.addImport("SerdeContext", "__SerdeContext", "@aws-sdk/types");
writer.write(IoUtils.readUtf8Resource(
AwsProtocolUtils.class, "populate-body-with-query-compatibility-code-stub.ts"));
}

/**
* Writes a response body parser function for XML protocols. This
* will parse a present body after converting it to utf-8.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package software.amazon.smithy.aws.typescript.codegen;

import java.util.Set;
import software.amazon.smithy.aws.traits.protocols.AwsQueryCompatibleTrait;
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.Shape;
Expand Down Expand Up @@ -85,6 +86,10 @@ public void generateSharedComponents(GenerationContext context) {
TypeScriptWriter writer = context.getWriter();
writer.addUseImports(getApplicationProtocol().getResponseType());
writer.write(IoUtils.readUtf8Resource(getClass(), "load-json-error-code-stub.ts"));

if (context.getService().hasTrait(AwsQueryCompatibleTrait.class)) {
AwsProtocolUtils.generateJsonParseBodyWithQueryHeader(context);
}
}

@Override
Expand Down Expand Up @@ -127,6 +132,14 @@ protected boolean writeUndefinedInputBody(GenerationContext context, OperationSh
protected void writeErrorCodeParser(GenerationContext context) {
TypeScriptWriter writer = context.getWriter();

if (context.getService().hasTrait(AwsQueryCompatibleTrait.class)) {
// Populate parsedOutput.body with 'Code' and 'Type' fields
// "x-amzn-query-error" header is available when AwsQueryCompatibleTrait is applied to a service
// The header value contains query error Code and Type joined by ';'
// E.g. "MalformedInput;Sender" or "InternalFailure;Receiver"
writer.write("populateBodyWithQueryCompatibility(parsedOutput, output.headers);");
}

// Outsource error code parsing since it's complex for this protocol.
writer.write("const errorCode = loadRestJsonErrorCode(output, parsedOutput.body);");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const populateBodyWithQueryCompatibility = (parsedOutput: any, headers: __HeaderBag) => {
const queryErrorHeader = headers["x-amzn-query-error"];
if (parsedOutput.body !== undefined && queryErrorHeader != null) {
const codeAndType = queryErrorHeader.split(";");
parsedOutput.body.Code = codeAndType[0];
parsedOutput.body.Type = codeAndType[1];
}
};

0 comments on commit 1086d6f

Please sign in to comment.