From eed7cfdc427634ea18aa97c997675e23fa94b8f8 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 4 Sep 2021 14:46:12 +0600 Subject: [PATCH] CoroutineScope with safely handler parameter --- CHANGELOG.md | 2 ++ .../dev/inmo/micro_utils/coroutines/HandleSafely.kt | 11 ++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0246de47656..4c8f38e77cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ * `Versions`: * `Coroutines`: `1.5.1` -> `1.5.2` * `Klock`: `2.3.4` -> `2.4.1` +* `Coroutines`: + * New function `CoroutineScope` with safely exceptions handler as second parameter ## 0.5.23 diff --git a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/HandleSafely.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/HandleSafely.kt index 8fb0d3ad3f9..92f43522de7 100644 --- a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/HandleSafely.kt +++ b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/HandleSafely.kt @@ -39,9 +39,11 @@ object ContextSafelyExceptionHandlerKey : CoroutineContext.Key -) : CoroutineContext.Element { +) : CoroutineContext.Element, CoroutineExceptionHandler { override val key: CoroutineContext.Key<*> get() = ContextSafelyExceptionHandlerKey + + override fun handleException(context: CoroutineContext, exception: Throwable) = handler(exception) } /** @@ -147,3 +149,10 @@ suspend inline fun runCatchingSafelyWithoutExceptions( ): Result = runCatching { safelyWithoutExceptions(onException, block) } + +suspend inline fun CoroutineScope( + context: CoroutineContext, + defaultExceptionsHandler: ExceptionHandler +) = CoroutineScope( + context + ContextSafelyExceptionHandler(defaultExceptionsHandler) +)