@@ -18,19 +18,30 @@ abstract class RequestHandler : RequestHandler<APIGatewayProxyRequestEvent, APIG
18
18
19
19
open val objectMapper = jacksonObjectMapper()
20
20
21
+ abstract val router: Router
22
+
21
23
@Suppress(" UNCHECKED_CAST" )
22
24
override fun handleRequest (input : APIGatewayProxyRequestEvent , context : Context ): APIGatewayProxyResponseEvent ? {
23
- log.info (" handling request with method '${input.httpMethod} ' and path '${input.path} ' - Accept:${input.acceptHeader()} Content-Type:${input.contentType()} $input " )
25
+ log.debug (" handling request with method '${input.httpMethod} ' and path '${input.path} ' - Accept:${input.acceptHeader()} Content-Type:${input.contentType()} $input " )
24
26
val routes = router.routes as List <RouterFunction <Any , Any >>
25
27
val matchResults: List <RequestMatchResult > = routes.map { routerFunction: RouterFunction <Any , Any > ->
26
28
val matchResult = routerFunction.requestPredicate.match(input)
27
- log.info (" match result for route '$routerFunction ' is '$matchResult '" )
29
+ log.debug (" match result for route '$routerFunction ' is '$matchResult '" )
28
30
if (matchResult.match) {
29
31
val handler: HandlerFunction <Any , Any > = routerFunction.handler
30
32
val requestBody = deserializeRequest(handler, input)
31
33
val request = Request (input, requestBody, routerFunction.requestPredicate.pathPattern)
32
- val response = router.filter.then(handler as HandlerFunction <* , * >).invoke(request)
33
- return createResponse(input, response)
34
+ return try {
35
+ val response = router.filter.then(handler as HandlerFunction <* , * >).invoke(request)
36
+ createResponse(input, response)
37
+ } catch (e: RuntimeException ) {
38
+ when (e) {
39
+ is ApiException -> createErrorResponse(input, e)
40
+ .also { log.info(" Caught api error while handling ${input.httpMethod} ${input.path} - $e " ) }
41
+ else -> createInternalServerErrorResponse(input, e)
42
+ .also { log.error(" Caught exception handling ${input.httpMethod} ${input.path} - $e " , e) }
43
+ }
44
+ }
34
45
}
35
46
36
47
matchResult
@@ -88,8 +99,6 @@ abstract class RequestHandler : RequestHandler<APIGatewayProxyRequestEvent, APIG
88
99
)
89
100
}
90
101
91
- abstract val router: Router
92
-
93
102
open fun createErrorResponse (input : APIGatewayProxyRequestEvent , ex : ApiException ): APIGatewayProxyResponseEvent =
94
103
APIGatewayProxyResponseEvent ()
95
104
.withBody(objectMapper.writeValueAsString(mapOf (
@@ -100,6 +109,15 @@ abstract class RequestHandler : RequestHandler<APIGatewayProxyRequestEvent, APIG
100
109
.withStatusCode(ex.httpResponseStatus)
101
110
.withHeaders(mapOf (" Content-Type" to " application/json" ))
102
111
112
+ open fun createInternalServerErrorResponse (input : APIGatewayProxyRequestEvent , ex : java.lang.RuntimeException ): APIGatewayProxyResponseEvent =
113
+ APIGatewayProxyResponseEvent ()
114
+ .withBody(objectMapper.writeValueAsString(mapOf (
115
+ " message" to ex.message,
116
+ " code" to " INTERNAL_SERVER_ERROR"
117
+ )))
118
+ .withStatusCode(500 )
119
+ .withHeaders(mapOf (" Content-Type" to " application/json" ))
120
+
103
121
open fun <T > createResponse (input : APIGatewayProxyRequestEvent , response : ResponseEntity <T >): APIGatewayProxyResponseEvent {
104
122
val accept = MediaType .parse(input.acceptHeader())
105
123
return when {
0 commit comments