From 176d8be5656a518cd42d484224aeadb8d663d108 Mon Sep 17 00:00:00 2001 From: GG Bond Date: Sat, 24 Feb 2024 21:08:59 +0800 Subject: [PATCH] Throwable.displayString() --- .../com/hippo/ehviewer/spider/SpiderQueen.kt | 8 +-- .../com/hippo/ehviewer/ui/MainActivity.kt | 4 +- .../hippo/ehviewer/ui/login/SignInScreen.kt | 4 +- .../com/hippo/ehviewer/ui/main/GalleryList.kt | 7 +- .../ehviewer/ui/main/LoadStateIndicator.kt | 4 +- .../ehviewer/ui/screen/FavoritesScreen.kt | 4 +- .../ui/screen/GalleryCommentsScreen.kt | 4 +- .../ehviewer/ui/screen/GalleryDetailScreen.kt | 6 +- .../ehviewer/ui/screen/ProgressScreen.kt | 4 +- .../hippo/ehviewer/ui/settings/AboutScreen.kt | 4 +- .../hippo/ehviewer/ui/settings/EhScreen.kt | 6 +- .../com/hippo/ehviewer/util/ExceptionUtils.kt | 65 ++++++++----------- 12 files changed, 55 insertions(+), 65 deletions(-) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/spider/SpiderQueen.kt b/app/src/main/kotlin/com/hippo/ehviewer/spider/SpiderQueen.kt index 8aec2d664..d97bc44d3 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/spider/SpiderQueen.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/spider/SpiderQueen.kt @@ -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) } diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/MainActivity.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/MainActivity.kt index e146cd3c9..8f1bfa0a4 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/MainActivity.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/MainActivity.kt @@ -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())) } } diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/login/SignInScreen.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/login/SignInScreen.kt index 0c6a79033..728e611f2 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/login/SignInScreen.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/login/SignInScreen.kt @@ -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(), ) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/main/GalleryList.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/main/GalleryList.kt index fc81f5703..6c1baf127 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/main/GalleryList.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/main/GalleryList.kt @@ -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(), ) } } diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/main/LoadStateIndicator.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/main/LoadStateIndicator.kt index edbdd6145..2baafc6b5 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/main/LoadStateIndicator.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/main/LoadStateIndicator.kt @@ -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)) } diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/FavoritesScreen.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/FavoritesScreen.kt index 73e47e0e4..0f1b55aa4 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/FavoritesScreen.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/FavoritesScreen.kt @@ -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()) } } } diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryCommentsScreen.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryCommentsScreen.kt index 0e7e13cf7..e91e79d87 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryCommentsScreen.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryCommentsScreen.kt @@ -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()) } } diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryDetailScreen.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryDetailScreen.kt index e9a5e3880..cb06c1796 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryDetailScreen.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryDetailScreen.kt @@ -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) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/ProgressScreen.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/ProgressScreen.kt index 1b012d645..b01b986e1 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/ProgressScreen.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/ProgressScreen.kt @@ -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() } } } diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/AboutScreen.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/AboutScreen.kt index 791abb7d3..8db93f1e7 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/AboutScreen.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/AboutScreen.kt @@ -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())) } } } diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/EhScreen.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/EhScreen.kt index 3e0938699..d30de6a02 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/EhScreen.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/EhScreen.kt @@ -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() } } } diff --git a/app/src/main/kotlin/com/hippo/ehviewer/util/ExceptionUtils.kt b/app/src/main/kotlin/com/hippo/ehviewer/util/ExceptionUtils.kt index 904eaa28c..d01d29815 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/util/ExceptionUtils.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/util/ExceptionUtils.kt @@ -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) {