From 01978548c611c1dba4a88c1dcf9ab91251f15f30 Mon Sep 17 00:00:00 2001 From: William Brawner Date: Thu, 22 Aug 2024 20:36:34 -0600 Subject: [PATCH] Add LocalOnlyException This is intended to enable local logging of some exceptions for debugging purposes without overrunning the remote crash reporter with issues that can't really be actioned upon --- .../com/wbrawner/simplemarkdown/MarkdownViewModel.kt | 3 ++- .../simplemarkdown/core/ErrorReporterTree.kt | 12 +++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/MarkdownViewModel.kt b/app/src/main/java/com/wbrawner/simplemarkdown/MarkdownViewModel.kt index f20312a..d3465b9 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/MarkdownViewModel.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/MarkdownViewModel.kt @@ -5,6 +5,7 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewmodel.CreationExtras +import com.wbrawner.simplemarkdown.core.LocalOnlyException import com.wbrawner.simplemarkdown.utility.FileHelper import com.wbrawner.simplemarkdown.utility.Preference import com.wbrawner.simplemarkdown.utility.PreferenceHelper @@ -109,7 +110,7 @@ class MarkdownViewModel( preferenceHelper[Preference.AUTOSAVE_URI] = actualLoadPath } ?: throw IllegalStateException("Opened file was null") } catch (e: Exception) { - Timber.e(e, "Failed to open file at path: $actualLoadPath") + Timber.e(LocalOnlyException(e), "Failed to open file at path: $actualLoadPath") _state.value = _state.value.copy( alert = AlertDialogModel( text = ParameterizedText(R.string.file_load_error), diff --git a/core/src/main/java/com/wbrawner/simplemarkdown/core/ErrorReporterTree.kt b/core/src/main/java/com/wbrawner/simplemarkdown/core/ErrorReporterTree.kt index 7a12067..7cdff8d 100644 --- a/core/src/main/java/com/wbrawner/simplemarkdown/core/ErrorReporterTree.kt +++ b/core/src/main/java/com/wbrawner/simplemarkdown/core/ErrorReporterTree.kt @@ -14,7 +14,9 @@ import timber.log.Timber class ErrorReporterTree private constructor(): Timber.Tree() { override fun log(priority: Int, tag: String?, message: String, t: Throwable?) { if (priority != Log.ERROR) return - t?.sendSilentlyWithAcra() + if (t !is LocalOnlyException) { + t?.sendSilentlyWithAcra() + } } companion object { @@ -25,6 +27,14 @@ class ErrorReporterTree private constructor(): Timber.Tree() { } } +/** + * An exception wrapper that prevents exceptions from being sent to an error reporter. Useful for + * logging things like IOExceptions that are useful to see locally but not so helpful if reported + */ +class LocalOnlyException(override val message: String?, override val cause: Throwable): Exception(message, cause) { + constructor(cause: Throwable): this(null, cause) +} + private suspend fun Application.createErrorReporterTree() = withContext(Dispatchers.IO) { initAcra { reportFormat = StringFormat.JSON