@@ -71,6 +71,16 @@ public abstract class HttpBindingProtocolGenerator implements ProtocolGenerator
71
71
private final Set <Shape > serializingDocumentShapes = new TreeSet <>();
72
72
private final Set <Shape > deserializingDocumentShapes = new TreeSet <>();
73
73
private final Set <StructureShape > deserializingErrorShapes = new TreeSet <>();
74
+ private final boolean isErrorCodeInBody ;
75
+
76
+ /**
77
+ * Creates a Http binding protocol generator.
78
+ *
79
+ * @param isErrorCodeInBody A boolean indicates whether error code is located in error response body.
80
+ */
81
+ public HttpBindingProtocolGenerator (boolean isErrorCodeInBody ) {
82
+ this .isErrorCodeInBody = isErrorCodeInBody ;
83
+ }
74
84
75
85
@ Override
76
86
public ApplicationProtocol getApplicationProtocol () {
@@ -595,7 +605,7 @@ private void generateOperationDeserializer(
595
605
596
606
// Write out the error deserialization dispatcher.
597
607
Set <StructureShape > errorShapes = HttpProtocolGeneratorUtils .generateErrorDispatcher (
598
- context , operation , responseType , this ::writeErrorCodeParser , this .isErrorCodeInBody () );
608
+ context , operation , responseType , this ::writeErrorCodeParser , this .isErrorCodeInBody );
599
609
deserializingErrorShapes .addAll (errorShapes );
600
610
}
601
611
@@ -607,13 +617,12 @@ private void generateErrorDeserializer(GenerationContext context, StructureShape
607
617
Symbol errorSymbol = symbolProvider .toSymbol (error );
608
618
String errorDeserMethodName = ProtocolGenerator .getDeserFunctionName (errorSymbol ,
609
619
context .getProtocolName ()) + "Response" ;
610
- boolean isBodyParsed = this .isErrorCodeInBody ();
611
620
612
621
writer .openBlock ("const $L = async (\n "
613
622
+ " $L: any,\n "
614
623
+ " context: __SerdeContext\n "
615
624
+ "): Promise<$T> => {" , "};" ,
616
- errorDeserMethodName , isBodyParsed ? "parsedOutput" : "output" , errorSymbol , () -> {
625
+ errorDeserMethodName , this . isErrorCodeInBody ? "parsedOutput" : "output" , errorSymbol , () -> {
617
626
writer .openBlock ("const contents: $T = {" , "};" , errorSymbol , () -> {
618
627
writer .write ("__type: $S," , error .getId ().getName ());
619
628
writer .write ("$$fault: $S," , error .getTrait (ErrorTrait .class ).get ().getValue ());
@@ -624,7 +633,8 @@ private void generateErrorDeserializer(GenerationContext context, StructureShape
624
633
});
625
634
626
635
readHeaders (context , error , bindingIndex );
627
- List <HttpBinding > documentBindings = readErrorResponseBody (context , error , bindingIndex , isBodyParsed );
636
+ List <HttpBinding > documentBindings = readErrorResponseBody (
637
+ context , error , bindingIndex , this .isErrorCodeInBody );
628
638
// Track all shapes bound to the document so their deserializers may be generated.
629
639
documentBindings .forEach (binding -> {
630
640
Shape target = model .expectShape (binding .getMember ().getTarget ());
@@ -912,10 +922,13 @@ private String getNumberOutputParam(Location bindingType, String dataSource, Sha
912
922
* Writes the code that loads an {@code errorCode} String with the content used
913
923
* to dispatch errors to specific serializers.
914
924
*
915
- * <p>Three variables will be in scope:
925
+ * <p>Two variables will be in scope:
916
926
* <ul>
917
- * <li>{@code output}: a value of the HttpResponse type.</li>
918
- * <li>{@code data}: the contents of the response body.</li>
927
+ * <li>{@code errorOutput} or {@code parsedOutput}: a value of the HttpResponse type.
928
+ * {@code errorOutput} is a raw HttpResponse whereas {@code parsedOutput} is a HttpResponse type with
929
+ * body parsed to JavaScript object.
930
+ * The actual value available is determined by {@link #isErrorCodeInBody}
931
+ * </li>
919
932
* <li>{@code context}: the SerdeContext.</li>
920
933
* </ul>
921
934
*
@@ -929,14 +942,6 @@ private String getNumberOutputParam(Location bindingType, String dataSource, Sha
929
942
*/
930
943
protected abstract void writeErrorCodeParser (GenerationContext context );
931
944
932
- /**
933
- * A boolean indicates whether body is collected and parsed in error code parser.
934
- * If so, each error shape deserializer should not parse body again.
935
- *
936
- * @return returns whether the error code exists in response body
937
- */
938
- protected abstract boolean isErrorCodeInBody ();
939
-
940
945
/**
941
946
* Writes the code needed to deserialize the output document of a response.
942
947
*
0 commit comments