set Rx error handler

This commit is contained in:
Milos Kozak 2021-10-20 22:54:32 +02:00
parent c966dfda13
commit aee569e9a9
2 changed files with 35 additions and 6 deletions

View file

@ -15,8 +15,8 @@ import info.nightscout.androidaps.database.entities.UserEntry
import info.nightscout.androidaps.database.transactions.InsertIfNewByTimestampTherapyEventTransaction
import info.nightscout.androidaps.database.transactions.VersionChangeTransaction
import info.nightscout.androidaps.db.CompatDBHelper
import info.nightscout.androidaps.di.StaticInjector
import info.nightscout.androidaps.dependencyInjection.DaggerAppComponent
import info.nightscout.androidaps.di.StaticInjector
import info.nightscout.androidaps.interfaces.Config
import info.nightscout.androidaps.interfaces.ConfigBuilder
import info.nightscout.androidaps.interfaces.PluginBase
@ -36,8 +36,12 @@ import info.nightscout.androidaps.utils.locale.LocaleHelper.update
import info.nightscout.androidaps.utils.protection.PasswordCheck
import info.nightscout.androidaps.utils.sharedPreferences.SP
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.exceptions.UndeliverableException
import io.reactivex.plugins.RxJavaPlugins
import io.reactivex.rxkotlin.plusAssign
import net.danlew.android.joda.JodaTimeAndroid
import java.io.IOException
import java.net.SocketException
import javax.inject.Inject
class MainApp : DaggerApplication() {
@ -64,6 +68,7 @@ class MainApp : DaggerApplication() {
super.onCreate()
aapsLogger.debug("onCreate")
RxDogTag.install()
setRxErrorHandler()
update(this)
var gitRemote: String? = BuildConfig.REMOTE
@ -94,6 +99,34 @@ class MainApp : DaggerApplication() {
passwordCheck.passwordResetCheck(this)
}
private fun setRxErrorHandler() {
RxJavaPlugins.setErrorHandler { t: Throwable ->
var e = t
if (e is UndeliverableException) {
e = e.cause!!
}
if (e is IOException || e is SocketException) {
// fine, irrelevant network problem or API that throws on cancellation
return@setErrorHandler
}
if (e is InterruptedException) {
// fine, some blocking code was interrupted by a dispose call
return@setErrorHandler
}
if (e is NullPointerException || e is IllegalArgumentException) {
// that's likely a bug in the application
Thread.currentThread().uncaughtExceptionHandler?.uncaughtException(Thread.currentThread(), e)
return@setErrorHandler
}
if (e is IllegalStateException) {
// that's a bug in RxJava or in a custom operator
Thread.currentThread().uncaughtExceptionHandler?.uncaughtException(Thread.currentThread(), e)
return@setErrorHandler
}
aapsLogger.warn(LTag.CORE, "Undeliverable exception received, not sure what to do", e)
}
}
private fun doMigrations() {
// set values for different builds
if (!sp.contains(R.string.key_ns_alarms)) sp.putBoolean(R.string.key_ns_alarms, config.NSCLIENT)

View file

@ -191,11 +191,7 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() {
.subscribe({
// catch only events from iobCobCalculator
if (it.cause is EventCustomCalculationFinished)
try {
refreshLoop("EventAutosensCalculationFinished")
} catch (e: InterruptedException) {
fabricPrivacy.logException(e)
}
refreshLoop("EventAutosensCalculationFinished")
}, fabricPrivacy::logException)
disposable += rxBus
.toObservable(EventIobCalculationProgress::class.java)