Log exceptions that occur in the MQTT coroutine

This commit is contained in:
Reinout Meliesie 2026-01-22 19:02:40 +01:00
commit 3d2573f8aa
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6
2 changed files with 12 additions and 2 deletions

View file

@ -0,0 +1,10 @@
package com.kernelmaft.zanbur.network
import kotlinx.coroutines.CoroutineExceptionHandler
val exceptionPrinter = CoroutineExceptionHandler { _ , throwable ->
throwable . printStackTrace ()
throw throwable
}

View file

@ -25,7 +25,7 @@ object MqttClient {
this . coroutineScope = coroutineScope this . coroutineScope = coroutineScope
val json = Json { ignoreUnknownKeys = true } val json = Json { ignoreUnknownKeys = true }
coroutineScope . launch (IO) { coroutineScope . launch ( IO + exceptionPrinter ) {
client = MQTTClient ( MQTT5 , MQTT_SERVER_HOST , MQTT_SERVER_PORT , null ) { client = MQTTClient ( MQTT5 , MQTT_SERVER_HOST , MQTT_SERVER_PORT , null ) {
for ( handler in publishHandlers ) handler ( it , json ) for ( handler in publishHandlers ) handler ( it , json )
} }
@ -38,7 +38,7 @@ object MqttClient {
fun addPublishHandler ( handler : MqttPublishHandler ) = publishHandlers . add (handler) fun addPublishHandler ( handler : MqttPublishHandler ) = publishHandlers . add (handler)
fun publish ( topic : String , payload : UByteArray ) { fun publish ( topic : String , payload : UByteArray ) {
coroutineScope !! . launch (IO) { coroutineScope !! . launch ( IO + exceptionPrinter ) {
client !! . publish ( false , AT_MOST_ONCE , topic , payload ) client !! . publish ( false , AT_MOST_ONCE , topic , payload )
} }
} }