Skip to content

Commit 213a707

Browse files
committed
fallback to status code for unmodeled error, cleanup error parser
1 parent 584f7ed commit 213a707

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
@@ -2889,8 +2889,9 @@ private String getNumberOutputParam(
28892889
}
28902890

28912891
/**
2892-
* Writes the code that loads an {@code errorCode} String with the content used
2893-
* to dispatch errors to specific serializers.
2892+
* Writes the code that loads an optional {@code errorCode} String with the content used
2893+
* to dispatch errors to specific serializers. If an error code cannot be load, the code
2894+
* must return {@code undefined} so default value can be injected in default case.
28942895
*
28952896
* <p>Two variables will be in scope:
28962897
* <ul>
@@ -2908,7 +2909,7 @@ private String getNumberOutputParam(
29082909
* <p>For example:
29092910
*
29102911
* <pre>{@code
2911-
* errorCode = output.headers["x-amzn-errortype"].split(':')[0];
2912+
* const errorCode = output.headers["x-amzn-errortype"].split(':')[0];
29122913
* }</pre>
29132914
*
29142915
* @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)