@@ -49,44 +49,54 @@ public struct OpenAPILambdaHandler<L: OpenAPILambda>: LambdaHandler {
49
49
50
50
// by default return HTTP 500
51
51
var lambdaResponse : OpenAPILambdaResponse = ( HTTPResponse ( status: . internalServerError) , " unknown error " )
52
-
52
+
53
53
do {
54
54
// convert Lambda event source to OpenAPILambdaRequest
55
55
let request = try lambda. request ( context: context, from: request)
56
-
56
+
57
57
// route the request to find the handlers and extract the paramaters
58
58
let ( handler, parameters) = try await router. route ( method: request. 0 . method, path: request. 0 . path!)
59
-
59
+
60
60
// call the request handler (and extract the HTTPRequest and HTTPBody)
61
61
let httpRequest = request. 0
62
62
let httpBody = HTTPBody ( stringLiteral: request. 1 ?? " " )
63
63
let response = try await handler ( httpRequest, httpBody, ServerRequestMetadata ( pathParameters: parameters) )
64
-
64
+
65
65
// transform the response to an OpenAPILambdaResponse
66
66
let maxPayloadSize = 10 * 1024 * 1024 // APIGateway payload is 10M max
67
67
let body : String ? = try ? await String ( collecting: response. 1 ?? " " , upTo: maxPayloadSize)
68
68
lambdaResponse = ( response. 0 , body)
69
-
70
- } catch OpenAPILambdaRouterError . noHandlerForPath( let path) {
71
-
69
+
70
+ }
71
+ catch OpenAPILambdaRouterError . noHandlerForPath( let path) {
72
+
72
73
// There is no hadler registered for this path. This is a programming error.
73
- lambdaResponse = ( HTTPResponse ( status: . internalServerError) , " There is no OpenAPI handler registered for the path \( path) " )
74
+ lambdaResponse = (
75
+ HTTPResponse ( status: . internalServerError) ,
76
+ " There is no OpenAPI handler registered for the path \( path) "
77
+ )
78
+
79
+ }
80
+ catch OpenAPILambdaRouterError . noRouteForMethod( let method) {
74
81
75
- } catch OpenAPILambdaRouterError . noRouteForMethod( let method) {
76
-
77
82
// There is no hadler registered for this path. This is a programming error.
78
83
lambdaResponse = ( HTTPResponse ( status: . notFound) , " There is no route registered for the method \( method) " )
79
84
80
- } catch OpenAPILambdaRouterError . noRouteForPath( let path) {
81
-
85
+ }
86
+ catch OpenAPILambdaRouterError . noRouteForPath( let path) {
87
+
82
88
// There is no hadler registered for this path. This is a programming error.
83
89
lambdaResponse = ( HTTPResponse ( status: . notFound) , " There is no route registered for the path \( path) " )
84
90
85
- } catch OpenAPILambdaHttpError . invalidMethod( let method) {
86
-
91
+ }
92
+ catch OpenAPILambdaHttpError . invalidMethod( let method) {
93
+
87
94
// the APIGateway HTTP verb is rejected by HTTTypes HTTPRequest.Method => HTTP 500
88
95
// this should never happen
89
- lambdaResponse = ( HTTPResponse ( status: . internalServerError) , " Type mismatch between APIGatewayV2 and HTTPRequest.Method. \( method) verb is rejected by HTTPRequest.Method 🤷♂️ " )
96
+ lambdaResponse = (
97
+ HTTPResponse ( status: . internalServerError) ,
98
+ " Type mismatch between APIGatewayV2 and HTTPRequest.Method. \( method) verb is rejected by HTTPRequest.Method 🤷♂️ "
99
+ )
90
100
91
101
}
92
102
0 commit comments