ktorio / ktorio/ktor

server-test-host: Rethrown exception in StatusPages exception handler fails test

Open
#1,701 5 comments 1 reaction 0 assignees View on GitHub
question
Dominant language
Kotlin
Stars
14.5k
Forks
1.3k
Avg merge
2d 13h
Merged PRs (30d)
49

Description

**Ktor Version and Engine Used (client or server and name)**
Using ktor server 1.3.1, with `ktor-server-test-host`

**Describe the bug**
When using the `StatusPages` feature to handle exceptions, a rethrowing the exception will bubble out to the test and cause a failure even if it was successfully handled with a `call.respond`. Rethrowing exceptions is suggested as a way to log caught exceptions in the documentation: https://ktor.io/servers/features/status-pages.html#logging-exceptions

**To Reproduce**
```kotlin
@Test
fun test() {
open class MyException : RuntimeException()

withTestApplication({
install(StatusPages) {
exception {
call.respond(HttpStatusCode.UnprocessableEntity, it.toString())
throw it // Comment out this line to pass test, but not log exception
}
}
routing {
get("/") {
throw MyException()
}
}
}) {
with(handleRequest {
method = HttpMethod.Get
uri = "/"
}) {
assertEquals(HttpStatusCode.UnprocessableEntity, response.status())
}
}
}
```

**Expected behavior**
I expect the above test to pass, and the exception to be logged as it would in a non-test application

Contributor guide

Open the contributing guide

Research direction

Start by running the provided withTestApplication and handleRequest reproduction with ktor-server-test-host, then inspect the StatusPages exception path used by the test host. Compare behavior when the exception is rethrown versus omitted; done means the response remains UnprocessableEntity, the test does not fail, and the exception can still be logged.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
backend, testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.