Skip to content

Commit e7f340f

Browse files
committed
update error handling
1 parent 3a35024 commit e7f340f

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

final/app/src/main/kotlin/com/example/rocketreserver/LaunchDetails.kt

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,15 @@ import androidx.compose.ui.res.painterResource
2727
import androidx.compose.ui.tooling.preview.Preview
2828
import androidx.compose.ui.unit.dp
2929
import coil.compose.AsyncImage
30-
import com.apollographql.apollo.api.Error
31-
import com.apollographql.apollo.exception.ApolloException
32-
import com.example.rocketreserver.LaunchDetailsState.ApplicationError
30+
import com.apollographql.apollo.exception.ApolloNetworkException
3331
import com.example.rocketreserver.LaunchDetailsState.Loading
34-
import com.example.rocketreserver.LaunchDetailsState.ProtocolError
3532
import com.example.rocketreserver.LaunchDetailsState.Success
33+
import com.example.rocketreserver.LaunchDetailsState.Error
3634
import kotlinx.coroutines.launch
3735

3836
private sealed interface LaunchDetailsState {
3937
object Loading : LaunchDetailsState
40-
data class ProtocolError(val exception: ApolloException) : LaunchDetailsState
41-
data class ApplicationError(val errors: List<Error>) : LaunchDetailsState
38+
data class Error(val message: String) : LaunchDetailsState
4239
data class Success(val data: LaunchDetailsQuery.Data) : LaunchDetailsState
4340
}
4441

@@ -47,20 +44,29 @@ fun LaunchDetails(launchId: String, navigateToLogin: () -> Unit) {
4744
var state by remember { mutableStateOf<LaunchDetailsState>(Loading) }
4845
LaunchedEffect(Unit) {
4946
val response = apolloClient.query(LaunchDetailsQuery(launchId)).execute()
50-
state = if (response.data != null) {
51-
Success(response.data!!)
52-
} else {
53-
if (response.exception != null) {
54-
ProtocolError(response.exception!!)
55-
} else {
56-
ApplicationError(response.errors!!)
47+
state = when {
48+
response.errors.orEmpty().isNotEmpty() -> {
49+
// GraphQL error
50+
Error(response.errors!!.first().message)
51+
}
52+
response.exception is ApolloNetworkException -> {
53+
// Network error
54+
Error("Please check your network connectivity.")
55+
}
56+
response.data != null -> {
57+
// data (never partial)
58+
Success(response.data!!)
59+
}
60+
else -> {
61+
// Another fetch error, maybe a cache miss?
62+
// Or potentially a non-compliant server returning data: null without an error
63+
Error("Oh no... An error happened.")
5764
}
5865
}
5966
}
6067
when (val s = state) {
6168
Loading -> Loading()
62-
is ProtocolError -> ErrorMessage("Oh no... A protocol error happened: ${s.exception.message}")
63-
is ApplicationError -> ErrorMessage(s.errors[0].message)
69+
is Error -> ErrorMessage(s.message)
6470
is Success -> LaunchDetails(s.data, navigateToLogin)
6571
}
6672
}

0 commit comments

Comments
 (0)