mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-13 18:27:00 +05:30
refactor(common): make NetworkError
also take in a Throwable
parameter
This commit is contained in:
parent
24afea0412
commit
cca50bf2d9
4 changed files with 15 additions and 7 deletions
|
@ -2,6 +2,6 @@ package dev.msfjarvis.claw.common
|
|||
|
||||
sealed class NetworkState {
|
||||
class Success<T>(val data: T) : NetworkState()
|
||||
class Error(val message: String) : NetworkState()
|
||||
class Error(val error: Throwable, val description: String) : NetworkState()
|
||||
object Loading : NetworkState()
|
||||
}
|
||||
|
|
|
@ -79,7 +79,7 @@ fun CommentsPage(
|
|||
runCatching { getDetails(postId) }
|
||||
.fold(
|
||||
onSuccess = { details -> value = Success(details) },
|
||||
onFailure = { value = Error("Failed to load comments") }
|
||||
onFailure = { value = Error(error = it, description = "Failed to load comments") }
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -92,7 +92,8 @@ fun CommentsPage(
|
|||
)
|
||||
}
|
||||
is Error -> {
|
||||
NetworkError((postDetails as Error).message)
|
||||
val error = postDetails as Error
|
||||
NetworkError(label = error.description, error = error.error)
|
||||
}
|
||||
Loading -> ProgressBar(modifier)
|
||||
}
|
||||
|
|
|
@ -9,8 +9,8 @@ import androidx.compose.ui.Alignment
|
|||
import androidx.compose.ui.Modifier
|
||||
|
||||
@Composable
|
||||
fun NetworkError(message: String, modifier: Modifier = Modifier) {
|
||||
fun NetworkError(label: String, error: Throwable, modifier: Modifier = Modifier) {
|
||||
Box(modifier = modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
|
||||
Text(text = message, style = MaterialTheme.typography.displayMedium)
|
||||
Text(text = label, style = MaterialTheme.typography.displayMedium)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,7 +39,13 @@ fun UserProfile(
|
|||
runCatching { getProfile(username) }
|
||||
.fold(
|
||||
onSuccess = { profile -> value = Success(profile) },
|
||||
onFailure = { value = Error("Failed to load profile for $username") }
|
||||
onFailure = {
|
||||
value =
|
||||
Error(
|
||||
error = it,
|
||||
description = "Failed to load profile for $username",
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
when (user) {
|
||||
|
@ -47,7 +53,8 @@ fun UserProfile(
|
|||
UserProfileInternal((user as Success<User>).data)
|
||||
}
|
||||
is Error -> {
|
||||
NetworkError((user as Error).message)
|
||||
val error = user as Error
|
||||
NetworkError(label = error.description, error = error.error)
|
||||
}
|
||||
Loading -> ProgressBar(modifier)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue