Fix coroutine cancellation handling in runCatching

Koitharu 3 years ago
parent e153463c35
commit 918318252e
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27

@ -4,7 +4,9 @@ import androidx.collection.ArrayMap
import androidx.collection.ArraySet import androidx.collection.ArraySet
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock 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.config.ConfigKey
import org.koitharu.kotatsu.parsers.exception.NotFoundException import org.koitharu.kotatsu.parsers.exception.NotFoundException
import org.koitharu.kotatsu.parsers.model.* 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) val currentYear = calendar.get(Calendar.YEAR).toString().takeLast(2)
"$relativeDate/$currentYear" "$relativeDate/$currentYear"
} }
3 -> relativeDate 3 -> relativeDate
else -> return 0L else -> return 0L
} }
@ -124,7 +127,7 @@ class NetTruyenParser(override val context: MangaLoaderContext) :
} }
val response = if (isSearching) { val response = if (isSearching) {
val result = runCatching { context.httpGet(url) } val result = runCatchingCancellable { context.httpGet(url) }
val exception = result.exceptionOrNull() val exception = result.exceptionOrNull()
if (exception is NotFoundException) { if (exception is NotFoundException) {
return emptyList() return emptyList()

@ -306,7 +306,7 @@ internal abstract class GroupleParser(
return context.httpPost(url, payload, headers) 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 context.httpHead(url, headers).isSuccessful
}.getOrDefault(false) }.getOrDefault(false)

@ -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.BeforeAllCallback
import org.junit.jupiter.api.extension.ExtensionContext import org.junit.jupiter.api.extension.ExtensionContext
import org.koitharu.kotatsu.parsers.model.MangaSource import org.koitharu.kotatsu.parsers.model.MangaSource
import org.koitharu.kotatsu.parsers.util.runCatchingCancellable
class AuthCheckExtension : BeforeAllCallback { class AuthCheckExtension : BeforeAllCallback {
@ -22,7 +23,7 @@ class AuthCheckExtension : BeforeAllCallback {
} }
private fun checkAuthorization(source: MangaSource, parser: MangaParserAuthProvider) = runTest { private fun checkAuthorization(source: MangaSource, parser: MangaParserAuthProvider) = runTest {
runCatching { runCatchingCancellable {
parser.getUsername() parser.getUsername()
}.onSuccess { username -> }.onSuccess { username ->
println("Signed in to ${source.name} as $username") println("Signed in to ${source.name} as $username")

Loading…
Cancel
Save