Skip to content

Commit

Permalink
fallback to status code for unmodeled error, cleanup error parser
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanZhengYP committed Jun 28, 2022
1 parent 584f7ed commit 213a707
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2889,8 +2889,9 @@ private String getNumberOutputParam(
}

/**
* Writes the code that loads an {@code errorCode} String with the content used
* to dispatch errors to specific serializers.
* Writes the code that loads an optional {@code errorCode} String with the content used
* to dispatch errors to specific serializers. If an error code cannot be load, the code
* must return {@code undefined} so default value can be injected in default case.
*
* <p>Two variables will be in scope:
* <ul>
Expand All @@ -2908,7 +2909,7 @@ private String getNumberOutputParam(
* <p>For example:
*
* <pre>{@code
* errorCode = output.headers["x-amzn-errortype"].split(':')[0];
* const errorCode = output.headers["x-amzn-errortype"].split(':')[0];
* }</pre>
*
* @param context The generation context.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,6 @@ static Set<StructureShape> generateErrorDispatcher(
// Error responses must be at least BaseException interface
SymbolReference baseExceptionReference = getClientBaseException(context);
writer.write("let response: $T;", baseExceptionReference);
writer.write("let errorCode: string = \"UnknownError\";");
errorCodeGenerator.accept(context);
writer.openBlock("switch (errorCode) {", "}", () -> {
// Generate the case statement for each error, invoking the specific deserializer.
Expand Down Expand Up @@ -381,10 +380,14 @@ static Set<StructureShape> generateErrorDispatcher(

// Get the protocol specific error location for retrieving contents.
String errorLocation = bodyErrorLocationModifier.apply(context, "parsedBody");
writer.write("const $$metadata = deserializeMetadata(output);");
writer.write("const statusCode = $$metadata.httpStatusCode ? $$metadata.httpStatusCode"
+ " + '' : undefined;");
writer.openBlock("response = new $T({", "});", baseExceptionReference, () -> {
writer.write("name: $1L.code || $1L.Code || errorCode,", errorLocation);
writer.write("name: $1L.code || $1L.Code || errorCode || statusCode || 'UnknowError',",
errorLocation);
writer.write("$$fault: \"client\",");
writer.write("$$metadata: deserializeMetadata(output)");
writer.write("$$metadata");
});
writer.addImport("decorateServiceException", "__decorateServiceException",
TypeScriptDependency.AWS_SMITHY_CLIENT.packageName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,9 @@ private void readResponseBody(GenerationContext context, OperationShape operatio
}

/**
* Writes the code that loads an {@code errorCode} String with the content used
* to dispatch errors to specific serializers.
* Writes the code that loads an optional {@code errorCode} String with the content used
* to dispatch errors to specific serializers. If an error code cannot be load, the code
* must return {@code undefined} so default value can be injected in default case.
*
* <p>Two variables will be in scope:
* <ul>
Expand All @@ -463,7 +464,7 @@ private void readResponseBody(GenerationContext context, OperationShape operatio
* <p>For example:
*
* <pre>{@code
* errorCode = output.headers["x-amzn-errortype"].split(':')[0];
* const errorCode = output.headers["x-amzn-errortype"].split(':')[0];
* }</pre>
*
* @param context The generation context.
Expand Down

0 comments on commit 213a707

Please sign in to comment.