Skip to content

Commit

Permalink
KTOR-5409 Response doesn't contain error message (#3605)
Browse files Browse the repository at this point in the history
  • Loading branch information
marychatte authored Jun 12, 2023
1 parent 720a835 commit c2d8a21
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,17 @@ public fun defaultEnginePipeline(environment: ApplicationEnvironment): EnginePip
return pipeline
}

/**
* Logs the [error] and responds with an appropriate error status code.
*/
public suspend fun handleFailure(call: ApplicationCall, error: Throwable) {
logError(call, error)
tryRespondError(call, defaultExceptionStatusCode(error) ?: HttpStatusCode.InternalServerError)
}

@OptIn(InternalAPI::class)
/**
* Logs the [error] with MDC setup.
*/
public suspend fun logError(call: ApplicationCall, error: Throwable) {
call.application.mdcProvider.withMDCBlock(call) {
call.application.environment.logFailure(call, error)
Expand Down Expand Up @@ -92,7 +97,7 @@ private fun ApplicationEnvironment.logFailure(call: ApplicationCall, cause: Thro
"(request error: $cause)"
}

val infoString = "$status: $logString. Exception ${cause::class}: ${cause.message}]"
val infoString = "$status: $logString. Exception ${cause::class}: ${cause.message}"
when (cause) {
is CancellationException,
is ClosedChannelException,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class CallLoggingTest {
private val logger: Logger = object : Logger by LoggerFactory.getLogger("ktor.test") {
override fun trace(message: String?) = add("TRACE: $message")
override fun debug(message: String?) = add("DEBUG: $message")
override fun debug(message: String?, cause: Throwable) = add("DEBUG: $message")
override fun info(message: String?) = add("INFO: $message")

private fun add(message: String?) {
Expand Down Expand Up @@ -478,6 +479,32 @@ class CallLoggingTest {
assertContains(messages, "INFO: 500 Internal Server Error: GET - /error in 0ms")
}

@Test
fun logErrorMessage() = testApplication {
environment {
log = logger
config = MapApplicationConfig("ktor.test.throwOnException" to "false")
}
install(CallLogging) {
level = Level.DEBUG
disableDefaultColors()
clock { 0 }
}
routing {
get("/") {
throw BadRequestException("Message of exception")
}
}

client.get("/").apply {
assertEquals(HttpStatusCode.BadRequest, status)
assertContains(
messages,
"DEBUG: Unhandled: GET - /. Exception class io.ktor.server.plugins.BadRequestException: Message of exception"
)
}
}

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
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ class TestApplicationEngine(
}

private suspend fun PipelineContext<Unit, ApplicationCall>.handleTestFailure(cause: Throwable) {
logError(call, cause)

val throwOnException = environment.config
.propertyOrNull(CONFIG_KEY_THROW_ON_EXCEPTION)
?.getString()?.toBoolean() ?: true
Expand Down

0 comments on commit c2d8a21

Please sign in to comment.