Skip to content

Commit 62653b4

Browse files
committed
Backport tweaks from #5975
1 parent 92bb4b6 commit 62653b4

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

docs/source/tutorial/08-add-a-details-view.mdx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ fun LaunchDetails(launchId: String) {
158158
Now use the state to show the appropriate UI:
159159
160160
```kotlin title="app/src/main/java/com/example/rocketreserver/LaunchDetails.kt"
161-
// Use the state
161+
// Use the state
162162
when (val s = state) {
163163
Loading -> Loading()
164164
is ProtocolError -> ErrorMessage("Oh no... A protocol error happened: ${s.exception.message}")
@@ -186,7 +186,7 @@ private sealed interface LaunchDetailsState {
186186
}
187187
```
188188

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

191191
```kotlin title="app/src/main/java/com/example/rocketreserver/LaunchDetails.kt"
192192
state = when {
@@ -202,6 +202,16 @@ state = when {
202202
}
203203
```
204204

205+
You should also update the conditional expression to handle the `ApplicationError` case:
206+
```kotlin title="app/src/main/java/com/example/rocketreserver/LaunchDetails.kt"
207+
when (val s = state) {
208+
Loading -> Loading()
209+
is ApplicationError -> ErrorMessage(text = s.errors.first().message) // highlight-line
210+
is ProtocolError -> ErrorMessage("Oh no... A protocol error happened: ${s.exception.message}")
211+
is Success -> LaunchDetails(s.data)
212+
}
213+
```
214+
205215
`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.
206216

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

0 commit comments

Comments
 (0)