Skip to content

Commit

Permalink
fix: parse body only once for error response (aws#694)
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanZhengYP authored and trivikr committed Mar 24, 2020
1 parent afb7c9e commit 38ffa88
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ static void generateJsonParseBody(GenerationContext context) {
// Include a JSON body parser used to deserialize documents from HTTP responses.
writer.addImport("SerdeContext", "__SerdeContext", "@aws-sdk/types");
writer.openBlock("const parseBody = (streamBody: any, context: __SerdeContext): any => {", "};", () -> {
writer.openBlock("return context.streamCollector(streamBody).then((body: any) => {", "});", () -> {
writer.write("const encoded = context.utf8Encoder(body);");
writer.openBlock("return collectBodyString(streamBody, context).then(encoded => {", "});", () -> {
writer.openBlock("if (encoded.length) {", "}", () -> {
writer.write("return JSON.parse(encoded);");
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@
*/
abstract class JsonRpcProtocolGenerator extends HttpRpcProtocolGenerator {

/**
* Creates a AWS JSON RPC protocol generator.
*/
JsonRpcProtocolGenerator() {
super(true);
}

protected Format getDocumentTimestampFormat() {
return Format.EPOCH_SECONDS;
}
Expand Down Expand Up @@ -96,8 +103,8 @@ private DocumentMemberSerVisitor getMemberSerVisitor(GenerationContext context,
@Override
protected void writeErrorCodeParser(GenerationContext context) {
TypeScriptWriter writer = context.getWriter();

writer.write("const errorTypeParts: String = data[\"__type\"].split('#');");
// parsedOutput is in scope because HttpRpcProtocolGenerator.isErrorCodeInBody is true
writer.write("const errorTypeParts: String = parsedOutput.body[\"__type\"].split('#');");
writer.write("errorCode = (errorTypeParts[1] === undefined) ? errorTypeParts[0] : errorTypeParts[1];");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
* @see <a href="https://awslabs.github.io/smithy/spec/http.html">Smithy HTTP protocol bindings.</a>
*/
abstract class RestJsonProtocolGenerator extends HttpBindingProtocolGenerator {
/**
* Creates a AWS JSON RPC protocol generator.
*/
RestJsonProtocolGenerator() {
super(false);
}

@Override
protected TimestampFormatTrait.Format getDocumentTimestampFormat() {
Expand Down

0 comments on commit 38ffa88

Please sign in to comment.