Commit 9f583f85 authored by Christian Sadilek's avatar Christian Sadilek Committed by Sebastian Kaspari
Browse files

Issue #3563: Introduce StoreException for errors caught in Store/Reducer

parent 32f69c0c
......@@ -7,11 +7,11 @@ package mozilla.components.browser.state.store
import androidx.test.ext.junit.runners.AndroidJUnit4
import mozilla.components.browser.state.action.TabListAction
import mozilla.components.browser.state.state.createTab
import mozilla.components.lib.state.StoreException
import mozilla.components.support.test.ext.joinBlocking
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.shadows.ShadowLooper
import java.lang.IllegalStateException
// These tests are in a separate class because they needs to run with
// Robolectric (different runner, slower) while all other tests only
......@@ -30,7 +30,7 @@ class BrowserStoreExceptionTest {
// Wait for the main looper to process the re-thrown exception.
ShadowLooper.idleMainLooper()
} catch (e: IllegalStateException) {
} catch (e: StoreException) {
val cause = e.cause
if (cause != null) {
throw cause
......
......@@ -48,7 +48,7 @@ open class Store<S : State, A : Action>(
// We want exceptions in the reducer to crash the app and not get silently ignored. Therefore we rethrow the
// exception on the main thread.
Handler(Looper.getMainLooper()).postAtFrontOfQueue {
throw IllegalStateException("Exception while reducing state", throwable)
throw StoreException("Exception while reducing state", throwable)
}
// Once an exception happened we do not want to accept any further actions. So let's cancel the scope which
......@@ -141,3 +141,9 @@ open class Store<S : State, A : Action>(
}
}
}
/**
* Exception for otherwise unhandled errors caught while reducing state or
* while managing/notifying observers.
*/
class StoreException(val msg: String, val e: Throwable? = null) : Exception(msg, e)
......@@ -15,7 +15,7 @@ import org.robolectric.shadows.ShadowLooper
class StoreExceptionTest {
// This test is in a separate class because it needs to run with Robolectric (different runner, slower) while all
// other tests only need a Java VM (fast).
@Test(expected = java.lang.IllegalStateException::class)
@Test(expected = StoreException::class)
fun `Exception in reducer will be rethrown on main thread`() {
val throwingReducer: (TestState, TestAction) -> TestState = { _, _ ->
throw IllegalStateException("Not reducing today")
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment