Replies: 1 comment
-
Hi @simplatek, thanks a lot for asking. All session-wide exceptions like this are propagated through subscription flows, so you can catch them in these flows using val client = StompClient()
launch {
val session = client.connect()
session.subscribeText("/destination")
.catch { e -> TODO("handle exceptions here") }
.collect { frameText ->
// do regular code with received frames here
}
} This is so that subscription sites can properly know about any hard failures that will stop the subscription abruptly. If you want to handle these independently of subscriptions as well, there is no dedicated mechanism at the moment, but you can use the general instrumentation callbacks to do that: val client = StompClient {
instrumentation = object : KrossbowInstrumentation {
override fun onWebSocketClosed(cause: Throwable?) {
when(cause) {
is MissingHeartBeatException -> TODO("handle connection drop")
is StompErrorFrameReceived -> TODO("handle protocol error")
null -> Unit // normal closure
else -> Unit // no special handling for other exceptions (maybe log?)
}
}
}
} Note that the exceptions will be thrown nonetheless in the subscriptions (they will not be "caught" here). What is the problem exactly? Would you want that the exceptions are not thrown on subscription collections? In the context of automatic reconnection (see #102), subscriptions would not crash in case of |
Beta Was this translation helpful? Give feedback.
-
Hi, would you be able to provide sample code of how to go about handling exceptions from the stomp client such as MissingHeartBeatException or StompErrorFrameReceived ? Currently, these errors end up as Uncaught exceptions in my testing. I have not been able to catch them even if I use try catch within the launch coroutine. I need to be able to gracefully handle those errors if possible
For instance, when the StompErrorFrameReceived is thrown during the consuming of the webSocketConnection.incomingFrames flow, I see the catch is being called, but then it calls close(), which is where the exception is being propagated and not caught.
Beta Was this translation helpful? Give feedback.
All reactions