@@ -149,6 +149,7 @@ private enum LocalLambda {
149
149
case . waitingForLambdaRequest, . waitingForLambdaResponse:
150
150
Self . invocations. append ( invocation)
151
151
}
152
+
152
153
// /next endpoint is called by the lambda polling for work
153
154
case ( . GET, let url) where url. hasSuffix ( Consts . getNextInvocationURLSuffix) :
154
155
// check if our server is in the correct state
@@ -168,7 +169,7 @@ private enum LocalLambda {
168
169
switch result {
169
170
case . failure( let error) :
170
171
self . logger. error ( " invocation error: \( error) " )
171
- self . writeResponse ( context: context, response : . init ( status: . internalServerError) )
172
+ self . writeResponse ( context: context, status: . internalServerError)
172
173
case . success( let invocation) :
173
174
Self . invocationState = . waitingForLambdaResponse( invocation)
174
175
self . writeResponse ( context: context, response: invocation. makeResponse ( ) )
@@ -180,33 +181,61 @@ private enum LocalLambda {
180
181
Self . invocationState = . waitingForLambdaResponse( invocation)
181
182
self . writeResponse ( context: context, response: invocation. makeResponse ( ) )
182
183
}
184
+
183
185
// :requestID/response endpoint is called by the lambda posting the response
184
186
case ( . POST, let url) where url. hasSuffix ( Consts . postResponseURLSuffix) :
185
187
let parts = request. head. uri. split ( separator: " / " )
186
188
guard let requestID = parts. count > 2 ? String ( parts [ parts. count - 2 ] ) : nil else {
187
189
// the request is malformed, since we were expecting a requestId in the path
188
- return self . writeResponse ( context: context, response : . init ( status: . badRequest) )
190
+ return self . writeResponse ( context: context, status: . badRequest)
189
191
}
190
192
guard case . waitingForLambdaResponse( let invocation) = Self . invocationState else {
191
193
// a response was send, but we did not expect to receive one
192
194
self . logger. error ( " invalid invocation state \( Self . invocationState) " )
193
- return self . writeResponse ( context: context, response : . init ( status: . unprocessableEntity) )
195
+ return self . writeResponse ( context: context, status: . unprocessableEntity)
194
196
}
195
197
guard requestID == invocation. requestID else {
196
198
// the request's requestId is not matching the one we are expecting
197
199
self . logger. error ( " invalid invocation state request ID \( requestID) does not match expected \( invocation. requestID) " )
198
- return self . writeResponse ( context: context, response : . init ( status: . badRequest) )
200
+ return self . writeResponse ( context: context, status: . badRequest)
199
201
}
200
202
201
203
invocation. responsePromise. succeed ( . init( status: . ok, body: request. body) )
202
- self . writeResponse ( context: context, response: . init( status: . accepted) )
204
+ self . writeResponse ( context: context, status: . accepted)
205
+ Self . invocationState = . waitingForLambdaRequest
206
+
207
+ // :requestID/error endpoint is called by the lambda posting an error response
208
+ case ( . POST, let url) where url. hasSuffix ( Consts . postErrorURLSuffix) :
209
+ let parts = request. head. uri. split ( separator: " / " )
210
+ guard let requestID = parts. count > 2 ? String ( parts [ parts. count - 2 ] ) : nil else {
211
+ // the request is malformed, since we were expecting a requestId in the path
212
+ return self . writeResponse ( context: context, status: . badRequest)
213
+ }
214
+ guard case . waitingForLambdaResponse( let invocation) = Self . invocationState else {
215
+ // a response was send, but we did not expect to receive one
216
+ self . logger. error ( " invalid invocation state \( Self . invocationState) " )
217
+ return self . writeResponse ( context: context, status: . unprocessableEntity)
218
+ }
219
+ guard requestID == invocation. requestID else {
220
+ // the request's requestId is not matching the one we are expecting
221
+ self . logger. error ( " invalid invocation state request ID \( requestID) does not match expected \( invocation. requestID) " )
222
+ return self . writeResponse ( context: context, status: . badRequest)
223
+ }
224
+
225
+ invocation. responsePromise. succeed ( . init( status: . internalServerError, body: request. body) )
226
+ self . writeResponse ( context: context, status: . accepted)
203
227
Self . invocationState = . waitingForLambdaRequest
228
+
204
229
// unknown call
205
230
default :
206
- self . writeResponse ( context: context, response : . init ( status: . notFound) )
231
+ self . writeResponse ( context: context, status: . notFound)
207
232
}
208
233
}
209
234
235
+ func writeResponse( context: ChannelHandlerContext , status: HTTPResponseStatus ) {
236
+ self . writeResponse ( context: context, response: . init( status: status) )
237
+ }
238
+
210
239
func writeResponse( context: ChannelHandlerContext , response: Response ) {
211
240
var headers = HTTPHeaders ( response. headers ?? [ ] )
212
241
headers. add ( name: " content-length " , value: " \( response. body? . readableBytes ?? 0 ) " )
0 commit comments