@@ -49,6 +49,11 @@ static void generateServiceHandler(SymbolProvider symbolProvider,
49
49
writer .addImport ("ServiceHandler" , null , "@aws-smithy/server-common" );
50
50
writer .addImport ("Mux" , null , "@aws-smithy/server-common" );
51
51
writer .addImport ("OperationSerializer" , null , "@aws-smithy/server-common" );
52
+ writer .addImport ("UnknownOperationException" , null , "@aws-smithy/server-common" );
53
+ writer .addImport ("InternalFailureException" , null , "@aws-smithy/server-common" );
54
+ writer .addImport ("SerializationException" , null , "@aws-smithy/server-common" );
55
+ writer .addImport ("SmithyFrameworkException" , null , "@aws-smithy/server-common" );
56
+ writer .addImport ("SerdeContext" , null , "@aws-sdk/types" );
52
57
writer .addImport ("NodeHttpHandler" , null , "@aws-sdk/node-http-handler" );
53
58
writer .addImport ("streamCollector" , null , "@aws-sdk/node-http-handler" );
54
59
writer .addImport ("fromBase64" , null , "@aws-sdk/util-base64-node" );
@@ -68,6 +73,8 @@ static void generateServiceHandler(SymbolProvider symbolProvider,
68
73
writer .write ("private mux: Mux<$S, $T>;" , serviceShape .getId ().getName (), operationsType );
69
74
writer .write ("private serializerFactory: <T extends $T>(operation: T) => "
70
75
+ "OperationSerializer<$T, T, SmithyException>;" , operationsType , serviceSymbol );
76
+ writer .write ("private serializeFrameworkException: (e: SmithyFrameworkException, "
77
+ + "ctx: Omit<SerdeContext, 'endpoint'>) => Promise<HttpResponse>;" );
71
78
writer .openBlock ("private serdeContextBase = {" , "};" , () -> {
72
79
writer .write ("base64Encoder: toBase64," );
73
80
writer .write ("base64Decoder: fromBase64," );
@@ -87,51 +94,66 @@ static void generateServiceHandler(SymbolProvider symbolProvider,
87
94
writer .write ("operation in $T that " , serviceSymbol );
88
95
writer .writeInline (" " )
89
96
.write ("handles deserialization of requests and serialization of responses" );
97
+ writer .write ("@param serializeFrameworkException A function that can serialize "
98
+ + "{@link SmithyFrameworkException}s" );
90
99
});
91
- writer .openBlock ("constructor(service: $1T, "
92
- + "mux: Mux<$3S, $2T>, "
93
- + "serializerFactory: <T extends $2T>(op: T) => "
94
- + "OperationSerializer<$1T, T, SmithyException>) {" , "}" ,
95
- serviceSymbol , operationsType , serviceShape .getId ().getName (), () -> {
96
- writer .write ("this.service = service;" );
97
- writer .write ("this.mux = mux;" );
98
- writer .write ("this.serializerFactory = serializerFactory;" );
100
+ writer .openBlock ("constructor(" , ") {" , () -> {
101
+ writer .write ("service: $T," , serviceSymbol );
102
+ writer .write ("mux: Mux<$S, $T>," , serviceShape .getId ().getName (), operationsType );
103
+ writer .write ("serializerFactory:<T extends $T>(op: T) => OperationSerializer<$T, T, SmithyException>," ,
104
+ operationsType , serviceSymbol );
105
+ writer .write ("serializeFrameworkException: (e: SmithyFrameworkException, ctx: Omit<SerdeContext, "
106
+ + "'endpoint'>) => Promise<HttpResponse>" );
99
107
});
108
+ writer .indent ();
109
+ writer .write ("this.service = service;" );
110
+ writer .write ("this.mux = mux;" );
111
+ writer .write ("this.serializerFactory = serializerFactory;" );
112
+ writer .write ("this.serializeFrameworkException = serializeFrameworkException;" );
113
+ writer .closeBlock ("}" );
100
114
writer .openBlock ("async handle(request: HttpRequest): Promise<HttpResponse> {" , "}" , () -> {
101
115
writer .write ("const target = this.mux.match(request);" );
102
116
writer .openBlock ("if (target === undefined) {" , "}" , () -> {
103
- writer .write ("throw new Error(`Could not match any operation to $${request.method} "
104
- + "$${request.path} $${JSON.stringify(request.query)}` );" );
117
+ writer .write ("return serializeFrameworkException( new UnknownOperationException(), "
118
+ + "this.serdeContextBase );" );
105
119
});
106
120
writer .openBlock ("switch (target.operation) {" , "}" , () -> {
107
121
for (OperationShape operation : operations ) {
108
- generateHandlerCase (writer , serviceSymbol , operation , symbolProvider .toSymbol (operation ));
122
+ generateHandlerCase (writer , operation , symbolProvider .toSymbol (operation ));
109
123
}
110
124
});
111
125
});
112
126
});
113
127
}
114
128
115
129
private static void generateHandlerCase (TypeScriptWriter writer ,
116
- Symbol serviceSymbol ,
117
130
Shape operationShape ,
118
131
Symbol operationSymbol ) {
119
132
String opName = operationShape .getId ().getName ();
120
133
writer .openBlock ("case $S : {" , "}" , opName , () -> {
121
134
writer .write ("let serializer = this.serializerFactory($S);" , opName );
122
- writer .openBlock ("try {" , "} catch(error: unknown) {" , () -> {
123
- writer .openBlock ("let input = await serializer.deserialize(request, {" , "});" , () -> {
135
+ writer .write ("let input;" );
136
+ writer .openBlock ("try {" , "} catch (error: unknown) {" , () -> {
137
+ writer .openBlock ("input = await serializer.deserialize(request, {" , "});" , () -> {
124
138
writer .write ("endpoint: () => Promise.resolve(request), ...this.serdeContextBase" );
125
139
});
126
- writer .write ("let output = this.service.$L(input, request);" , operationSymbol .getName ());
140
+ });
141
+ writer .indent ();
142
+ writer .write ("return this.serializeFrameworkException(new SerializationException(), "
143
+ + "this.serdeContextBase);" );
144
+ writer .closeBlock ("}" );
145
+ writer .openBlock ("try {" , "} catch(error: unknown) {" , () -> {
146
+ writer .write ("let output = await this.service.$L(input, request);" , operationSymbol .getName ());
127
147
writer .write ("return serializer.serialize(output, this.serdeContextBase);" );
128
148
});
129
- writer .openBlock ("" , "}" , () -> {
130
- writer .openBlock ("if (serializer.isOperationError(error)) {" , "}" , () -> {
131
- writer .write ("return serializer.serializeError(error, this.serdeContextBase);" );
132
- });
133
- writer .write ("throw error;" );
149
+ writer .indent ();
150
+ writer .openBlock ("if (serializer.isOperationError(error)) {" , "}" , () -> {
151
+ writer .write ("return serializer.serializeError(error, this.serdeContextBase);" );
134
152
});
153
+ writer .write ("console.log('Received an unexpected error', error);" );
154
+ writer .write ("return this.serializeFrameworkException(new InternalFailureException(), "
155
+ + "this.serdeContextBase);" );
156
+ writer .closeBlock ("}" );
135
157
});
136
158
}
137
159
0 commit comments