Make safeLaunch inherit CoroutineScope's handler

This commit is contained in:
Butterscotch! committed 2026-05-27 23:41:46 -04:00
1 parent 559c076b26
commit 24da10715f
1 file changed
+8 -4
@@ -25,10 +25,14 @@ fun CoroutineScope.safeLaunch(
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> Unit,
): Job {
val handler = context[CoroutineExceptionHandler] ?: CoroutineExceptionHandler { ctx, throwable ->
val name = ctx[CoroutineName]?.name ?: "UnknownScope"
AppLogger.coroutines.error(throwable, "Unhandled exception in safeLaunch (scope: $name)")
}
val handler = context[CoroutineExceptionHandler]
?: this.coroutineContext[CoroutineExceptionHandler]
?: CoroutineExceptionHandler { ctx, throwable ->
val name = ctx[CoroutineName]?.name ?: "UnknownScope"
AppLogger.coroutines.error(
throwable, "Unhandled exception in safeLaunch (scope: $name)"
)
}
//noinspection RAW_LAUNCH_USAGE
return this.launch(context + handler, start = start, block = block)
}