Skip to content

Commit

Permalink
Log connection failures
Browse files Browse the repository at this point in the history
  • Loading branch information
Stexxe committed Dec 20, 2024
1 parent 3e96bf5 commit 1b8416f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,14 @@ public val Logging: ClientPlugin<LoggingConfig> = createClientPlugin("Logging",

if (stdFormat) {
logRequestStdFormat(request)

try {
proceed()
} catch (cause: Throwable) {
logger.log("<-- HTTP FAILED: $cause")
throw cause
}

return@on
}

Expand Down Expand Up @@ -565,6 +573,7 @@ private object SendHook : ClientHook<suspend SendHook.Context.(response: HttpReq

class Context(private val context: PipelineContext<Any, HttpRequestBuilder>) {
suspend fun proceedWith(content: Any) = context.proceedWith(content)
suspend fun proceed() = context.proceed()
}

override fun install(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.test.runTest
import kotlinx.io.readByteArray
import org.junit.jupiter.api.BeforeEach
import java.net.UnknownHostException
import kotlin.coroutines.CoroutineContext
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertTrue

class NewFormatTest {
Expand Down Expand Up @@ -724,6 +726,21 @@ class NewFormatTest {
.assertNoMoreLogs()
}

@Test
fun connectFailed() = testWithLevel(LogLevel.INFO, handle = { respondOk() }) { client ->
client.sendPipeline.intercept(HttpSendPipeline.Engine) {
throw UnknownHostException("reason")
}

assertFailsWith<UnknownHostException> {
client.get("/")
}

log.assertLogEqual("--> GET /")
.assertLogEqual("<-- HTTP FAILED: java.net.UnknownHostException: reason")
.assertNoMoreLogs()
}

private fun MockRequestHandleScope.respondWithLength(): HttpResponseData {
return respond("", headers = Headers.build {
append("Content-Length", "0")
Expand Down

0 comments on commit 1b8416f

Please sign in to comment.