diff --git a/coroutines/build.gradle b/coroutines/build.gradle index 42c09d974c4..5b5d02e9f95 100644 --- a/coroutines/build.gradle +++ b/coroutines/build.gradle @@ -11,6 +11,7 @@ kotlin { commonMain { dependencies { api libs.kt.coroutines + api libs.kslog } } jsMain { diff --git a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/ContextSafelyExceptionHandler.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/ContextSafelyExceptionHandler.kt index 140e59ae9e1..8e62a124087 100644 --- a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/ContextSafelyExceptionHandler.kt +++ b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/ContextSafelyExceptionHandler.kt @@ -41,4 +41,4 @@ class ContextSafelyExceptionHandler( ) : CoroutineContext.Element { override val key: CoroutineContext.Key<*> get() = ContextSafelyExceptionHandlerKey -} \ No newline at end of file +} 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 ce84e2c71fe..bd29880e3c7 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 @@ -15,6 +15,10 @@ import kotlin.coroutines.coroutineContext * * @return [Result] with result of [block] if no exceptions or [Result] from [onException] execution */ +@Deprecated( + "This function become redundant since coroutines correctly handling throwing exceptions", + replaceWith = ReplaceWith("runCatching(block).replaceIfFailure { onException(it) }") +) suspend inline fun runCatchingSafely( onException: ExceptionHandler, block: suspend () -> T @@ -29,6 +33,10 @@ suspend inline fun runCatchingSafely( } } +@Deprecated( + "This function become redundant since coroutines correctly handling throwing exceptions", + replaceWith = ReplaceWith("runCatching(block).replaceIfFailure { onException(it) }") +) suspend inline fun R.runCatchingSafely( onException: ExceptionHandler, block: suspend R.() -> T @@ -39,10 +47,18 @@ suspend inline fun R.runCatchingSafely( /** * Launching [runCatchingSafely] with [defaultSafelyExceptionHandler] as `onException` parameter */ +@Deprecated( + "This function become redundant since coroutines correctly handling throwing exceptions", + replaceWith = ReplaceWith("runCatching(block).replaceIfFailure { defaultSafelyExceptionHandler(it) }") +) suspend inline fun runCatchingSafely( block: suspend () -> T ): Result = runCatchingSafely(defaultSafelyExceptionHandler, block) +@Deprecated( + "This function become redundant since coroutines correctly handling throwing exceptions", + replaceWith = ReplaceWith("runCatching(block).replaceIfFailure { defaultSafelyExceptionHandler(it) }") +) suspend inline fun R.runCatchingSafely( block: suspend R.() -> T ): Result = runCatchingSafely { @@ -73,6 +89,9 @@ suspend fun contextSafelyExceptionHandler() = coroutineContext[ContextSafelyExce * After all, will be called [withContext] method with created [ContextSafelyExceptionHandler] and block which will call * [safely] method with [safelyExceptionHandler] as onException parameter and [block] as execution block */ +@Deprecated( + "This function become redundant since coroutines correctly handling throwing exceptions", +) suspend fun safelyWithContextExceptionHandler( contextExceptionHandler: ExceptionHandler, safelyExceptionHandler: ExceptionHandler = defaultSafelyExceptionHandler, @@ -94,6 +113,10 @@ suspend fun safelyWithContextExceptionHandler( * * @see runCatchingSafely */ +@Deprecated( + "This function become redundant since coroutines correctly handling throwing exceptions", + replaceWith = ReplaceWith("runCatching(block).replaceIfFailure { onException(it) }.getOrThrow()") +) suspend inline fun safely( onException: ExceptionHandler, block: suspend () -> T @@ -104,9 +127,17 @@ suspend inline fun safely( * * @see runCatchingSafely */ +@Deprecated( + "This function become redundant since coroutines correctly handling throwing exceptions", + replaceWith = ReplaceWith("runCatching(block).replaceIfFailure { defaultSafelyExceptionHandler(it) }.getOrThrow()") +) suspend inline fun safely( block: suspend () -> T ): T = safely(defaultSafelyExceptionHandler, block) +@Deprecated( + "This function become redundant since coroutines correctly handling throwing exceptions", + replaceWith = ReplaceWith("runCatching(block).replaceIfFailure { defaultSafelyExceptionHandler(it) }.getOrThrow()") +) suspend inline fun R.safely( block: suspend R.() -> T ): T = safely { block() } @@ -137,11 +168,19 @@ val defaultSafelyWithoutExceptionHandlerWithNull: ExceptionHandler = { * Shortcut for [safely] with exception handler, that as expected must return null in case of impossible creating of * result from exception (instead of throwing it, by default always returns null) */ +@Deprecated( + "This function become redundant since coroutines correctly handling throwing exceptions", + replaceWith = ReplaceWith("runCatching(block).replaceIfFailure { onException(it) }.getOrNull()") +) suspend fun safelyWithoutExceptions( onException: ExceptionHandler = defaultSafelyExceptionHandler, block: suspend () -> T ): T? = runCatchingSafely(onException, block).getOrNull() +@Deprecated( + "This function become redundant since coroutines correctly handling throwing exceptions", + replaceWith = ReplaceWith("runCatching(block).replaceIfFailure { onException(it) }.getOrNull()") +) suspend fun runCatchingSafelyWithoutExceptions( onException: ExceptionHandler = defaultSafelyExceptionHandler, block: suspend () -> T diff --git a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/LaunchLogging.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/LaunchLogging.kt new file mode 100644 index 00000000000..86ec4a000e1 --- /dev/null +++ b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/LaunchLogging.kt @@ -0,0 +1,55 @@ +package dev.inmo.micro_utils.coroutines + +import dev.inmo.kslog.common.KSLog +import dev.inmo.kslog.common.e +import kotlinx.coroutines.* +import kotlin.coroutines.CoroutineContext +import kotlin.coroutines.EmptyCoroutineContext + +fun CoroutineScope.launchLogging( + errorMessageBuilder: () -> Any = { "Something web wrong" }, + logger: KSLog = KSLog, + context: CoroutineContext = EmptyCoroutineContext, + start: CoroutineStart = CoroutineStart.DEFAULT, + block: suspend CoroutineScope.() -> Unit +) = launch(context, start) { + runCatching { block() }.onFailure { + logger.e(it, errorMessageBuilder) + }.getOrThrow() +} + +fun CoroutineScope.launchLoggingDropExceptions( + errorMessageBuilder: () -> Any = { "Something web wrong" }, + logger: KSLog = KSLog, + context: CoroutineContext = EmptyCoroutineContext, + start: CoroutineStart = CoroutineStart.DEFAULT, + block: suspend CoroutineScope.() -> Unit +) = launch(context, start) { + runCatching { block() }.onFailure { + logger.e(it, errorMessageBuilder) + } // just dropping exception +} + +fun CoroutineScope.asyncLogging( + errorMessageBuilder: () -> Any = { "Something web wrong" }, + logger: KSLog = KSLog, + context: CoroutineContext = EmptyCoroutineContext, + start: CoroutineStart = CoroutineStart.DEFAULT, + block: suspend CoroutineScope.() -> T +) = async(context, start) { + runCatching { block() }.onFailure { + logger.e(it, errorMessageBuilder) + }.getOrThrow() +} + +fun CoroutineScope.asyncLoggingDropExceptions( + errorMessageBuilder: () -> Any = { "Something web wrong" }, + logger: KSLog = KSLog, + context: CoroutineContext = EmptyCoroutineContext, + start: CoroutineStart = CoroutineStart.DEFAULT, + block: suspend CoroutineScope.() -> T +) = async(context, start) { + runCatching { block() }.onFailure { + logger.e(it, errorMessageBuilder) + } +} diff --git a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/ReplaceResult.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/ReplaceResult.kt new file mode 100644 index 00000000000..a9629015e91 --- /dev/null +++ b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/ReplaceResult.kt @@ -0,0 +1,3 @@ +package dev.inmo.micro_utils.coroutines + +inline fun Result.replaceIfFailure(onException: (Throwable) -> T) = if (isSuccess) { this } else { runCatching { onException(exceptionOrNull()!!) } }