Skip to content

Commit c6d430f

Browse files
committed
Document exception behaviour
1 parent 7a5ea13 commit c6d430f

File tree

7 files changed

+59
-12
lines changed

7 files changed

+59
-12
lines changed

src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkClient.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,22 @@ class LavalinkClient(val userId: Long) : Closeable, Disposable {
112112
}
113113

114114
// For the java people
115+
/**
116+
* Listen to events from all nodes. Please note that uncaught exceptions will cause the listener to stop emitting events.
117+
*
118+
* @param type the [ClientEvent] to listen for
119+
*
120+
* @return a [Flux] of [ClientEvent]s
121+
*/
115122
fun <T : ClientEvent<*>> on(type: Class<T>): Flux<T> {
116123
return flux.ofType(type)
117124
}
118125

126+
/**
127+
* Listen to events from all nodes. Please note that uncaught exceptions will cause the listener to stop emitting events.
128+
*
129+
* @return a [Flux] of [ClientEvent]s
130+
*/
119131
inline fun <reified T : ClientEvent<*>> on() = on(T::class.java)
120132

121133
/**
@@ -138,7 +150,7 @@ class LavalinkClient(val userId: Long) : Closeable, Disposable {
138150
try {
139151
sink.tryEmitNext(it)
140152
} catch (e: Exception) {
141-
sink.tryEmitError(e)
153+
sink.emitError(e, Sinks.EmitFailureHandler.FAIL_FAST)
142154
}
143155
}
144156
}

src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkNode.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,22 @@ class LavalinkNode(
8282
}
8383

8484
// For the java people
85+
/**
86+
* Listen to events from the node. Please note that uncaught exceptions will cause the listener to stop emitting events.
87+
*
88+
* @param type the [ClientEvent] to listen for
89+
*
90+
* @return a [Flux] of [ClientEvent]s
91+
*/
8592
fun <T : ClientEvent<*>> on(type: Class<T>): Flux<T> {
8693
return flux.ofType(type)
8794
}
8895

96+
/**
97+
* Listen to events from the node. Please note that uncaught exceptions will cause the listener to stop emitting events.
98+
*
99+
* @return a [Flux] of [ClientEvent]s
100+
*/
89101
inline fun <reified T : ClientEvent<*>> on() = on(T::class.java)
90102

91103
/**

src/main/kotlin/dev/arbjerg/lavalink/client/protocol/Track.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import dev.arbjerg.lavalink.internal.fromJsonElement
55
import dev.arbjerg.lavalink.internal.toJackson
66
import dev.arbjerg.lavalink.internal.toJsonElement
77
import kotlinx.serialization.json.JsonObject
8+
import kotlinx.serialization.json.jsonObject
89
import dev.arbjerg.lavalink.protocol.v4.Track as ProtocolTrack
910

1011
internal fun ProtocolTrack.toCustom() = Track(this)
@@ -16,9 +17,9 @@ class Track internal constructor(private var internalTrack: ProtocolTrack) {
1617
val info = internalTrack.info
1718
val pluginInfo = internalTrack.pluginInfo.toJackson()
1819

19-
fun setUserData(userData: Any) {
20+
fun setUserData(userData: Any?) {
2021
internalTrack = internalTrack.copyWithUserData(
21-
toJsonElement(userData) as JsonObject
22+
toJsonElement(userData).jsonObject
2223
)
2324
}
2425

src/main/kotlin/dev/arbjerg/lavalink/client/protocol/TrackUpdateBuilder.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import dev.arbjerg.lavalink.protocol.v4.Omissible
55
import dev.arbjerg.lavalink.protocol.v4.PlayerUpdateTrack
66
import dev.arbjerg.lavalink.protocol.v4.toOmissible
77
import kotlinx.serialization.json.JsonObject
8+
import kotlinx.serialization.json.jsonObject
89

910
/**
1011
* Allows you to update the playing track with only the fields that you wish to update.
@@ -42,9 +43,9 @@ class TrackUpdateBuilder {
4243
)
4344
}
4445

45-
fun setUserData(userData: Any) = apply {
46+
fun setUserData(userData: Any?) = apply {
4647
internalUpdate = internalUpdate.copy(
47-
userData = (toJsonElement(userData) as JsonObject).toOmissible()
48+
userData = (toJsonElement(userData).jsonObject).toOmissible()
4849
)
4950
}
5051

src/main/kotlin/dev/arbjerg/lavalink/internal/JsonParser.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ private val objectMapper = ObjectMapper()
1313
fun JsonElement.toJackson(): JsonNode = objectMapper.readTree(this.toString())
1414
fun JsonNode.toKotlin(): JsonObject = toJsonElement(this) as JsonObject
1515

16-
fun toJsonElement(obj: Any): JsonElement {
16+
fun toJsonElement(obj: Any?): JsonElement {
17+
if (obj == null) {
18+
return JsonObject(mapOf())
19+
}
20+
1721
val jsonString = objectMapper.writeValueAsString(obj)
1822

1923
return json.parseToJsonElement(jsonString)

src/main/kotlin/dev/arbjerg/lavalink/internal/LavalinkSocket.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import okhttp3.Response
1212
import okhttp3.WebSocket
1313
import okhttp3.WebSocketListener
1414
import org.slf4j.LoggerFactory
15+
import reactor.core.publisher.Sinks
1516
import java.io.Closeable
1617
import java.io.EOFException
1718
import java.net.ConnectException
@@ -113,7 +114,7 @@ class LavalinkSocket(private val node: LavalinkNode) : WebSocketListener(), Clos
113114
try {
114115
node.sink.tryEmitNext(event.toClientEvent(node))
115116
} catch (e: Exception) {
116-
node.sink.tryEmitError(e)
117+
node.sink.emitError(e, Sinks.EmitFailureHandler.FAIL_FAST)
117118
}
118119
}
119120

src/test/kotlin/testScript.kt

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,25 @@ fun main() {
2828
println("Node '${event.node.name}' is ready, session id is '${event.sessionId}'!")
2929
}
3030

31+
// Testing for exceptions thrown in the handler
32+
var counter = 0
33+
3134
client.on<StatsEvent>()
32-
.subscribe { event ->
35+
.subscribe({ event ->
36+
counter++
3337
println("Node '${event.node.name}' has stats, current players: ${event.playingPlayers}/${event.players}")
38+
39+
// WARNING: if you do not catch your exception here, the subscription will be canceled.
40+
if (counter == 2) {
41+
throw RuntimeException("Counter has reached 2")
42+
}
43+
}) { err ->
44+
err.printStackTrace()
45+
}
46+
47+
client.on<StatsEvent>()
48+
.subscribe { event ->
49+
println("[event 2] Node '${event.node.name}' has stats, current players: ${event.playingPlayers}/${event.players}")
3450
}
3551

3652
client.on<EmittedEvent<*>>()
@@ -84,19 +100,19 @@ fun main() {
84100

85101
fun registerNode(client: LavalinkClient) {
86102
listOf(
87-
client.addNode(
103+
/*client.addNode(
88104
"Testnode",
89105
URI.create("ws://localhost:2333"),
90106
"youshallnotpass",
91107
RegionGroup.EUROPE
92-
),
108+
),*/
93109

94-
/*client.addNode(
110+
client.addNode(
95111
"Mac-mini",
96112
URI.create("ws://mac-mini.local.duncte123.lgbt:2333/bepis"),
97113
"youshallnotpass",
98114
RegionGroup.US
99-
)*/
115+
)
100116
)
101117
.forEach { node ->
102118
node.on<TrackStartEvent>()

0 commit comments

Comments
 (0)