fix(telemetry): don't downgrade wrapped programming errors

isEnvironmentalFault() returned true for any RecoverableException in the
chain, but the worker wraps every failure into InitException /
UsbCommunicationException, so NPEs, IllegalArgumentExceptions and
coroutine cancellations were tagged recoverable and hidden as warnings.
Return false when the chain contains a programming error.

Fixes ETCHDROID-A4
Fixes ETCHDROID-AJ
Fixes ETCHDROID-9Y
Fixes ETCHDROID-Z
Fixes ETCHDROID-9Z
Fixes ETCHDROID-AX
Fixes ETCHDROID-CH
This commit is contained in:
Davide Depau committed 2026-10-30 18:35:08 +01:00
1 parent a3507432e2
commit 2473cbb00d
2 files changed
+34 -3

No files matched your search

@@ -12,14 +12,22 @@ val RecoverableException.isUnplugged: Boolean
/**
* A known non-app-fault: a recoverable EtchDroid error or a libusb hardware error
* anywhere in the cause chain. Used to de-prioritize such failures in telemetry.
* anywhere in the cause chain, unless the chain also contains a programming error
* (NPE, IllegalArgument/IllegalState incl. coroutine cancellation, IndexOutOfBounds,
* ClassCast), which the worker may have wrapped. Used to de-prioritize such failures
* in telemetry.
*/
fun Throwable?.isEnvironmentalFault(): Boolean {
var t = this
var environmental = false
val seen = HashSet<Throwable>()
while (t != null && seen.add(t)) {
if (t is RecoverableException || t is LibusbException) return true
when (t) {
is NullPointerException, is IllegalArgumentException, is IllegalStateException,
is IndexOutOfBoundsException, is ClassCastException -> return false
is RecoverableException, is LibusbException -> environmental = true
}
t = t.cause
}
return false
return environmental
}
@@ -0,0 +1,23 @@
package eu.depau.etchdroid
import eu.depau.etchdroid.utils.exception.InitException
import eu.depau.etchdroid.utils.exception.UsbCommunicationException
import eu.depau.etchdroid.utils.exception.base.isEnvironmentalFault
import kotlinx.coroutines.CancellationException
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import java.io.IOException
class EnvironmentalFaultTest {
@Test
fun classifiesCauseChain() {
assertTrue(UsbCommunicationException(IOException("io")).isEnvironmentalFault())
assertTrue(InitException("init").isEnvironmentalFault())
assertFalse(InitException("init", NullPointerException()).isEnvironmentalFault())
assertFalse(UsbCommunicationException(IllegalArgumentException()).isEnvironmentalFault())
assertFalse(UsbCommunicationException(CancellationException("x")).isEnvironmentalFault())
assertFalse(IOException("plain").isEnvironmentalFault())
assertFalse(null.isEnvironmentalFault())
}
}