diff --git a/src/main/kotlin/configuration/ExceptionConfiguration.kt b/src/main/kotlin/configuration/ExceptionConfiguration.kt index 6f00bf57ab8f83adb494b97ce16f740573f186b4..7c6ad94bc7f3ac0ddc1ca12a5415d77ebd21e2e5 100644 --- a/src/main/kotlin/configuration/ExceptionConfiguration.kt +++ b/src/main/kotlin/configuration/ExceptionConfiguration.kt @@ -6,25 +6,40 @@ import io.ktor.server.application.* import io.ktor.server.plugins.* import io.ktor.server.plugins.statuspages.* import io.ktor.server.response.* +import kotlinx.serialization.Serializable import software.amazon.awssdk.services.dynamodb.model.DynamoDbException private const val SOMETHING_WENT_WRONG = "Something went wrong" +private const val RESOURCE_NOT_FOUND = "Resource not found" + +@Serializable +data class ErrorResponse( + val message: String, + val statusCode: Int, +) fun Application.configureExceptionHandling() { install(StatusPages) { exception<Throwable> { call, cause -> - when (cause) { - is NotFoundException -> call.respond(HttpStatusCode.NotFound, cause.message ?: SOMETHING_WENT_WRONG) - is DynamoDbException -> call.respond( - HttpStatusCode.InternalServerError, - cause.message ?: "$SOMETHING_WENT_WRONG with DynamoDb" + val (errorStatusCode, errorResponse) = when (cause) { + is NotFoundException -> HttpStatusCode.NotFound to ErrorResponse( + message = cause.message ?: RESOURCE_NOT_FOUND, + statusCode = HttpStatusCode.NotFound.value + ) + + is DynamoDbException -> HttpStatusCode.ServiceUnavailable to ErrorResponse( + message = cause.message ?: "$SOMETHING_WENT_WRONG with DynamoDb", + statusCode = HttpStatusCode.ServiceUnavailable.value, ) - is AlreadyExistingPlayerException -> call.respond( - HttpStatusCode.BadRequest, - cause.message ?: SOMETHING_WENT_WRONG + is AlreadyExistingPlayerException -> HttpStatusCode.BadRequest to ErrorResponse( + message = cause.message ?: SOMETHING_WENT_WRONG, + statusCode = HttpStatusCode.BadRequest.value, ) + + else -> throw cause } + call.respond(errorStatusCode, errorResponse) } } } \ No newline at end of file diff --git a/src/main/kotlin/configuration/SecurityConfiguration.kt b/src/main/kotlin/configuration/SecurityConfiguration.kt deleted file mode 100644 index a3f508ed1e58876e02e01b1ff24d9f7e2d8f826c..0000000000000000000000000000000000000000 --- a/src/main/kotlin/configuration/SecurityConfiguration.kt +++ /dev/null @@ -1,7 +0,0 @@ -package betclic.test.configuration - -import io.ktor.server.application.* - -fun Application.configureSecurity() { - //TODO Add comment to say what I would do in term of security -}