diff --git a/backend/src/main/kotlin/plugins/Routing.kt b/backend/src/main/kotlin/plugins/Routing.kt index f56e1ba..a31e282 100644 --- a/backend/src/main/kotlin/plugins/Routing.kt +++ b/backend/src/main/kotlin/plugins/Routing.kt @@ -3,6 +3,7 @@ package dev.svitan.plugins import dev.svitan.routes.routeAuth import io.ktor.http.* import io.ktor.server.application.* +import io.ktor.server.plugins.NotFoundException import io.ktor.server.plugins.requestvalidation.* import io.ktor.server.plugins.statuspages.* import io.ktor.server.response.* @@ -20,7 +21,22 @@ fun Application.configureRouting() { install(StatusPages) { exception { call, cause -> - call.respondText(text = "500: $cause", status = HttpStatusCode.InternalServerError) + when (cause) { + is IllegalArgumentException -> call.respond( + status = HttpStatusCode.BadRequest, + message = cause.message!! + ) + + is NotFoundException -> call.respond( + status = HttpStatusCode.NotFound, + message = cause.message ?: "" + ) + + else -> call.respond( + status = HttpStatusCode.InternalServerError, + message = cause.message ?: "" + ) + } } } diff --git a/backend/src/main/kotlin/routes/Auth.kt b/backend/src/main/kotlin/routes/Auth.kt index e622fc5..7546ec1 100644 --- a/backend/src/main/kotlin/routes/Auth.kt +++ b/backend/src/main/kotlin/routes/Auth.kt @@ -5,6 +5,7 @@ import dev.svitan.schemas.NewAuthDTO import io.ktor.http.HttpStatusCode import io.ktor.server.application.Application import io.ktor.server.auth.authentication +import io.ktor.server.plugins.BadRequestException import io.ktor.server.plugins.NotFoundException import io.ktor.server.request.receive import io.ktor.server.response.respond @@ -24,14 +25,14 @@ fun Application.routeAuth() { } get("/auth/{id}") { - val idRaw = call.parameters["id"] ?: throw IllegalArgumentException("Invalid id") + val idRaw = call.parameters["id"] ?: throw BadRequestException("Invalid id") val id = UUID.fromString(idRaw) val auth = AuthService.read(id) ?: throw NotFoundException() call.respond(auth) } delete("/auth/{id}") { - val idRaw = call.parameters["id"] ?: throw IllegalArgumentException("Invalid id") + val idRaw = call.parameters["id"] ?: throw BadRequestException("Invalid id") val id = UUID.fromString(idRaw) AuthService.delete(id) call.respond(HttpStatusCode.NoContent) diff --git a/backend/src/main/kotlin/schemas/Action.kt b/backend/src/main/kotlin/schemas/Action.kt index 968eab3..d59ea7e 100644 --- a/backend/src/main/kotlin/schemas/Action.kt +++ b/backend/src/main/kotlin/schemas/Action.kt @@ -1,5 +1,6 @@ package dev.svitan.schemas +import io.ktor.server.plugins.NotFoundException import kotlinx.serialization.Serializable import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq @@ -65,7 +66,7 @@ class ActionService { fun create(action: NewActionDTO): UUID = transaction { val now = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) val authId = UUID.fromString(action.authId) - AuthService.read(authId) ?: throw Exception("auth not found") + AuthService.read(authId) ?: throw NotFoundException("auth not found") Actions.insert { it[name] = action.name