Dont throw a exception if the request fails for mdlist tracking

This commit is contained in:
Jobobby04 2021-03-07 13:04:40 -05:00
parent d64a8907eb
commit da3681e602
2 changed files with 37 additions and 10 deletions

View File

@ -19,6 +19,7 @@ import exh.md.handlers.serializers.FollowsPageSerializer
import exh.md.utils.FollowStatus import exh.md.utils.FollowStatus
import exh.md.utils.MdUtil import exh.md.utils.MdUtil
import exh.metadata.metadata.MangaDexSearchMetadata import exh.metadata.metadata.MangaDexSearchMetadata
import exh.util.awaitResponse
import exh.util.floor import exh.util.floor
import kotlinx.serialization.decodeFromString import kotlinx.serialization.decodeFromString
import okhttp3.CacheControl import okhttp3.CacheControl
@ -218,16 +219,8 @@ class FollowsHandler(val client: OkHttpClient, val headers: Headers, val prefere
headers, headers,
CacheControl.FORCE_NETWORK CacheControl.FORCE_NETWORK
) )
try { val response = client.newCall(request).awaitResponse()
val response = client.newCall(request).await() followStatusParse(response)
followStatusParse(response)
} catch (e: Exception) {
if (e.message.equals("HTTP error 404", true)) {
Track.create(TrackManager.MDLIST).apply {
status = FollowStatus.UNFOLLOWED.int
}
} else throw e
}
} }
} }
} }

View File

@ -1,11 +1,16 @@
package exh.util package exh.util
import kotlinx.coroutines.suspendCancellableCoroutine
import okhttp3.Call import okhttp3.Call
import okhttp3.Callback
import okhttp3.Response import okhttp3.Response
import rx.Observable import rx.Observable
import rx.Producer import rx.Producer
import rx.Subscription import rx.Subscription
import java.io.IOException
import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicBoolean
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
fun Call.asObservableWithAsyncStacktrace(): Observable<Pair<Exception, Response>> { fun Call.asObservableWithAsyncStacktrace(): Observable<Pair<Exception, Response>> {
// Record stacktrace at creation time for easier debugging // Record stacktrace at creation time for easier debugging
@ -52,3 +57,32 @@ fun Call.asObservableWithAsyncStacktrace(): Observable<Pair<Exception, Response>
subscriber.setProducer(requestArbiter) subscriber.setProducer(requestArbiter)
} }
} }
/**
* Similar to [Call.await] but it doesn't throw when the response is not successful
*/
suspend fun Call.awaitResponse(): Response {
return suspendCancellableCoroutine { continuation ->
enqueue(
object : Callback {
override fun onResponse(call: Call, response: Response) {
continuation.resume(response)
}
override fun onFailure(call: Call, e: IOException) {
// Don't bother with resuming the continuation if it is already cancelled.
if (continuation.isCancelled) return
continuation.resumeWithException(e)
}
}
)
continuation.invokeOnCancellation {
try {
cancel()
} catch (ex: Throwable) {
// Ignore cancel exception
}
}
}
}