@@ -4,6 +4,8 @@ import com.amazonaws.services.lambda.runtime.Context
4
4
import com.amazonaws.services.lambda.runtime.RequestHandler
5
5
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent
6
6
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent
7
+ import com.fasterxml.jackson.core.JsonParseException
8
+ import com.fasterxml.jackson.databind.exc.InvalidDefinitionException
7
9
import com.fasterxml.jackson.databind.exc.InvalidFormatException
8
10
import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException
9
11
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
@@ -122,6 +124,8 @@ abstract class RequestHandler : RequestHandler<APIGatewayProxyRequestEvent, APIG
122
124
*/
123
125
open fun createUnprocessableEntityErrorBody (errors : List <UnprocessableEntityError >): Any = errors
124
126
127
+ private fun createUnprocessableEntityErrorBody (error : UnprocessableEntityError ): Any = createUnprocessableEntityErrorBody(listOf (error))
128
+
125
129
open fun createApiExceptionErrorResponse (contentType : MediaType , input : APIGatewayProxyRequestEvent , ex : ApiException ): APIGatewayProxyResponseEvent =
126
130
createErrorBody(ex.toApiError()).let {
127
131
APIGatewayProxyResponseEvent ()
@@ -138,19 +142,39 @@ abstract class RequestHandler : RequestHandler<APIGatewayProxyRequestEvent, APIG
138
142
139
143
open fun createUnexpectedErrorResponse (contentType : MediaType , input : APIGatewayProxyRequestEvent , ex : Exception ): APIGatewayProxyResponseEvent =
140
144
when (ex) {
145
+ is JsonParseException -> createResponse(contentType, input,
146
+ ResponseEntity (422 , createUnprocessableEntityErrorBody(
147
+ UnprocessableEntityError (
148
+ message = " INVALID_ENTITY" ,
149
+ code = " ENTITY" ,
150
+ path = " " ,
151
+ details = mapOf (
152
+ " payload" to ex.requestPayloadAsString.orEmpty(),
153
+ " message" to ex.message.orEmpty()
154
+ )))))
155
+ is InvalidDefinitionException -> createResponse(contentType, input,
156
+ ResponseEntity (422 , createUnprocessableEntityErrorBody(
157
+ UnprocessableEntityError (
158
+ message = " INVALID_FIELD_FORMAT" ,
159
+ code = " FIELD" ,
160
+ path = ex.path.last().fieldName.orEmpty(),
161
+ details = mapOf (
162
+ " cause" to ex.cause?.message.orEmpty(),
163
+ " message" to ex.message.orEmpty()
164
+ )))))
141
165
is InvalidFormatException ->
142
166
createResponse(contentType, input,
143
- ResponseEntity (422 , createUnprocessableEntityErrorBody(listOf (
167
+ ResponseEntity (422 , createUnprocessableEntityErrorBody(
144
168
UnprocessableEntityError (
145
169
message = " INVALID_FIELD_FORMAT" ,
146
170
code = " FIELD" ,
147
- path = ex.path.last().fieldName.orEmpty())))))
171
+ path = ex.path.last().fieldName.orEmpty()))))
148
172
is MissingKotlinParameterException ->
149
173
createResponse(contentType, input,
150
- ResponseEntity (422 , createUnprocessableEntityErrorBody(listOf ( UnprocessableEntityError (
174
+ ResponseEntity (422 , createUnprocessableEntityErrorBody(UnprocessableEntityError (
151
175
message = " MISSING_REQUIRED_FIELDS" ,
152
176
code = " FIELD" ,
153
- path = ex.parameter.name.orEmpty())))))
177
+ path = ex.parameter.name.orEmpty()))))
154
178
else -> createResponse(contentType, input,
155
179
ResponseEntity (500 , createErrorBody(ApiError (ex.message.orEmpty(), " INTERNAL_SERVER_ERROR" ))))
156
180
}
0 commit comments