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.*
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
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