Skip to content

Commit

Permalink
Widen exception type to Throwable
Browse files Browse the repository at this point in the history
Gotta catch 'em all
  • Loading branch information
JakeWharton committed Oct 11, 2022
1 parent b9ccd28 commit 8bc731a
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/app/cash/turbine/channel.kt
Expand Up @@ -92,7 +92,7 @@ public suspend fun <T> ReceiveChannel<T>.awaitEvent(name: String? = null): Event
throw e
} catch (e: ClosedReceiveChannelException) {
Event.Complete
} catch (e: Exception) {
} catch (e: Throwable) {
Event.Error(e)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/app/cash/turbine/flow.kt
Expand Up @@ -130,7 +130,7 @@ internal fun <T> Flow<T>.collectIntoChannel(scope: CoroutineScope): Channel<T> {
try {
collect { output.trySend(it) }
output.close()
} catch (e: Exception) {
} catch (e: Throwable) {
output.close(e)
}
}
Expand Down
19 changes: 9 additions & 10 deletions src/commonTest/kotlin/app/cash/turbine/ChannelTest.kt
Expand Up @@ -23,7 +23,6 @@ import kotlin.time.Duration.Companion.milliseconds
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.emptyFlow
Expand All @@ -39,9 +38,9 @@ import kotlinx.coroutines.withContext
class ChannelTest {
@Test
fun exceptionsPropagateWhenExpectMostRecentItem() = runTest {
val expected = CustomRuntimeException("hello")
val expected = CustomThrowable("hello")

val actual = assertFailsWith<RuntimeException> {
val actual = assertFailsWith<CustomThrowable> {
val channel = flow {
emit(1)
emit(2)
Expand Down Expand Up @@ -124,7 +123,7 @@ class ChannelTest {
}

@Test fun expectErrorOnErrorReceivedBeforeAllItemsWereSkipped() = runTest {
val error = CustomRuntimeException("hello")
val error = CustomThrowable("hello")
val channel = flow {
emit(1)
throw error
Expand Down Expand Up @@ -155,7 +154,7 @@ class ChannelTest {
}

@Test fun expectErrorEvent() = runTest {
val exception = CustomRuntimeException("hello")
val exception = CustomThrowable("hello")
val channel = flow<Nothing> { throw exception }.collectIntoChannel(this)
val event = channel.awaitEvent()
assertEquals(Event.Error(exception), event)
Expand All @@ -175,12 +174,12 @@ class ChannelTest {
}

@Test fun awaitItemButWasErrorThrows() = runTest {
val error = CustomRuntimeException("hello")
val error = CustomThrowable("hello")
val actual = assertFailsWith<AssertionError> {
flow<Unit> { throw error }.collectIntoChannel(this)
.awaitItem()
}
assertEquals("Expected item but found Error(CustomRuntimeException)", actual.message)
assertEquals("Expected item but found Error(CustomThrowable)", actual.message)
assertSame(error, actual.cause)
}

Expand All @@ -205,7 +204,7 @@ class ChannelTest {
}

@Test fun awaitError() = runTest {
val error = CustomRuntimeException("hello")
val error = CustomThrowable("hello")
val channel = flow<Nothing> { throw error }.collectIntoChannel(this)
assertSame(error, channel.awaitError())
}
Expand Down Expand Up @@ -285,12 +284,12 @@ class ChannelTest {
}

@Test fun takeItemButWasErrorThrows() = withTestScope {
val error = CustomRuntimeException("hello")
val error = CustomThrowable("hello")
val actual = assertFailsWith<AssertionError> {
flow<Unit> { throw error }.collectIntoChannel(this)
.takeItem()
}
assertEquals("Expected item but found Error(CustomRuntimeException)", actual.message)
assertEquals("Expected item but found Error(CustomThrowable)", actual.message)
assertSame(error, actual.cause)
}

Expand Down
Expand Up @@ -19,7 +19,7 @@ package app.cash.turbine
* This type prevents coroutines from breaking referential equality by
* reflectively creating new instances.
*/
internal class CustomRuntimeException(
internal class CustomThrowable(
message: String?,
override val cause: Throwable? = null,
) : RuntimeException(message)
) : Throwable(message)
4 changes: 2 additions & 2 deletions src/commonTest/kotlin/app/cash/turbine/FlowInScopeTest.kt
Expand Up @@ -202,12 +202,12 @@ class FlowInScopeTest {
}

@Test fun expectItemButWasErrorThrowsWithName() = runTest {
val error = CustomRuntimeException("hi")
val error = CustomThrowable("hi")
val actual = assertFailsWith<AssertionError> {
flow<Unit> { throw error }.testIn(this, name = "unit flow")
.awaitItem()
}
assertEquals("Expected item for unit flow but found Error(CustomRuntimeException)", actual.message)
assertEquals("Expected item for unit flow but found Error(CustomThrowable)", actual.message)
assertSame(error, actual.cause)
}

Expand Down
26 changes: 13 additions & 13 deletions src/commonTest/kotlin/app/cash/turbine/FlowTest.kt
Expand Up @@ -50,9 +50,9 @@ import kotlinx.coroutines.yield

class FlowTest {
@Test fun exceptionsPropagate() = runTest {
val expected = CustomRuntimeException("hello")
val expected = CustomThrowable("hello")

val actual = assertFailsWith<RuntimeException> {
val actual = assertFailsWith<CustomThrowable> {
neverFlow().test {
throw expected
}
Expand Down Expand Up @@ -311,13 +311,13 @@ class FlowTest {
}

@Test fun expectItemButWasErrorThrows() = runTest {
val error = CustomRuntimeException("hi")
val error = CustomThrowable("hi")
val actual = assertFailsWith<AssertionError> {
flow<Unit> { throw error }.test {
awaitItem()
}
}
assertEquals("Expected item but found Error(CustomRuntimeException)", actual.message)
assertEquals("Expected item but found Error(CustomThrowable)", actual.message)
assertSame(error, actual.cause)
}

Expand All @@ -337,18 +337,18 @@ class FlowTest {
}

@Test fun expectCompleteButWasErrorThrows() = runTest {
val error = CustomRuntimeException("hi")
val error = CustomThrowable("hi")
val actual = assertFailsWith<AssertionError> {
flow<Nothing> { throw error }.test {
awaitComplete()
}
}
assertEquals("Expected complete but found Error(CustomRuntimeException)", actual.message)
assertEquals("Expected complete but found Error(CustomThrowable)", actual.message)
assertSame(error, actual.cause)
}

@Test fun expectError() = runTest {
val error = CustomRuntimeException("hi")
val error = CustomThrowable("hi")
flow<Nothing> { throw error }.test {
assertSame(error, awaitError())
}
Expand Down Expand Up @@ -410,7 +410,7 @@ class FlowTest {
}

@Test fun expectErrorEvent() = runTest {
val exception = CustomRuntimeException("hi")
val exception = CustomThrowable("hi")
flow<Nothing> { throw exception }.test {
val event = awaitEvent()
assertEquals(Event.Error(exception), event)
Expand Down Expand Up @@ -444,9 +444,9 @@ class FlowTest {
}

@Test fun exceptionsPropagateWhenExpectMostRecentItem() = runTest {
val expected = CustomRuntimeException("hello")
val expected = CustomThrowable("hello")

val actual = assertFailsWith<RuntimeException> {
val actual = assertFailsWith<CustomThrowable> {
flow {
emit(1)
emit(2)
Expand Down Expand Up @@ -544,7 +544,7 @@ class FlowTest {
}

@Test fun expectErrorOnErrorReceivedBeforeAllItemsWereSkipped() = runTest {
val error = CustomRuntimeException("hi")
val error = CustomThrowable("hi")
flow {
emit(1)
throw error
Expand Down Expand Up @@ -613,13 +613,13 @@ class FlowTest {
}

@Test fun expectItemButWasErrorThrowsWithName() = runTest {
val error = CustomRuntimeException("hi")
val error = CustomThrowable("hi")
val actual = assertFailsWith<AssertionError> {
flow<Unit> { throw error }.test(name = "unit flow") {
awaitItem()
}
}
assertEquals("Expected item for unit flow but found Error(CustomRuntimeException)", actual.message)
assertEquals("Expected item for unit flow but found Error(CustomThrowable)", actual.message)
assertSame(error, actual.cause)
}

Expand Down
18 changes: 9 additions & 9 deletions src/commonTest/kotlin/app/cash/turbine/TurbineTest.kt
Expand Up @@ -31,9 +31,9 @@ import kotlinx.coroutines.test.runTest
class TurbineTest {
@Test
fun exceptionsPropagateWhenExpectMostRecentItem() = runTest {
val expected = CustomRuntimeException("hello")
val expected = CustomThrowable("hello")

val actual = assertFailsWith<RuntimeException> {
val actual = assertFailsWith<CustomThrowable> {
val channel = Turbine<Int>()

channel.add(1)
Expand Down Expand Up @@ -116,7 +116,7 @@ class TurbineTest {

@Test
fun expectErrorOnErrorReceivedBeforeAllItemsWereSkipped() = runTest {
val error = CustomRuntimeException("hello")
val error = CustomThrowable("hello")
val channel = Turbine<Int>()
channel.add(1)
channel.close(error)
Expand Down Expand Up @@ -150,7 +150,7 @@ class TurbineTest {

@Test
fun expectErrorEvent() = runTest {
val exception = CustomRuntimeException("hello")
val exception = CustomThrowable("hello")
val channel = Turbine<Any>()
channel.close(exception)
val event = channel.awaitEvent()
Expand All @@ -177,13 +177,13 @@ class TurbineTest {

@Test
fun awaitItemButWasErrorThrows() = runTest {
val error = CustomRuntimeException("hello")
val error = CustomThrowable("hello")
val actual = assertFailsWith<AssertionError> {
val channel = Turbine<Any>()
channel.close(error)
channel.awaitItem()
}
assertEquals("Expected item but found Error(CustomRuntimeException)", actual.message)
assertEquals("Expected item but found Error(CustomThrowable)", actual.message)
assertSame(error, actual.cause)
}

Expand Down Expand Up @@ -216,7 +216,7 @@ class TurbineTest {

@Test
fun awaitError() = runTest {
val error = CustomRuntimeException("hello")
val error = CustomThrowable("hello")
val channel = Turbine<Any>()
channel.close(error)
assertSame(error, channel.awaitError())
Expand Down Expand Up @@ -266,7 +266,7 @@ class TurbineTest {

@Test
fun takeItemButWasErrorThrows() = withTestScope {
val error = CustomRuntimeException("hello")
val error = CustomThrowable("hello")
val actual = assertFailsWith<AssertionError> {
val channel = Turbine<Any>()
// JS
Expand All @@ -275,7 +275,7 @@ class TurbineTest {
}
channel.takeItem()
}
assertEquals("Expected item but found Error(CustomRuntimeException)", actual.message)
assertEquals("Expected item but found Error(CustomThrowable)", actual.message)
assertSame(error, actual.cause)
}

Expand Down

0 comments on commit 8bc731a

Please sign in to comment.