Skip to content

Commit

Permalink
KTOR-550 Fix MDC exception crash request (#3143)
Browse files Browse the repository at this point in the history
  • Loading branch information
e5l committed Aug 31, 2022
1 parent e638b15 commit 9bf1ff9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Expand Up @@ -29,7 +29,8 @@ internal fun List<MDCEntry>.setup(call: ApplicationCall): Map<String, String> {
val result = HashMap<String, String>()

forEach { entry ->
entry.provider(call)?.let { mdcValue ->
val provider = runCatching { entry.provider(call) }.getOrNull()
provider?.let { mdcValue ->
result[entry.name] = mdcValue
}
}
Expand Down
Expand Up @@ -5,6 +5,7 @@
package io.ktor.server.plugins.callloging

import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.engine.*
Expand Down Expand Up @@ -371,6 +372,32 @@ class CallLoggingTest {
assertTrue("INFO: 404 Not Found: GET - /" in messages)
}

@Test
fun `mdc exception does not crash request`() = testApplication {
var failed = 0
install(CallLogging) {
mdc("bar") {
failed++
throw Exception()
}
mdc("foo") { "Hello" }
}

routing {
get {
call.respond("OK")
}
}

assertEquals(0, failed)

assertEquals("OK", client.get("/").bodyAsText())
assertEquals(3, failed)

assertEquals("OK", client.get("/").bodyAsText())
assertEquals(6, failed)
}

private fun green(value: Any): String = colored(value, Ansi.Color.GREEN)
private fun red(value: Any): String = colored(value, Ansi.Color.RED)
private fun cyan(value: Any): String = colored(value, Ansi.Color.CYAN)
Expand Down

0 comments on commit 9bf1ff9

Please sign in to comment.