https://kotlinlang.org logo
#coroutines
Title
# coroutines
f

frogman544

11/08/2017, 8:08 AM
Thank you! I saw the answer, but I don’t know how to do this retry only if I get a HTTP error. How can I access the result of the call and only retry if the call fails?
e

elizarov

11/08/2017, 8:41 AM
Write the logic to analyze what kind of error is that. That is what “make a more finer-grained analysis” placeholder for
f

frogman544

11/08/2017, 9:55 AM
If i get an error for the call the execution does not go in the catch block.
e

elizarov

11/08/2017, 11:16 AM
It should. Can you produce a self-contained piece of code that demonstrates the problem? In particular, your implementation of
Call.await
f

frogman544

11/08/2017, 12:37 PM
Copy code
suspend fun <T> Call<T>.awaitResult(retries: Int = 2): Result<T> {
    return suspendCancellableCoroutine { continuation ->
        enqueue(object : Callback<T> {
            override fun onResponse(call: Call<T>?, response: Response<T>) {
                continuation.resume(
                        if (response.isSuccessful) {
                            Result.Ok(response.body(), response.raw())
                        } else {
                            Result.Error(HttpError(response), response.raw(), call)
                        }
                )
            }

            override fun onFailure(call: Call<T>, t: Throwable) {
                continuation.resume(Result.Exception(t))
            }
        })
    }
}
This is my implementation and I wrap the actual call inside.
Copy code
retry { actualCall.awaitResult }
e

elizarov

11/08/2017, 1:48 PM
So, your
awaitResult
implementation never resumes with exception, so it is no surprise that
try { … } catch
inside
retry
is not doing anything
You have two options here: 1. Either use an implementation of
await
that throws exceptions on error. 2. Or implement a version of
retry
that is specifically tailored to
Resut<T>
can can make decisions based on it
f

frogman544

11/09/2017, 8:02 AM
Thank you for the answers!
4 Views