Skip to content

Commit

Permalink
Backport tweaks from #5975
Browse files Browse the repository at this point in the history
  • Loading branch information
BoD committed Jun 17, 2024
1 parent ea39ab2 commit 862b9b6
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions docs/source/tutorial/08-add-a-details-view.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ fun LaunchDetails(launchId: String) {
Now use the state to show the appropriate UI:
```kotlin title="app/src/main/java/com/example/rocketreserver/LaunchDetails.kt"
// Use the state
// Use the state
when (val s = state) {
Loading -> Loading()
is ProtocolError -> ErrorMessage("Oh no... A protocol error happened: ${s.exception.message}")
Expand Down Expand Up @@ -186,7 +186,7 @@ private sealed interface LaunchDetailsState {
}
```

Then, in the `try` block, check for `response.hasErrors()` and wrap the result in the new state:
Then, in the `when` block, check for `response.hasErrors()` and wrap the result in the new state:

```kotlin title="app/src/main/java/com/example/rocketreserver/LaunchDetails.kt"
state = when {
Expand All @@ -202,6 +202,16 @@ state = when {
}
```

You should also update the conditional expression to handle the `ApplicationError` case:
```kotlin title="app/src/main/java/com/example/rocketreserver/LaunchDetails.kt"
when (val s = state) {
Loading -> Loading()
is ApplicationError -> ErrorMessage(text = s.errors.first().message) // highlight-line
is ProtocolError -> ErrorMessage("Oh no... A protocol error happened: ${s.exception.message}")
is Success -> LaunchDetails(s.data)
}
```

`response.errors` contains details about any errors that occurred. Note that this code also null-checks `response.data!!`. In theory, a server should not set `response.data == null` and `response.hasErrors == false` at the same time, but the type system cannot guarantee this.

To trigger an error, replace `LaunchDetailsQuery(launchId)` with `LaunchDetailsQuery("invalidId")`. Disable airplane mode and select a launch. The server will send this response:
Expand Down

0 comments on commit 862b9b6

Please sign in to comment.