Throwable.displayString()

This commit is contained in:
GG Bond authored and Vladimir Dostoyevskiy committed 2024-02-24 22:29:41 +08:00
1 parent 7b581e45e1
commit 176d8be565
12 files changed
+55 -65

No files matched your search

@@ -37,7 +37,7 @@ import com.hippo.ehviewer.client.parser.GalleryDetailParser.parsePreviewPages
import com.hippo.ehviewer.client.parser.GalleryMultiPageViewerPTokenParser
import com.hippo.ehviewer.client.parser.GalleryPageUrlParser
import com.hippo.ehviewer.image.Image
import com.hippo.ehviewer.util.ExceptionUtils
import com.hippo.ehviewer.util.displayString
import com.hippo.unifile.UniFile
import eu.kanade.tachiyomi.util.lang.launchIO
import eu.kanade.tachiyomi.util.system.logcat
@@ -558,7 +558,7 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
}
throw it
}
updatePageState(index, STATE_FAILED, ExceptionUtils.getReadableString(it))
updatePageState(index, STATE_FAILED, it.displayString())
}
}
}
@@ -699,7 +699,7 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
error = when (it) {
is TimeoutCancellationException -> ERROR_TIMEOUT
is CancellationException -> throw it
else -> ExceptionUtils.getReadableString(it)
else -> it.displayString()
}
}
}
@@ -709,7 +709,7 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
is QuotaExceededException -> notifyGet509(index)
// TODO: Check IP ban
}
error = ExceptionUtils.getReadableString(it)
error = it.displayString()
}
updatePageState(index, STATE_FAILED, error)
}
@@ -125,8 +125,8 @@ import com.hippo.ehviewer.ui.tools.LocalDialogState
import com.hippo.ehviewer.ui.tools.LocalTouchSlopProvider
import com.hippo.ehviewer.ui.tools.LocalWindowSizeClass
import com.hippo.ehviewer.updater.AppUpdater
import com.hippo.ehviewer.util.ExceptionUtils
import com.hippo.ehviewer.util.addTextToClipboard
import com.hippo.ehviewer.util.displayString
import com.hippo.ehviewer.util.getParcelableExtraCompat
import com.hippo.ehviewer.util.getUrlFromClipboard
import com.hippo.ehviewer.util.isAtLeastQ
@@ -239,7 +239,7 @@ class MainActivity : EhActivity() {
}
}
}.onFailure {
snackbarState.showSnackbar(getString(R.string.update_failed, ExceptionUtils.getReadableString(it)))
snackbarState.showSnackbar(getString(R.string.update_failed, it.displayString()))
}
}
@@ -66,7 +66,7 @@ import com.hippo.ehviewer.ui.openBrowser
import com.hippo.ehviewer.ui.screen.popNavigate
import com.hippo.ehviewer.ui.tools.LocalDialogState
import com.hippo.ehviewer.ui.tools.LocalWindowSizeClass
import com.hippo.ehviewer.util.ExceptionUtils
import com.hippo.ehviewer.util.displayString
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootNavGraph
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
@@ -124,7 +124,7 @@ fun SignInScreen(navigator: DestinationsNavigator) {
text = {
Text(
"""
${ExceptionUtils.getReadableString(it)}
${it.displayString()}
${stringResource(R.string.sign_in_failed_tip)}
""".trimIndent(),
)
@@ -53,7 +53,7 @@ import com.hippo.ehviewer.icons.big.SadAndroid
import com.hippo.ehviewer.ui.screen.collectDetailSizeAsState
import com.hippo.ehviewer.ui.tools.FastScrollLazyVerticalGrid
import com.hippo.ehviewer.ui.tools.FastScrollLazyVerticalStaggeredGrid
import com.hippo.ehviewer.util.ExceptionUtils
import com.hippo.ehviewer.util.displayString
import kotlinx.coroutines.flow.drop
@Stable
@@ -184,9 +184,8 @@ fun GalleryList(
is LoadState.Error -> {
Surface {
ErrorTip(
modifier = Modifier.widthIn(max = 228.dp)
.clip(ShapeDefaults.Small).clickable { data.retry() },
text = ExceptionUtils.getReadableString(state.error),
modifier = Modifier.widthIn(max = 228.dp).clip(ShapeDefaults.Small).clickable { data.retry() },
text = state.error.displayString(),
)
}
}
@@ -10,7 +10,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.paging.LoadState
import com.hippo.ehviewer.R
import com.hippo.ehviewer.util.ExceptionUtils
import com.hippo.ehviewer.util.displayString
@Composable
fun LoadStateIndicator(
@@ -24,7 +24,7 @@ fun LoadStateIndicator(
is LoadState.Error -> {
Column(modifier = modifier, horizontalAlignment = Alignment.CenterHorizontally) {
Text(text = ExceptionUtils.getReadableString(state.error))
Text(text = state.error.displayString())
Button(onClick = retry) {
Text(text = stringResource(id = R.string.action_retry))
}
@@ -81,7 +81,7 @@ import com.hippo.ehviewer.ui.tools.LocalDialogState
import com.hippo.ehviewer.ui.tools.delegateSnapshotUpdate
import com.hippo.ehviewer.ui.tools.foldToLoadResult
import com.hippo.ehviewer.ui.tools.rememberInVM
import com.hippo.ehviewer.util.ExceptionUtils
import com.hippo.ehviewer.util.displayString
import com.hippo.ehviewer.util.findActivity
import com.hippo.ehviewer.util.mapToLongArray
import com.ramcosta.composedestinations.annotation.Destination
@@ -426,7 +426,7 @@ fun FavouritesScreen(navigator: DestinationsNavigator) {
}.onSuccess {
data.refresh()
}.onFailure {
snackbarState.showSnackbar(ExceptionUtils.getReadableString(it))
snackbarState.showSnackbar(it.displayString())
}
}
}
@@ -97,10 +97,10 @@ import com.hippo.ehviewer.ui.tools.snackBarPadding
import com.hippo.ehviewer.ui.tools.toAnnotatedString
import com.hippo.ehviewer.ui.tools.toBBCode
import com.hippo.ehviewer.ui.tools.updateSpan
import com.hippo.ehviewer.util.ExceptionUtils
import com.hippo.ehviewer.util.ReadableTime
import com.hippo.ehviewer.util.TextUrl
import com.hippo.ehviewer.util.addTextToClipboard
import com.hippo.ehviewer.util.displayString
import com.hippo.ehviewer.util.findActivity
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
@@ -210,7 +210,7 @@ fun GalleryCommentsScreen(gid: Long, navigator: DestinationsNavigator) {
snackbarState.showSnackbar(msg)
}.onFailure {
val text = if (commentId != -1L) editCommentFail else commentFail
snackbarState.showSnackbar(text + "\n" + ExceptionUtils.getReadableString(it))
snackbarState.showSnackbar(text + "\n" + it.displayString())
}
}
@@ -146,12 +146,12 @@ import com.hippo.ehviewer.ui.tools.rememberInVM
import com.hippo.ehviewer.ui.tools.rememberLambda
import com.hippo.ehviewer.util.AppConfig
import com.hippo.ehviewer.util.AppHelper
import com.hippo.ehviewer.util.ExceptionUtils
import com.hippo.ehviewer.util.FavouriteStatusRouter
import com.hippo.ehviewer.util.FileUtils
import com.hippo.ehviewer.util.addTextToClipboard
import com.hippo.ehviewer.util.awaitActivityResult
import com.hippo.ehviewer.util.bgWork
import com.hippo.ehviewer.util.displayString
import com.hippo.ehviewer.util.findActivity
import com.hippo.ehviewer.util.isAtLeastQ
import com.hippo.ehviewer.util.requestPermission
@@ -275,7 +275,7 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa
}
}.onFailure {
galleryInfo?.let { info -> EhDB.putHistoryInfo(info.findBaseInfo()) }
getDetailError = ExceptionUtils.getReadableString(it)
getDetailError = it.displayString()
}.getOrNull()
galleryDetail?.let {
EhDB.putHistoryInfo(it.galleryInfo)
@@ -382,7 +382,7 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa
}.onFailure {
when (it) {
is NoHAtHClientException -> snackbarState.showSnackbar(failureNoHath)
is EhException -> snackbarState.showSnackbar(ExceptionUtils.getReadableString(it))
is EhException -> snackbarState.showSnackbar(it.displayString())
else -> {
logcat(it)
snackbarState.showSnackbar(downloadFailed)
@@ -25,7 +25,7 @@ import com.hippo.ehviewer.client.EhEngine
import com.hippo.ehviewer.icons.EhIcons
import com.hippo.ehviewer.icons.big.SadAndroid
import com.hippo.ehviewer.ui.LockDrawer
import com.hippo.ehviewer.util.ExceptionUtils
import com.hippo.ehviewer.util.displayString
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import eu.kanade.tachiyomi.util.lang.withUIContext
@@ -50,7 +50,7 @@ fun ProgressScreen(gid: Long, token: String, page: Int, navigator: DestinationsN
navigator.navigate(gid asDstPageTo page with it)
}
}.onFailure {
error = ExceptionUtils.getReadableString(it)
error = it.displayString()
}
}
}
@@ -39,7 +39,7 @@ import com.hippo.ehviewer.ui.tools.toAnnotatedString
import com.hippo.ehviewer.updater.AppUpdater
import com.hippo.ehviewer.updater.Release
import com.hippo.ehviewer.util.AppConfig
import com.hippo.ehviewer.util.ExceptionUtils
import com.hippo.ehviewer.util.displayString
import com.hippo.ehviewer.util.installPackage
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
@@ -123,7 +123,7 @@ fun AboutScreen(navigator: DestinationsNavigator) {
dialogState.showNewVersion(context, it)
} ?: launchSnackBar(context.getString(R.string.already_latest_version))
}.onFailure {
launchSnackBar(context.getString(R.string.update_failed, ExceptionUtils.getReadableString(it)))
launchSnackBar(context.getString(R.string.update_failed, it.displayString()))
}
}
}
@@ -55,8 +55,8 @@ import com.hippo.ehviewer.ui.tools.LocalDialogState
import com.hippo.ehviewer.ui.tools.observed
import com.hippo.ehviewer.ui.tools.rememberedAccessor
import com.hippo.ehviewer.ui.tools.toAnnotatedString
import com.hippo.ehviewer.util.ExceptionUtils
import com.hippo.ehviewer.util.copyTextToClipboard
import com.hippo.ehviewer.util.displayString
import com.hippo.ehviewer.util.isAtLeastT
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
@@ -163,7 +163,7 @@ fun EhScreen(navigator: DestinationsNavigator) {
runSuspendCatching {
withIOContext { getImageLimits() }
}.onFailure {
error = ExceptionUtils.getReadableString(it)
error = it.displayString()
}
}
}
@@ -201,7 +201,7 @@ fun EhScreen(navigator: DestinationsNavigator) {
}.onSuccess {
launchSnackBar(resetImageLimitSucceed)
}.onFailure {
error = ExceptionUtils.getReadableString(it)
error = it.displayString()
}
}
}
@@ -26,44 +26,35 @@ import java.net.UnknownHostException
import javax.net.ssl.SSLException
import splitties.init.appCtx
fun Throwable.displayString(): String {
logcat(this)
val cause = cause
return when {
cause != null && this is IOException -> getReadableStringInternal(cause)
else -> getReadableStringInternal(this)
}
}
private fun getReadableStringInternal(e: Throwable) = when (e) {
is MalformedURLException -> appCtx.getString(R.string.error_invalid_url)
is SocketTimeoutException -> appCtx.getString(R.string.error_timeout)
is UnknownHostException -> appCtx.getString(R.string.error_unknown_host)
is StatusCodeException -> buildString {
append(appCtx.getString(R.string.error_bad_status_code, e.responseCode))
if (e.isIdentifiedResponseCode) {
append(", ").append(e.message)
}
}
is ProtocolException -> if (e.message!!.startsWith("Too many follow-up requests:")) {
appCtx.getString(R.string.error_redirection)
} else {
appCtx.getString(R.string.error_socket)
}
is SocketException, is SSLException -> appCtx.getString(R.string.error_socket)
else -> e.message ?: appCtx.getString(R.string.error_unknown)
}
object ExceptionUtils {
fun getReadableString(e: Throwable): String {
logcat(e)
val cause = e.cause
return if (e is IOException && cause != null) {
getReadableStringInternal(cause)
} else {
getReadableStringInternal(e)
}
}
private fun getReadableStringInternal(e: Throwable): String {
return when (e) {
is MalformedURLException -> appCtx.getString(R.string.error_invalid_url)
is SocketTimeoutException -> appCtx.getString(R.string.error_timeout)
is UnknownHostException -> appCtx.getString(R.string.error_unknown_host)
is StatusCodeException -> {
buildString {
append(appCtx.getString(R.string.error_bad_status_code, e.responseCode))
if (e.isIdentifiedResponseCode) {
append(", ").append(e.message)
}
}
}
is ProtocolException -> {
if (e.message!!.startsWith("Too many follow-up requests:")) {
appCtx.getString(R.string.error_redirection)
} else {
appCtx.getString(R.string.error_socket)
}
}
is SocketException, is SSLException -> appCtx.getString(R.string.error_socket)
else -> e.message ?: appCtx.getString(R.string.error_unknown)
}
}
fun throwIfFatal(t: Throwable) {
// values here derived from https://github.com/ReactiveX/RxJava/issues/748#issuecomment-32471495
when (t) {