refactor(common): make NetworkError also take in a Throwable parameter

This commit is contained in:
Harsh Shandilya 2022-09-30 10:43:52 +05:30
parent 24afea0412
commit cca50bf2d9
No known key found for this signature in database
4 changed files with 15 additions and 7 deletions

View file

@ -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()
}

View file

@ -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)
}

View file

@ -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)
}
}

View file

@ -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)
}