diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Result.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Result.kt index 313574042..e5be12937 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Result.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Result.kt @@ -13,3 +13,17 @@ inline fun T.runCatchingCancellable(block: T.() -> R): Result { Result.failure(e) } } + +inline fun Result.recoverCatchingCancellable(transform: (exception: Throwable) -> R): Result { + return when (val exception = exceptionOrNull()) { + null -> this + else -> runCatchingCancellable { transform(exception) } + } +} + +inline fun Result.recoverNotNull(transform: (exception: Throwable) -> R?): Result { + return when (val exception = exceptionOrNull()) { + null -> this + else -> transform(exception)?.let(Result.Companion::success) ?: this + } +}