Skip to content

Commit

Permalink
Fix ConnectExcpetion not beeing cought whilst reconnecting
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Apr 24, 2022
1 parent 5f6ea59 commit 07a0768
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 16 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "dev.schlaubi.lavakord"
version = "3.6.1"
version = "3.6.2"

allprojects {
repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal class NodeImpl(

internal suspend fun connect(resume: Boolean = false) {
session = try {
lavakord.gatewayClient.webSocketSession {
connect(resume) {
url(this@NodeImpl.host)
header("Authorization", authenticationHeader)
header("Num-Shards", lavakord.shardsTotal)
Expand All @@ -54,21 +54,11 @@ internal class NodeImpl(
header("Resume-Key", resumeKey)
}
}
} catch (e: ServerResponseException) {
reconnect(
IllegalArgumentException(
"The provided server responded with an invalid response code",
e
), resume
)
return
} catch (e: ConnectTimeoutException) {
reconnect(IllegalStateException("The connection to the node timed out", e), resume)
return
} catch (e: ClientRequestException) {
reconnect(e, resume)
} catch (e: ReconnectException) {
reconnect(e.cause, resume)
return
}

retry.reset()
available = true

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.schlaubi.lavakord.audio.internal

import io.ktor.client.plugins.websocket.*
import io.ktor.client.request.*

internal class ReconnectException(cause: Throwable) : RuntimeException(cause)

internal fun reconnect(cause: Throwable): Nothing = throw ReconnectException(cause)

/**
* Connects to the ws server configured by [block] and throws a [ReconnectException] if a recoverable error occurrs.
*/
internal expect suspend fun NodeImpl.connect(
resume: Boolean,
block: HttpRequestBuilder.() -> Unit
): DefaultClientWebSocketSession
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package dev.schlaubi.lavakord.audio.internal

import io.ktor.client.network.sockets.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.websocket.*
import io.ktor.client.request.*
import dev.schlaubi.lavakord.audio.internal.reconnect as internalReconnect

internal actual suspend fun NodeImpl.connect(
resume: Boolean,
block: HttpRequestBuilder.() -> Unit
): DefaultClientWebSocketSession {
return try {
lavakord.gatewayClient.webSocketSession(block)
} catch (e: ServerResponseException) {
internalReconnect(
IllegalArgumentException(
"The provided server responded with an invalid response code",
e
)
)
} catch (e: ConnectTimeoutException) {
internalReconnect(IllegalStateException("The connection to the node timed out", e))
} catch (e: ClientRequestException) {
internalReconnect(e)
} }

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
@file:JvmName("PlatformKtJvm")
package dev.schlaubi.lavakord.audio.internal

import io.ktor.client.network.sockets.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.websocket.*
import io.ktor.client.request.*
import java.net.ConnectException
import dev.schlaubi.lavakord.audio.internal.reconnect as internalReconnect

internal actual suspend fun NodeImpl.connect(
resume: Boolean,
block: HttpRequestBuilder.() -> Unit
): DefaultClientWebSocketSession {
return try {
lavakord.gatewayClient.webSocketSession(block)
} catch (e: ServerResponseException) {
internalReconnect(
IllegalArgumentException(
"The provided server responded with an invalid response code",
e
)
)
} catch (e: ConnectTimeoutException) {
internalReconnect(IllegalStateException("The connection to the node timed out", e))
} catch (e: ClientRequestException) {
internalReconnect(e)
} catch (e: ConnectException) {
internalReconnect(e)
}
}

0 comments on commit 07a0768

Please sign in to comment.