🔧 Fixes status pages

This commit is contained in:
Daniel Svitan 2025-05-11 17:51:31 +02:00
parent e8ce65fbd1
commit de3bc8897e
3 changed files with 22 additions and 4 deletions

View File

@ -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<Throwable> { 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 ?: ""
)
}
}
}

View File

@ -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)

View File

@ -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