Skip to content

Commit

Permalink
move exception handling into ktor statuspages
Browse files Browse the repository at this point in the history
  • Loading branch information
c committed Mar 5, 2024
1 parent 0f422b9 commit 6bac1a0
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions examples/server/ktor-server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies {
implementation(libs.ktor.server.netty)
implementation(libs.ktor.server.websockets)
implementation(libs.ktor.server.cors)
implementation(libs.ktor.server.statuspages)
implementation(libs.logback)
implementation(libs.kotlinx.coroutines.jdk8)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,22 @@ import io.ktor.server.plugins.cors.routing.CORS
import io.ktor.server.routing.Routing
import io.ktor.server.websocket.WebSockets
import io.ktor.server.websocket.pingPeriod
import java.lang.UnsupportedOperationException
import java.time.Duration

fun Application.graphQLModule() {
install(WebSockets) {
pingPeriod = Duration.ofSeconds(1)
contentConverter = JacksonWebsocketContentConverter()
}
install(StatusPages) {
exception<Throwable> { call, cause ->
when (cause) {
is UnsupportedOperationException -> call.respond(HttpStatusCode.MethodNotAllowed)
else -> call.respond(HttpStatusCode.BadRequest)
}
}
}
install(CORS) {
anyHost()
}
Expand Down
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ ktor-client-serialization = { group = "io.ktor", name = "ktor-client-serializati
ktor-client-websockets = { group = "io.ktor", name = "ktor-client-websockets", version.ref = "ktor" }
ktor-serialization-jackson = { group = "io.ktor", name = "ktor-serialization-jackson", version.ref = "ktor" }
ktor-server-core = { group = "io.ktor", name = "ktor-server-core", version.ref = "ktor" }
ktor-server-cors = { group = "io.ktor", name = "ktor-server-cors", version.ref = "ktor" }
ktor-server-content = { group = "io.ktor", name = "ktor-server-content-negotiation", version.ref = "ktor" }
ktor-server-statuspages = { group = "io.ktor", name = "ktor-server-status-pages", version.ref = "ktor" }
ktor-server-websockets = { group = "io.ktor", name = "ktor-server-websockets", version.ref = "ktor" }
ktor-server-cors = { group = "io.ktor", name = "ktor-server-cors", version.ref = "ktor" }
maven-plugin-annotations = { group = "org.apache.maven.plugin-tools", name = "maven-plugin-annotations", version.ref = "maven-plugin-annotation" }
maven-plugin-api = { group = "org.apache.maven", name = "maven-plugin-api", version.ref = "maven-plugin-api" }
maven-project = { group = "org.apache.maven", name = "maven-project", version.ref = "maven-project" }
Expand Down
1 change: 1 addition & 0 deletions servers/graphql-kotlin-ktor-server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies {
testImplementation(libs.ktor.client.content)
testImplementation(libs.ktor.client.websockets)
testImplementation(libs.ktor.server.cio)
testImplementation(libs.ktor.server.statuspages)
testImplementation(libs.ktor.server.test.host)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,7 @@ internal fun List<Any>.toTopLevelObjects(): List<TopLevelObject> = this.map {
TopLevelObject(it)
}

internal suspend inline fun KtorGraphQLServer.executeRequest(call: ApplicationCall) = try {
internal suspend inline fun KtorGraphQLServer.executeRequest(call: ApplicationCall) =
execute(call.request)?.let {
call.respond(it)
} ?: call.respond(HttpStatusCode.BadRequest)
} catch (e: UnsupportedOperationException) {
call.respond(HttpStatusCode.MethodNotAllowed)
} catch (e: Exception) {
call.respond(HttpStatusCode.BadRequest)
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import io.ktor.http.contentType
import io.ktor.serialization.jackson.jackson
import io.ktor.server.application.Application
import io.ktor.server.application.install
import io.ktor.server.plugins.statuspages.StatusPages
import io.ktor.server.response.respond
import io.ktor.server.routing.Routing
import io.ktor.server.testing.testApplication
import io.ktor.websocket.Frame
Expand Down Expand Up @@ -183,7 +185,7 @@ class GraphQLPluginTest {
fun `server should return Bad Request for invalid POST requests`() {
testApplication {
val response = client.post("/graphql")
assertEquals(HttpStatusCode.BadRequest, response.status)
assertEquals(HttpStatusCode.UnsupportedMediaType, response.status)
}
}

Expand Down Expand Up @@ -231,6 +233,14 @@ class GraphQLPluginTest {
}

fun Application.testGraphQLModule() {
install(StatusPages) {
exception<Throwable> { call, cause ->
when (cause) {
is UnsupportedOperationException -> call.respond(HttpStatusCode.MethodNotAllowed)
else -> call.respond(HttpStatusCode.BadRequest)
}
}
}
install(GraphQL) {
schema {
// packages property is read from application.conf
Expand Down

0 comments on commit 6bac1a0

Please sign in to comment.