Skip to content

Commit

Permalink
feat: add exception type filter during exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehardy committed Dec 1, 2024
1 parent e68c8fc commit 05d76e8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package com.ichi2.anki.servicelayer

import androidx.annotation.VisibleForTesting
import com.ichi2.anki.exception.StorageAccessException
import net.ankiweb.rsdroid.exceptions.BackendNetworkException
import net.ankiweb.rsdroid.exceptions.BackendSyncException
import net.ankiweb.rsdroid.exceptions.BackendSyncException.BackendSyncServerMessageException
import timber.log.Timber

Expand Down Expand Up @@ -79,7 +82,22 @@ object ThrowableFilterService {
}

fun shouldDiscardThrowable(t: Throwable): Boolean {
return !t.safeFromPII()
// Note that an exception may have a nested BackendSyncException,
// so we check if it is safe from PII despite also filtering by type
return exceptionIsUnwanted(t) || !t.safeFromPII()
}

// There are few exception types that are common, but are unwanted in
// our analytics or crash report service because they are not actionable
fun exceptionIsUnwanted(t: Throwable): Boolean {
Timber.v("exceptionIsUnwanted - examining %s", t.javaClass.simpleName)
when (t) {
is BackendNetworkException -> return true
is BackendSyncException -> return true
is StorageAccessException -> return true
}
Timber.v("exceptionIsUnwanted - exception was wanted")
return false
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ package com.ichi2.anki

import androidx.test.ext.junit.runners.AndroidJUnit4
import anki.backend.BackendError
import com.ichi2.anki.exception.StorageAccessException
import com.ichi2.anki.servicelayer.ThrowableFilterService
import com.ichi2.anki.servicelayer.ThrowableFilterService.safeFromPII
import com.ichi2.testutils.JvmTest
import net.ankiweb.rsdroid.exceptions.BackendDeckIsFilteredException
import net.ankiweb.rsdroid.exceptions.BackendNetworkException
import net.ankiweb.rsdroid.exceptions.BackendSyncException
import net.ankiweb.rsdroid.exceptions.BackendSyncException.BackendSyncServerMessageException
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -44,4 +48,18 @@ class ThrowableFilterServiceTest : JvmTest() {
val exception2 = Exception("", Exception("", exception1))
assertFalse(exception2.safeFromPII(), "Nested exception reported as not safe from PII")
}

@Test
fun `exceptions are discarded correctly by type`() {
// regular exceptions should go through
assertFalse(ThrowableFilterService.shouldDiscardThrowable(Exception("wanted")))

// exceptions of known unwanted types should not go through
val exception1 = BackendNetworkException(BackendError.newBuilder().build())
assertTrue(ThrowableFilterService.shouldDiscardThrowable(exception1))
val exception2 = BackendSyncException(BackendError.newBuilder().build())
assertTrue(ThrowableFilterService.shouldDiscardThrowable(exception2))
val exception3 = StorageAccessException("test exception")
assertTrue(ThrowableFilterService.shouldDiscardThrowable(exception3))
}
}

0 comments on commit 05d76e8

Please sign in to comment.