Skip to content

Commit b91fcdc

Browse files
Fix algebra
1 parent d1188f6 commit b91fcdc

File tree

2 files changed

+25
-2
lines changed
  • stream-android-core/src

2 files changed

+25
-2
lines changed

stream-android-core/src/main/java/io/getstream/android/core/api/utils/Algebra.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,14 @@ public operator fun Throwable.plus(other: Throwable): StreamAggregateException {
4747
* [StreamAggregateException] if either [Result] is a failure.
4848
*/
4949
@StreamInternalApi
50-
public operator fun <T, R> Result<T>.times(other: Result<R>): Result<Pair<T, R>> =
51-
this.flatMap { first -> other.map { second -> first to second } }
50+
public operator fun <T, R> Result<T>.times(other: Result<R>): Result<Pair<T, R>> {
51+
when {
52+
this.isFailure && other.isFailure -> {
53+
return Result.failure(this.exceptionOrNull()!! + other.exceptionOrNull()!!)
54+
}
55+
56+
this.isFailure -> return Result.failure(this.exceptionOrNull()!!)
57+
other.isFailure -> return Result.failure(other.exceptionOrNull()!!)
58+
}
59+
return Result.success(this.getOrThrow() to other.getOrThrow())
60+
}

stream-android-core/src/test/java/io/getstream/android/core/api/utils/AlgebraTest.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,18 @@ class AlgebraTest {
103103
assertTrue(combined.isFailure)
104104
assertSame(failure, combined.exceptionOrNull())
105105
}
106+
107+
@Test
108+
fun `times propagates both results when both fail`() {
109+
val failure = IllegalArgumentException("broken")
110+
val failure2 = IllegalArgumentException("broken2")
111+
val left = Result.failure<String>(failure)
112+
val right = Result.failure<String>(failure2)
113+
114+
val combined = left * right
115+
116+
assertTrue(combined.isFailure)
117+
val exception = combined.exceptionOrNull() as StreamAggregateException
118+
assertEquals(listOf(failure, failure2), exception.causes)
119+
}
106120
}

0 commit comments

Comments
 (0)