CoroutineScope with safely handler parameter

This commit is contained in:
InsanusMokrassar committed 2021-09-04 14:46:12 +06:00
1 parent bd9b0d16ab
commit eed7cfdc42
2 files changed
+12 -1

No files matched your search

@@ -39,9 +39,11 @@ object ContextSafelyExceptionHandlerKey : CoroutineContext.Key<ContextSafelyExce
*/
class ContextSafelyExceptionHandler(
val handler: ExceptionHandler<Unit>
) : 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 <T> runCatchingSafelyWithoutExceptions(
): Result<T?> = runCatching {
safelyWithoutExceptions(onException, block)
}
suspend inline fun CoroutineScope(
context: CoroutineContext,
defaultExceptionsHandler: ExceptionHandler<Unit>
) = CoroutineScope(
context + ContextSafelyExceptionHandler(defaultExceptionsHandler)
)