Updated on 2026-08-14
This commit is contained in:
parent
337b9908c4
commit
292d895c4a
3 changed files with 15 additions and 7 deletions
|
|
@ -0,0 +1,30 @@
|
|||
package com.tangem.data.common.utils
|
||||
|
||||
import kotlinx.coroutines.currentCoroutineContext
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.ensureActive
|
||||
import kotlinx.coroutines.yield
|
||||
import timber.log.Timber
|
||||
import kotlin.coroutines.cancellation.CancellationException
|
||||
|
||||
@Suppress("UnconditionalJumpStatementInLoop")
|
||||
suspend fun <T> retryOnError(priority: Boolean = false, call: suspend () -> T): T {
|
||||
while (true) {
|
||||
return try {
|
||||
call()
|
||||
} catch (e: Exception) {
|
||||
if (e is CancellationException) {
|
||||
currentCoroutineContext().ensureActive()
|
||||
}
|
||||
|
||||
Timber.e(e, "Error occurred during retryOnError block")
|
||||
|
||||
if (priority.not()) {
|
||||
yield()
|
||||
delay(timeMillis = 500)
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue