Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate network failures in Emoji Search #927

Merged
merged 1 commit into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package com.example.redwood.emojisearch.android.composeui

import com.example.redwood.emojisearch.treehouse.HostApi
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlinx.coroutines.suspendCancellableCoroutine
import okhttp3.Call
import okhttp3.Callback
Expand All @@ -25,6 +27,10 @@ import okhttp3.Request
import okhttp3.Response
import okio.IOException

/** Exception for an unexpected, non-2xx HTTP response. */
class HttpException(response: Response) :
RuntimeException("HTTP ${response.code} ${response.message}")

class RealHostApi(
private val client: OkHttpClient,
) : HostApi {
Expand All @@ -43,8 +49,11 @@ class RealHostApi(
}

override fun onResponse(call: Call, response: Response) {
val responseString = response.body!!.string()
continuation.resumeWith(Result.success(responseString))
if (response.isSuccessful) {
continuation.resume(response.body!!.string())
} else {
continuation.resumeWithException(HttpException(response))
}
}
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package com.example.redwood.emojisearch.android.views

import com.example.redwood.emojisearch.treehouse.HostApi
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlinx.coroutines.suspendCancellableCoroutine
import okhttp3.Call
import okhttp3.Callback
Expand All @@ -25,6 +27,10 @@ import okhttp3.Request
import okhttp3.Response
import okio.IOException

/** Exception for an unexpected, non-2xx HTTP response. */
class HttpException(response: Response) :
RuntimeException("HTTP ${response.code} ${response.message}")

class RealHostApi(
private val client: OkHttpClient,
) : HostApi {
Expand All @@ -43,8 +49,11 @@ class RealHostApi(
}

override fun onResponse(call: Call, response: Response) {
val responseString = response.body!!.string()
continuation.resumeWith(Result.success(responseString))
if (response.isSuccessful) {
continuation.resume(response.body!!.string())
} else {
continuation.resumeWithException(HttpException(response))
}
}
},
)
Expand Down