🔧 Fixes status pages
This commit is contained in:
parent
e8ce65fbd1
commit
de3bc8897e
@ -3,6 +3,7 @@ package dev.svitan.plugins
|
|||||||
import dev.svitan.routes.routeAuth
|
import dev.svitan.routes.routeAuth
|
||||||
import io.ktor.http.*
|
import io.ktor.http.*
|
||||||
import io.ktor.server.application.*
|
import io.ktor.server.application.*
|
||||||
|
import io.ktor.server.plugins.NotFoundException
|
||||||
import io.ktor.server.plugins.requestvalidation.*
|
import io.ktor.server.plugins.requestvalidation.*
|
||||||
import io.ktor.server.plugins.statuspages.*
|
import io.ktor.server.plugins.statuspages.*
|
||||||
import io.ktor.server.response.*
|
import io.ktor.server.response.*
|
||||||
@ -20,7 +21,22 @@ fun Application.configureRouting() {
|
|||||||
|
|
||||||
install(StatusPages) {
|
install(StatusPages) {
|
||||||
exception<Throwable> { call, cause ->
|
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 ?: ""
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ import dev.svitan.schemas.NewAuthDTO
|
|||||||
import io.ktor.http.HttpStatusCode
|
import io.ktor.http.HttpStatusCode
|
||||||
import io.ktor.server.application.Application
|
import io.ktor.server.application.Application
|
||||||
import io.ktor.server.auth.authentication
|
import io.ktor.server.auth.authentication
|
||||||
|
import io.ktor.server.plugins.BadRequestException
|
||||||
import io.ktor.server.plugins.NotFoundException
|
import io.ktor.server.plugins.NotFoundException
|
||||||
import io.ktor.server.request.receive
|
import io.ktor.server.request.receive
|
||||||
import io.ktor.server.response.respond
|
import io.ktor.server.response.respond
|
||||||
@ -24,14 +25,14 @@ fun Application.routeAuth() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get("/auth/{id}") {
|
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 id = UUID.fromString(idRaw)
|
||||||
val auth = AuthService.read(id) ?: throw NotFoundException()
|
val auth = AuthService.read(id) ?: throw NotFoundException()
|
||||||
call.respond(auth)
|
call.respond(auth)
|
||||||
}
|
}
|
||||||
|
|
||||||
delete("/auth/{id}") {
|
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)
|
val id = UUID.fromString(idRaw)
|
||||||
AuthService.delete(id)
|
AuthService.delete(id)
|
||||||
call.respond(HttpStatusCode.NoContent)
|
call.respond(HttpStatusCode.NoContent)
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package dev.svitan.schemas
|
package dev.svitan.schemas
|
||||||
|
|
||||||
|
import io.ktor.server.plugins.NotFoundException
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
import org.jetbrains.exposed.sql.*
|
import org.jetbrains.exposed.sql.*
|
||||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||||
@ -65,7 +66,7 @@ class ActionService {
|
|||||||
fun create(action: NewActionDTO): UUID = transaction {
|
fun create(action: NewActionDTO): UUID = transaction {
|
||||||
val now = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
|
val now = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
|
||||||
val authId = UUID.fromString(action.authId)
|
val authId = UUID.fromString(action.authId)
|
||||||
AuthService.read(authId) ?: throw Exception("auth not found")
|
AuthService.read(authId) ?: throw NotFoundException("auth not found")
|
||||||
|
|
||||||
Actions.insert {
|
Actions.insert {
|
||||||
it[name] = action.name
|
it[name] = action.name
|
||||||
|
Loading…
x
Reference in New Issue
Block a user