Skip to content
Snippets Groups Projects
Commit 5c423d47 authored by Clément CORNU's avatar Clément CORNU
Browse files

feat: better exception handling

parent 97346e96
Branches
No related tags found
No related merge requests found
...@@ -6,25 +6,40 @@ import io.ktor.server.application.* ...@@ -6,25 +6,40 @@ import io.ktor.server.application.*
import io.ktor.server.plugins.* import io.ktor.server.plugins.*
import io.ktor.server.plugins.statuspages.* import io.ktor.server.plugins.statuspages.*
import io.ktor.server.response.* import io.ktor.server.response.*
import kotlinx.serialization.Serializable
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException import software.amazon.awssdk.services.dynamodb.model.DynamoDbException
private const val SOMETHING_WENT_WRONG = "Something went wrong" 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() { fun Application.configureExceptionHandling() {
install(StatusPages) { install(StatusPages) {
exception<Throwable> { call, cause -> exception<Throwable> { call, cause ->
when (cause) { val (errorStatusCode, errorResponse) = when (cause) {
is NotFoundException -> call.respond(HttpStatusCode.NotFound, cause.message ?: SOMETHING_WENT_WRONG) is NotFoundException -> HttpStatusCode.NotFound to ErrorResponse(
is DynamoDbException -> call.respond( message = cause.message ?: RESOURCE_NOT_FOUND,
HttpStatusCode.InternalServerError, statusCode = HttpStatusCode.NotFound.value
cause.message ?: "$SOMETHING_WENT_WRONG with DynamoDb"
) )
is AlreadyExistingPlayerException -> call.respond( is DynamoDbException -> HttpStatusCode.ServiceUnavailable to ErrorResponse(
HttpStatusCode.BadRequest, message = cause.message ?: "$SOMETHING_WENT_WRONG with DynamoDb",
cause.message ?: SOMETHING_WENT_WRONG statusCode = HttpStatusCode.ServiceUnavailable.value,
) )
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
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
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment