Skip to content

Commit

Permalink
chore(codegen): add generateParseErrorBody to populate error message
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Sep 28, 2022
1 parent 3ed75ec commit e9b66b5
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ protected void generateDocumentBodyShapeDeserializers(GenerationContext context,
public void generateSharedComponents(GenerationContext context) {
super.generateSharedComponents(context);
AwsProtocolUtils.generateXmlParseBody(context);
AwsProtocolUtils.generateParseErrorBody(context);
AwsProtocolUtils.generateBuildFormUrlencodedString(context);
AwsProtocolUtils.addItempotencyAutofillImport(context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,27 @@ static void generateJsonParseBody(GenerationContext context) {
writer.write("");
}

/**
* Writes a response body parser function for errors. This
* will populate message field in parsed object, if it's not present.
*
* @param context The generation context.
*/
static void generateParseErrorBody(GenerationContext context) {
TypeScriptWriter writer = context.getWriter();

// Include a JSON body parser used to deserialize documents from HTTP responses.
writer.addImport("SerdeContext", "__SerdeContext", "@aws-sdk/types");
writer.openBlock("const parseErrorBody = (errorBody: any, context: __SerdeContext): "
+ "any => {", "}", () -> {
writer.write("const value = parseBody(errorBody, context);");
writer.write("value.message = value.message ?? value.Message;");
writer.write("return value;");
});

writer.write("");
}

/**
* 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 @@ -85,6 +85,7 @@ protected void generateDocumentBodyShapeDeserializers(GenerationContext context,
public void generateSharedComponents(GenerationContext context) {
super.generateSharedComponents(context);
AwsProtocolUtils.generateXmlParseBody(context);
AwsProtocolUtils.generateParseErrorBody(context);
AwsProtocolUtils.generateBuildFormUrlencodedString(context);
AwsProtocolUtils.addItempotencyAutofillImport(context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ protected void generateDocumentBodyShapeDeserializers(GenerationContext context,
public void generateSharedComponents(GenerationContext context) {
super.generateSharedComponents(context);
AwsProtocolUtils.generateXmlParseBody(context);
AwsProtocolUtils.generateParseErrorBody(context);
AwsProtocolUtils.addItempotencyAutofillImport(context);

TypeScriptWriter writer = context.getWriter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ protected void generateDocumentBodyShapeDeserializers(GenerationContext context,
public void generateSharedComponents(GenerationContext context) {
super.generateSharedComponents(context);
AwsProtocolUtils.generateJsonParseBody(context);
AwsProtocolUtils.generateParseErrorBody(context);
AwsProtocolUtils.addItempotencyAutofillImport(context);

TypeScriptWriter writer = context.getWriter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ protected void generateDocumentBodyShapeDeserializers(GenerationContext context,
public void generateSharedComponents(GenerationContext context) {
super.generateSharedComponents(context);
AwsProtocolUtils.generateJsonParseBody(context);
AwsProtocolUtils.generateParseErrorBody(context);
AwsProtocolUtils.addItempotencyAutofillImport(context);

TypeScriptWriter writer = context.getWriter();
Expand Down

0 comments on commit e9b66b5

Please sign in to comment.