Skip to content

Commit

Permalink
Make OkHttp coroutine calls always throw exceptions on errors
Browse files Browse the repository at this point in the history
  • Loading branch information
az4521 committed Jan 29, 2024
1 parent 6accf5e commit 0d742fe
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DevRepoUpdateChecker : UpdateChecker() {

override suspend fun checkForUpdate(): UpdateResult {
val response = withContext(Dispatchers.IO) {
client.newCall(GET(DevRepoRelease.LATEST_URL)).await(assertSuccess = false)
client.newCall(GET(DevRepoRelease.LATEST_URL)).await()
}

// Get latest repo version number from header in format "Location: tachiyomi-r1512.apk"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ fun Call.asObservableWithAsyncStacktrace(): Observable<Pair<Exception, Response>
fun Call.asObservable() = asObservableWithAsyncStacktrace().map { it.second }

// Based on https://github.com/gildor/kotlin-coroutines-okhttp
suspend fun Call.await(assertSuccess: Boolean = false): Response {
suspend fun Call.await(): Response {
return suspendCancellableCoroutine { continuation ->
enqueue(object : Callback {
override fun onResponse(call: Call, response: Response) {
if (assertSuccess && !response.isSuccessful) {
if (!response.isSuccessful) {
continuation.resumeWithException(Exception("HTTP error ${response.code}"))
return
}
Expand Down

0 comments on commit 0d742fe

Please sign in to comment.