Fix coroutine cancellation handling in runCatching
This commit is contained in:
@@ -4,7 +4,9 @@ import androidx.collection.ArrayMap
|
||||
import androidx.collection.ArraySet
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import org.koitharu.kotatsu.parsers.*
|
||||
import org.koitharu.kotatsu.parsers.MangaLoaderContext
|
||||
import org.koitharu.kotatsu.parsers.MangaSourceParser
|
||||
import org.koitharu.kotatsu.parsers.PagedMangaParser
|
||||
import org.koitharu.kotatsu.parsers.config.ConfigKey
|
||||
import org.koitharu.kotatsu.parsers.exception.NotFoundException
|
||||
import org.koitharu.kotatsu.parsers.model.*
|
||||
@@ -89,6 +91,7 @@ class NetTruyenParser(override val context: MangaLoaderContext) :
|
||||
val currentYear = calendar.get(Calendar.YEAR).toString().takeLast(2)
|
||||
"$relativeDate/$currentYear"
|
||||
}
|
||||
|
||||
3 -> relativeDate
|
||||
else -> return 0L
|
||||
}
|
||||
@@ -124,7 +127,7 @@ class NetTruyenParser(override val context: MangaLoaderContext) :
|
||||
}
|
||||
|
||||
val response = if (isSearching) {
|
||||
val result = runCatching { context.httpGet(url) }
|
||||
val result = runCatchingCancellable { context.httpGet(url) }
|
||||
val exception = result.exceptionOrNull()
|
||||
if (exception is NotFoundException) {
|
||||
return emptyList()
|
||||
|
||||
@@ -306,7 +306,7 @@ internal abstract class GroupleParser(
|
||||
return context.httpPost(url, payload, headers)
|
||||
}
|
||||
|
||||
private suspend fun tryHead(url: String, headers: Headers): Boolean = runCatching {
|
||||
private suspend fun tryHead(url: String, headers: Headers): Boolean = runCatchingCancellable {
|
||||
context.httpHead(url, headers).isSuccessful
|
||||
}.getOrDefault(false)
|
||||
|
||||
|
||||
13
src/main/kotlin/org/koitharu/kotatsu/parsers/util/Result.kt
Normal file
13
src/main/kotlin/org/koitharu/kotatsu/parsers/util/Result.kt
Normal file
@@ -0,0 +1,13 @@
|
||||
package org.koitharu.kotatsu.parsers.util
|
||||
|
||||
import kotlinx.coroutines.CancellationException
|
||||
|
||||
inline fun <T, R> T.runCatchingCancellable(block: T.() -> R): Result<R> {
|
||||
return try {
|
||||
Result.success(block())
|
||||
} catch (e: CancellationException) {
|
||||
throw e
|
||||
} catch (e: Throwable) {
|
||||
Result.failure(e)
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import kotlinx.coroutines.test.runTest
|
||||
import org.junit.jupiter.api.extension.BeforeAllCallback
|
||||
import org.junit.jupiter.api.extension.ExtensionContext
|
||||
import org.koitharu.kotatsu.parsers.model.MangaSource
|
||||
import org.koitharu.kotatsu.parsers.util.runCatchingCancellable
|
||||
|
||||
class AuthCheckExtension : BeforeAllCallback {
|
||||
|
||||
@@ -22,7 +23,7 @@ class AuthCheckExtension : BeforeAllCallback {
|
||||
}
|
||||
|
||||
private fun checkAuthorization(source: MangaSource, parser: MangaParserAuthProvider) = runTest {
|
||||
runCatching {
|
||||
runCatchingCancellable {
|
||||
parser.getUsername()
|
||||
}.onSuccess { username ->
|
||||
println("Signed in to ${source.name} as $username")
|
||||
|
||||
Reference in New Issue
Block a user