From 3add01d57e6ebf8057b8420794f03552b5af3dbf Mon Sep 17 00:00:00 2001 From: Koitharu Date: Fri, 8 Jul 2022 10:35:18 +0300 Subject: [PATCH] Improve reporting of caught exceptions --- .../kotatsu/details/ui/DetailsActivity.kt | 8 ++++---- .../koitharu/kotatsu/utils/ext/ThrowableExt.kt | 17 ++++++++++++++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/koitharu/kotatsu/details/ui/DetailsActivity.kt b/app/src/main/java/org/koitharu/kotatsu/details/ui/DetailsActivity.kt index 84155b516..d4339645c 100644 --- a/app/src/main/java/org/koitharu/kotatsu/details/ui/DetailsActivity.kt +++ b/app/src/main/java/org/koitharu/kotatsu/details/ui/DetailsActivity.kt @@ -25,7 +25,6 @@ import com.google.android.material.snackbar.Snackbar import com.google.android.material.tabs.TabLayout import com.google.android.material.tabs.TabLayoutMediator import kotlinx.coroutines.launch -import org.acra.ktx.sendWithAcra import org.koin.android.ext.android.get import org.koin.androidx.viewmodel.ext.android.viewModel import org.koin.core.parameter.parametersOf @@ -39,7 +38,6 @@ import org.koitharu.kotatsu.core.os.ShortcutsRepository import org.koitharu.kotatsu.databinding.ActivityDetailsBinding import org.koitharu.kotatsu.details.ui.adapter.BranchesAdapter import org.koitharu.kotatsu.download.ui.service.DownloadService -import org.koitharu.kotatsu.parsers.exception.ParseException import org.koitharu.kotatsu.parsers.model.Manga import org.koitharu.kotatsu.parsers.model.MangaSource import org.koitharu.kotatsu.parsers.util.mapNotNullToSet @@ -48,6 +46,8 @@ import org.koitharu.kotatsu.reader.ui.ReaderState import org.koitharu.kotatsu.scrobbling.ui.selector.ScrobblingSelectorBottomSheet import org.koitharu.kotatsu.search.ui.multi.MultiSearchActivity import org.koitharu.kotatsu.utils.ext.getDisplayMessage +import org.koitharu.kotatsu.utils.ext.isReportable +import org.koitharu.kotatsu.utils.ext.report class DetailsActivity : BaseActivity(), @@ -118,7 +118,7 @@ class DetailsActivity : Toast.makeText(this, e.getDisplayMessage(resources), Toast.LENGTH_LONG).show() finishAfterTransition() } - e is ParseException || e is IllegalArgumentException || e is IllegalStateException -> { + e.isReportable() -> { binding.snackbar.show( messageText = e.getDisplayMessage(resources), actionId = R.string.report, @@ -128,7 +128,7 @@ class DetailsActivity : Snackbar.LENGTH_LONG }, onActionClick = { - e.sendWithAcra() + e.report("DetailsActivity::onError") dismiss() } ) diff --git a/app/src/main/java/org/koitharu/kotatsu/utils/ext/ThrowableExt.kt b/app/src/main/java/org/koitharu/kotatsu/utils/ext/ThrowableExt.kt index 6dac10c3b..7b7f76408 100644 --- a/app/src/main/java/org/koitharu/kotatsu/utils/ext/ThrowableExt.kt +++ b/app/src/main/java/org/koitharu/kotatsu/utils/ext/ThrowableExt.kt @@ -4,12 +4,14 @@ import android.content.ActivityNotFoundException import android.content.res.Resources import okio.FileNotFoundException import org.acra.ACRA +import org.acra.ktx.sendWithAcra import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.exceptions.CloudFlareProtectedException import org.koitharu.kotatsu.core.exceptions.EmptyHistoryException import org.koitharu.kotatsu.core.exceptions.UnsupportedFileException import org.koitharu.kotatsu.core.exceptions.WrongPasswordException import org.koitharu.kotatsu.parsers.exception.AuthRequiredException +import org.koitharu.kotatsu.parsers.exception.ParseException import org.koitharu.kotatsu.parsers.model.Manga import java.net.SocketTimeoutException @@ -26,4 +28,17 @@ fun Throwable.getDisplayMessage(resources: Resources) = when (this) { else -> localizedMessage ?: resources.getString(R.string.error_occurred) } -fun ACRA.setCurrentManga(manga: Manga?) = errorReporter.putCustomData("manga", manga?.publicUrl.toString()) \ No newline at end of file +fun Throwable.isReportable(): Boolean { + if (this !is Exception) { + return true + } + return this is ParseException || this is IllegalArgumentException || this is IllegalStateException +} + +fun Throwable.report(message: String?) { + CaughtException(this, message).sendWithAcra() +} + +fun ACRA.setCurrentManga(manga: Manga?) = errorReporter.putCustomData("manga", manga?.publicUrl.toString()) + +private class CaughtException(cause: Throwable, override val message: String?) : RuntimeException(cause) \ No newline at end of file