Skip to content

Commit

Permalink
Use error message in docs instaed of Throwable
Browse files Browse the repository at this point in the history
  • Loading branch information
sockeqwe committed Jul 27, 2023
1 parent ff78665 commit 37b5f6a
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docs/user-guide/10_accross-multiple-states.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ sealed interface ListState {
data class ShowContent(val items: List<Item>) : PostLoading

// Error while loading happened
data class Error(val cause: Throwable) : PostLoading
data class Error(val message: String) : PostLoading
}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/user-guide/12_improve-readability.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ItemListStateMachine(
val items = httpClient.loadItems()
state.override { ShowContent(items) }
} catch (t: Throwable) {
state.override { Error(t, countdown = 3) } // countdown is new
state.override { Error("A network error occurred", countdown = 3) } // countdown is new
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions docs/user-guide/14_testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fun `move from Loading to Error state on error http response`() = runTest {
val statemachine = ItemListStateMachine(httpClient)
statemachine.state.test {
assertEquals(Loading, awaitItem()) // initial state
assertEquals(Error(cause = exception, countdown = 3), awaitItem())
assertEquals(Error(message = "A network error occurred", countdown = 3), awaitItem())
}
}
```
Expand All @@ -66,7 +66,7 @@ Now let's write a test that checks that pressing the retry button works:
```kotlin
@Test
fun `from Error state to Loading if RetryLoadingAction is dispatched`() = runTest {
val initialState = Error(cause = IOException("fake"), countdown = 3)
val initialState = Error(message = "A network error occurred", countdown = 3)
val statemachine = ItemListStateMachine(httpClient, initialState)

statemachine.state.test {
Expand All @@ -80,15 +80,15 @@ fun `from Error state to Loading if RetryLoadingAction is dispatched`() = runTes
}

@Test `once Error countdown is 0 move to Loading state`() = runTest {
val cause = IOException("fake")
val initialState = Error(cause = cause, countdown = 3)
val msg = "A network error occurred"
val initialState = Error(message = msg, countdown = 3)
val statemachine = ItemListStateMachine(httpClient, initialState)

statemachine.state.test {
assertEquals(initialState, awaitItem())
assertEquals(Error(cause, 2))
assertEquals(Error(cause, 1))
assertEquals(Error(cause, 0))
assertEquals(Error(msg, 2))
assertEquals(Error(msg, 1))
assertEquals(Error(msg, 0))
assertEquals(Loading, awaitItem())
}
}
Expand All @@ -111,7 +111,7 @@ suspend fun loadItemsAndMoveToContentOrError(state: State<Loading>): ChangedStat
val items = httpClient.loadItems()
state.override { ShowContent(items) }
} catch (t: Throwable) {
state.override { Error(cause = t, countdown = 3) }
state.override { Error(message = "A network error occurred", countdown = 3) }
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion docs/user-guide/1_FlowReduxStateMachine.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ sealed interface ListState {
data class ShowContent(val items: List<Item>) : ListState

// Error while loading happened
data class Error(val cause: Throwable) : ListState
data class Error(val message: String) : ListState
}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/user-guide/3_onEnter.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ItemListStateMachine(
val items = httpClient.loadItems() // loadItems() is a suspend function
state.override { ShowContent(items) } // return ShowContent from onEnter block
} catch (t: Throwable) {
state.override { Error(t) } // return Error state from onEnter block
state.override { Error("A network error occurred") } // return Error state from onEnter block
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/user-guide/5_onAction.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ItemListStateMachine(
val items = httpClient.loadItems()
state.override { ShowContent(items) }
} catch (t: Throwable) {
state.override { Error(t) }
state.override { Error("A network error occurred") }
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions docs/user-guide/6_collectWhileInState.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ To implement this let's first extend our `Error` state:

```kotlin
data class Error(
val cause: Throwable,
val message: String,
val countdown: Int // This value is decreased from 3 then 2 then 1 and represents the countdown value.
) : ListState
```
Expand All @@ -34,7 +34,10 @@ class ItemListStateMachine(
val items = httpClient.loadItems()
state.override { ShowContent(items) }
} catch (t: Throwable) {
state.override { Error(t, countdown = 3) } // countdown is new
state.override { Error(
message ="A network error occurred",
countdown = 3 // countdown is new
) }
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions docs/user-guide/8_condition.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Example: One could have also modeled the state for our `ItemListStateMachine` as
data class ListState(
val loading: Boolean, // true means loading, false means not loading
val items: List<Items>, // empty list if no items loaded yet
val error: Throwable?, // if not null we are in error state
val errorMessage: String?, // if not null we are in error state
val errorCountDown: Int? // the seconds for the error countdown
)
```
Expand Down Expand Up @@ -49,7 +49,12 @@ class ItemListStateMachine(
}
} catch (t: Throwable) {
state.mutate {
this.copy(loading = false, items = emptyList(), error = t, errorCountdown = 3)
this.copy(
loading = false,
items = emptyList(),
error = "A network error occurred",
errorCountdown = 3
)
}
}
}
Expand Down

0 comments on commit 37b5f6a

Please sign in to comment.