-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ConnectExcpetion not beeing cought whilst reconnecting
- Loading branch information
1 parent
5f6ea59
commit 07a0768
Showing
6 changed files
with
78 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
core/src/commonMain/kotlin/dev/schlaubi/lavakord/audio/internal/Platform.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
26 changes: 26 additions & 0 deletions
26
core/src/jsMain/kotlin/dev/schlaubi/lavakord/audio/internal/Platform.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} } |
1 change: 0 additions & 1 deletion
1
core/src/jsMain/kotlin/dev/schlaubi/lavakord/internal/package-info.md
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
core/src/jvmMain/kotlin/dev/schlaubi/lavakord/audio/internal/Platform.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |