Send worker: let LIMIT_EXCEEDED error to be retry

This commit is contained in:
ganfra 2019-08-02 11:36:32 +02:00
parent a2b6bd0f62
commit d696bd2830
1 changed files with 12 additions and 7 deletions

View File

@ -21,6 +21,7 @@ import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import com.squareup.moshi.JsonClass
import im.vector.matrix.android.api.failure.Failure
import im.vector.matrix.android.api.failure.MatrixError
import im.vector.matrix.android.api.session.events.model.Content
import im.vector.matrix.android.api.session.events.model.Event
import im.vector.matrix.android.api.session.room.send.SendState
@ -67,17 +68,21 @@ internal class SendEventWorker constructor(context: Context, params: WorkerParam
sendEvent(event.eventId, event.type, event.content, params.roomId)
Result.success()
} catch (exception: Throwable) {
when (exception) {
is Failure.NetworkConnection -> Result.retry()
else -> {
localEchoUpdater.updateSendState(event.eventId, SendState.UNDELIVERED)
//always return success, or the chain will be stuck for ever!
Result.success()
}
if (exception.shouldBeRetried()) {
Result.retry()
} else {
localEchoUpdater.updateSendState(event.eventId, SendState.UNDELIVERED)
//always return success, or the chain will be stuck for ever!
Result.success()
}
}
}

private fun Throwable.shouldBeRetried(): Boolean {
return this is Failure.NetworkConnection
|| (this is Failure.ServerError && this.error.code == MatrixError.LIMIT_EXCEEDED)
}

private suspend fun sendEvent(eventId: String, eventType: String, content: Content?, roomId: String) {
localEchoUpdater.updateSendState(eventId, SendState.SENDING)
executeRequest<SendResponse> {