From c9805b8612cbcba9c2b017ff67333c6c08ea32ba Mon Sep 17 00:00:00 2001 From: arkon Date: Thu, 27 Apr 2023 22:45:30 -0400 Subject: [PATCH] Consolidate exception message formatting Closes #9408 --- .../presentation/browse/BrowseSourceScreen.kt | 9 ++------- .../presentation/util/ExceptionFormatter.kt | 19 +++++++++++++++++++ .../tachiyomi/ui/manga/MangaScreenModel.kt | 13 +++---------- 3 files changed, 24 insertions(+), 17 deletions(-) create mode 100644 app/src/main/java/eu/kanade/presentation/util/ExceptionFormatter.kt diff --git a/app/src/main/java/eu/kanade/presentation/browse/BrowseSourceScreen.kt b/app/src/main/java/eu/kanade/presentation/browse/BrowseSourceScreen.kt index 453f4735a..89a3e33df 100644 --- a/app/src/main/java/eu/kanade/presentation/browse/BrowseSourceScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/browse/BrowseSourceScreen.kt @@ -21,10 +21,10 @@ import eu.kanade.presentation.browse.components.BrowseSourceComfortableGrid import eu.kanade.presentation.browse.components.BrowseSourceCompactGrid import eu.kanade.presentation.browse.components.BrowseSourceList import eu.kanade.presentation.components.AppBar +import eu.kanade.presentation.util.formattedMessage import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.source.Source import kotlinx.coroutines.flow.StateFlow -import tachiyomi.data.source.NoResultsException import tachiyomi.domain.library.model.LibraryDisplayMode import tachiyomi.domain.manga.model.Manga import tachiyomi.domain.source.model.StubSource @@ -54,12 +54,7 @@ fun BrowseSourceContent( ?: mangaList.loadState.append.takeIf { it is LoadState.Error } val getErrorMessage: (LoadState.Error) -> String = { state -> - when { - state.error is NoResultsException -> context.getString(R.string.no_results_found) - state.error.message.isNullOrEmpty() -> "" - state.error.message.orEmpty().startsWith("HTTP error") -> "${state.error.message}: ${context.getString(R.string.http_error_hint)}" - else -> state.error.message.orEmpty() - } + with(context) { state.error.formattedMessage } } LaunchedEffect(errorState) { diff --git a/app/src/main/java/eu/kanade/presentation/util/ExceptionFormatter.kt b/app/src/main/java/eu/kanade/presentation/util/ExceptionFormatter.kt new file mode 100644 index 000000000..ff7ae44ae --- /dev/null +++ b/app/src/main/java/eu/kanade/presentation/util/ExceptionFormatter.kt @@ -0,0 +1,19 @@ +package eu.kanade.presentation.util + +import android.content.Context +import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.network.HttpException +import tachiyomi.data.source.NoResultsException +import tachiyomi.domain.source.model.SourceNotInstalledException +import java.io.IOException + +context(Context) +val Throwable.formattedMessage: String + get() = when { + this is NoResultsException -> getString(R.string.no_results_found) + this is SourceNotInstalledException -> getString(R.string.loader_not_implemented_error) + this is HttpException -> "$message: ${getString(R.string.http_error_hint)}" + this is IOException || this is Exception -> message ?: this::class.simpleName.orEmpty() + this::class.simpleName != null -> "${this::class.simpleName}: $message" + else -> message.orEmpty() + } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreenModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreenModel.kt index 875a10d77..1f268bedf 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreenModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreenModel.kt @@ -18,6 +18,7 @@ import eu.kanade.domain.manga.model.toSManga import eu.kanade.domain.ui.UiPreferences import eu.kanade.presentation.manga.DownloadAction import eu.kanade.presentation.manga.components.ChapterDownloadAction +import eu.kanade.presentation.util.formattedMessage import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.download.DownloadCache import eu.kanade.tachiyomi.data.download.DownloadManager @@ -227,7 +228,7 @@ class MangaInfoScreenModel( logcat(LogPriority.ERROR, e) coroutineScope.launch { - snackbarHostState.showSnackbar(message = e.snackbarMessage) + snackbarHostState.showSnackbar(message = with(context) { e.formattedMessage }) } } } @@ -517,7 +518,7 @@ class MangaInfoScreenModel( context.getString(R.string.no_chapters_error) } else { logcat(LogPriority.ERROR, e) - e.snackbarMessage + with(context) { e.formattedMessage } } coroutineScope.launch { @@ -1000,14 +1001,6 @@ class MangaInfoScreenModel( } } } - - private val Throwable.snackbarMessage: String - get() = when (val className = this::class.simpleName) { - null -> message ?: "" - "SourceNotInstalledException" -> context.getString(R.string.loader_not_implemented_error) - "Exception", "HttpException", "IOException" -> message ?: className - else -> "$className: $message" - } } sealed class MangaScreenState {