<https://stackoverflow.com/questions/46872242/how-...
# coroutines
f
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
Write the logic to analyze what kind of error is that. That is what “make a more finer-grained analysis” placeholder for
f
If i get an error for the call the execution does not go in the catch block.
e
It should. Can you produce a self-contained piece of code that demonstrates the problem? In particular, your implementation of
Call.await
f
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
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
Thank you for the answers!