fix: don't crash app when cronet thread fails

This commit is contained in:
Bnyro 2024-05-06 12:58:20 +02:00
parent 7ac50280fd
commit 0670bbf656

View File

@ -1,11 +1,17 @@
package com.github.libretube.util
import android.os.Looper
import com.github.libretube.helpers.PreferenceHelper
class ExceptionHandler(
private val defaultExceptionHandler: Thread.UncaughtExceptionHandler?
) : Thread.UncaughtExceptionHandler {
override fun uncaughtException(thread: Thread, exc: Throwable) {
// if the exception is not on the main thread, the app is still functional and responsive
// hence the app doesn't have to be quit fully
// work around for Cronet spawning different threads with uncaught Exception when used with Coil
if (thread.id != Looper.getMainLooper().thread.id) return
// save the error log
PreferenceHelper.saveErrorLog(exc.stackTraceToString())
// throw the exception with the default exception handler to make the app crash