From 8c5569aac61ac7e212447cec52d27d20cfa8ef31 Mon Sep 17 00:00:00 2001 From: Merlin Volkmer Date: Thu, 23 Oct 2025 16:40:59 +0200 Subject: [PATCH] retryFlow now correctly retries on catching a Throwable --- CHANGELOG.md | 1 + .../net/folivo/trixnity/utils/retryFlow.kt | 2 +- .../net/folivo/trixnity/utils/RetryFlowTest.kt | 17 ++++++++++++++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7115c1d35..7117525f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed cache did not persist values when there were no cache entry but subscribers for an index (very rare case) - Fix dokka documentation generation +- retryFlow now correctly retries on catching a Throwable ### Security diff --git a/trixnity-utils/src/commonMain/kotlin/net/folivo/trixnity/utils/retryFlow.kt b/trixnity-utils/src/commonMain/kotlin/net/folivo/trixnity/utils/retryFlow.kt index d982ac809..f411524a5 100644 --- a/trixnity-utils/src/commonMain/kotlin/net/folivo/trixnity/utils/retryFlow.kt +++ b/trixnity-utils/src/commonMain/kotlin/net/folivo/trixnity/utils/retryFlow.kt @@ -38,7 +38,7 @@ fun retryFlow( emit(RetryFlowResult.Suspend) // ensure, that block is not called before a new value is requested emitAll(flow(block).map { RetryFlowResult.Emit(it) }) retryCounter = 0 - } catch (error: Exception) { + } catch (error: Throwable) { if (error is CancellationException) throw error val retryDelay = diff --git a/trixnity-utils/src/commonTest/kotlin/net/folivo/trixnity/utils/RetryFlowTest.kt b/trixnity-utils/src/commonTest/kotlin/net/folivo/trixnity/utils/RetryFlowTest.kt index d71f6f94c..a49c35273 100644 --- a/trixnity-utils/src/commonTest/kotlin/net/folivo/trixnity/utils/RetryFlowTest.kt +++ b/trixnity-utils/src/commonTest/kotlin/net/folivo/trixnity/utils/RetryFlowTest.kt @@ -29,7 +29,7 @@ class RetryFlowTest : TrixnityBaseTest() { } @Test - fun shouldRetryOnError() = runTest { + fun shouldRetryOnException() = runTest { var attempts = 0 val result = retryFlow { attempts++ @@ -43,6 +43,21 @@ class RetryFlowTest : TrixnityBaseTest() { attempts shouldBe 3 } + @Test + fun shouldRetryOnThrowable() = runTest { + var attempts = 0 + val result = retryFlow { + attempts++ + if (attempts < 3) { + throw Throwable("Test throwable") + } + emit(attempts) + }.take(3).first() + + result shouldBe 3 + attempts shouldBe 3 + } + @Test fun shouldRespectDelayConfig() = runTest { var attempts = 0 -- GitLab