@@ -18,19 +18,30 @@ abstract class RequestHandler : RequestHandler<APIGatewayProxyRequestEvent, APIG
1818
1919 open val objectMapper = jacksonObjectMapper()
2020
21+ abstract val router: Router
22+
2123 @Suppress(" UNCHECKED_CAST" )
2224 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 " )
2426 val routes = router.routes as List <RouterFunction <Any , Any >>
2527 val matchResults: List <RequestMatchResult > = routes.map { routerFunction: RouterFunction <Any , Any > ->
2628 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 '" )
2830 if (matchResult.match) {
2931 val handler: HandlerFunction <Any , Any > = routerFunction.handler
3032 val requestBody = deserializeRequest(handler, input)
3133 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+ }
3445 }
3546
3647 matchResult
@@ -88,8 +99,6 @@ abstract class RequestHandler : RequestHandler<APIGatewayProxyRequestEvent, APIG
8899 )
89100 }
90101
91- abstract val router: Router
92-
93102 open fun createErrorResponse (input : APIGatewayProxyRequestEvent , ex : ApiException ): APIGatewayProxyResponseEvent =
94103 APIGatewayProxyResponseEvent ()
95104 .withBody(objectMapper.writeValueAsString(mapOf (
@@ -100,6 +109,15 @@ abstract class RequestHandler : RequestHandler<APIGatewayProxyRequestEvent, APIG
100109 .withStatusCode(ex.httpResponseStatus)
101110 .withHeaders(mapOf (" Content-Type" to " application/json" ))
102111
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+
103121 open fun <T > createResponse (input : APIGatewayProxyRequestEvent , response : ResponseEntity <T >): APIGatewayProxyResponseEvent {
104122 val accept = MediaType .parse(input.acceptHeader())
105123 return when {
0 commit comments