From 0ae7a2fe9a966b9a802d68854e7a5c7c5d45b39d Mon Sep 17 00:00:00 2001 From: Syer10 Date: Fri, 9 Jul 2021 00:13:06 -0400 Subject: [PATCH] Handle uncaught exceptions when starting server --- src/main/kotlin/ca/gosyer/data/server/ServerService.kt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/ca/gosyer/data/server/ServerService.kt b/src/main/kotlin/ca/gosyer/data/server/ServerService.kt index 93d7d441..ae96561b 100644 --- a/src/main/kotlin/ca/gosyer/data/server/ServerService.kt +++ b/src/main/kotlin/ca/gosyer/data/server/ServerService.kt @@ -10,6 +10,7 @@ import ca.gosyer.BuildConfig import ca.gosyer.util.lang.withIOContext import ca.gosyer.util.system.CKLogger import ca.gosyer.util.system.userDataDir +import kotlinx.coroutines.CoroutineExceptionHandler import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.flow.MutableStateFlow @@ -96,7 +97,13 @@ class ServerService @Inject constructor( ServerResult.UNUSED return@onEach } - GlobalScope.launch { + val handler = CoroutineExceptionHandler { coroutineContext, throwable -> + error(throwable) { "Error launching Tachidesk.jar" } + if (initialized.value == ServerResult.STARTING || initialized.value == ServerResult.STARTED) { + initialized.value = ServerResult.FAILED + } + } + GlobalScope.launch(handler) { val jarFile = File(userDataDir, "Tachidesk.jar") if (!jarFile.exists()) { info { "Copying server to resources" }