Skip to content

Commit

Permalink
Do not fail when accessing the body for info on handshake failure (Ok…
Browse files Browse the repository at this point in the history
…Http)

Resolves:
#359
  • Loading branch information
joffrey-bion committed Aug 10, 2023
1 parent 13aa1a1 commit 44231ea
Showing 1 changed file with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import okio.ByteString.Companion.toByteString
import org.hildan.krossbow.websocket.WebSocketConnectionException
import org.hildan.krossbow.websocket.WebSocketFrame
import org.hildan.krossbow.websocket.WebSocketListenerFlowAdapter
import java.net.SocketException
import kotlin.coroutines.Continuation
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
Expand Down Expand Up @@ -68,10 +69,17 @@ private class KrossbowToOkHttpListenerAdapter(

override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
if (isConnecting) {
val responseBody = try {
response?.body?.string()?.takeIf { it.isNotBlank() }
} catch (e: SocketException) {
// we can't always read the body when the connection failed
t.addSuppressed(e)
null
}
val exception = WebSocketConnectionException(
url = webSocket.request().url.toString(),
httpStatusCode = response?.code,
additionalInfo = response?.body?.string()?.takeIf { it.isNotBlank() },
additionalInfo = responseBody,
cause = t,
)
completeConnection {
Expand Down

0 comments on commit 44231ea

Please sign in to comment.