Skip to content

Commit 636c99d

Browse files
AllanZhengYPsrchase
authored andcommitted
fallback to status code for unmodeled error, cleanup error parser (smithy-lang#565)
1 parent 34e9136 commit 636c99d

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2911,8 +2911,9 @@ private String getNumberOutputParam(
29112911
}
29122912

29132913
/**
2914-
* Writes the code that loads an {@code errorCode} String with the content used
2915-
* to dispatch errors to specific serializers.
2914+
* Writes the code that loads an optional {@code errorCode} String with the content used
2915+
* to dispatch errors to specific serializers. If an error code cannot be load, the code
2916+
* must return {@code undefined} so default value can be injected in default case.
29162917
*
29172918
* <p>Two variables will be in scope:
29182919
* <ul>
@@ -2930,7 +2931,7 @@ private String getNumberOutputParam(
29302931
* <p>For example:
29312932
*
29322933
* <pre>{@code
2933-
* errorCode = output.headers["x-amzn-errortype"].split(':')[0];
2934+
* const errorCode = output.headers["x-amzn-errortype"].split(':')[0];
29342935
* }</pre>
29352936
*
29362937
* @param context The generation context.

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpProtocolGeneratorUtils.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,6 @@ static Set<StructureShape> generateErrorDispatcher(
349349
// Error responses must be at least BaseException interface
350350
SymbolReference baseExceptionReference = getClientBaseException(context);
351351
writer.write("let response: $T;", baseExceptionReference);
352-
writer.write("let errorCode: string = \"UnknownError\";");
353352
errorCodeGenerator.accept(context);
354353
writer.openBlock("switch (errorCode) {", "}", () -> {
355354
// Generate the case statement for each error, invoking the specific deserializer.
@@ -381,10 +380,14 @@ static Set<StructureShape> generateErrorDispatcher(
381380

382381
// Get the protocol specific error location for retrieving contents.
383382
String errorLocation = bodyErrorLocationModifier.apply(context, "parsedBody");
383+
writer.write("const $$metadata = deserializeMetadata(output);");
384+
writer.write("const statusCode = $$metadata.httpStatusCode ? $$metadata.httpStatusCode"
385+
+ " + '' : undefined;");
384386
writer.openBlock("response = new $T({", "});", baseExceptionReference, () -> {
385-
writer.write("name: $1L.code || $1L.Code || errorCode,", errorLocation);
387+
writer.write("name: $1L.code || $1L.Code || errorCode || statusCode || 'UnknowError',",
388+
errorLocation);
386389
writer.write("$$fault: \"client\",");
387-
writer.write("$$metadata: deserializeMetadata(output)");
390+
writer.write("$$metadata");
388391
});
389392
writer.addImport("decorateServiceException", "__decorateServiceException",
390393
TypeScriptDependency.AWS_SMITHY_CLIENT.packageName);

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpRpcProtocolGenerator.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,9 @@ private void readResponseBody(GenerationContext context, OperationShape operatio
444444
}
445445

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

0 commit comments

Comments
 (0)