Skip to content

Commit

Permalink
Setup uncaught exception error handling.
Browse files Browse the repository at this point in the history
* Updated `setUpUncaughtErrorHandlerForOnlineLibrary` to handle `RxJavaPlugin` errors.
  • Loading branch information
MohitMaliDeveloper committed Jul 26, 2024
1 parent 0c0ddd3 commit f29634e
Showing 1 changed file with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ import androidx.lifecycle.ViewModel
import io.reactivex.Flowable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import io.reactivex.exceptions.UndeliverableException
import io.reactivex.functions.BiFunction
import io.reactivex.functions.Function6
import io.reactivex.plugins.RxJavaPlugins
import io.reactivex.processors.BehaviorProcessor
import io.reactivex.processors.PublishProcessor
import io.reactivex.schedulers.Schedulers
Expand All @@ -47,6 +49,7 @@ import org.kiwix.kiwixmobile.core.extensions.calculateSearchMatches
import org.kiwix.kiwixmobile.core.extensions.registerReceiver
import org.kiwix.kiwixmobile.core.utils.BookUtils
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.core.utils.files.Log
import org.kiwix.kiwixmobile.core.utils.files.ScanningProgressListener
import org.kiwix.kiwixmobile.core.zim_manager.Language
import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.SelectionMode.MULTI
Expand Down Expand Up @@ -74,13 +77,16 @@ import org.kiwix.kiwixmobile.zimManager.libraryView.adapter.LibraryListItem
import org.kiwix.kiwixmobile.zimManager.libraryView.adapter.LibraryListItem.BookItem
import org.kiwix.kiwixmobile.zimManager.libraryView.adapter.LibraryListItem.DividerItem
import org.kiwix.kiwixmobile.zimManager.libraryView.adapter.LibraryListItem.LibraryDownloadItem
import java.io.IOException
import java.util.LinkedList
import java.util.Locale
import java.util.concurrent.TimeUnit.MILLISECONDS
import javax.inject.Inject

const val DEFAULT_PROGRESS = 0
const val MAX_PROGRESS = 100
private const val TAG_RX_JAVA_DEFAULT_ERROR_HANDLER = "RxJavaDefaultErrorHandler"

class ZimManageViewModel @Inject constructor(
private val downloadDao: FetchDownloadDao,
private val bookDao: NewBookDao,
Expand Down Expand Up @@ -156,7 +162,9 @@ class ZimManageViewModel @Inject constructor(
updateNetworkStates(),
requestsAndConnectivtyChangesToLibraryRequests(networkLibrary),
fileSelectActions()
)
).also {
setUpUncaughtErrorHandlerForOnlineLibrary(networkLibrary)
}
}

private fun fileSelectActions() = fileSelectActions.subscribe({
Expand Down Expand Up @@ -288,6 +296,11 @@ class ZimManageViewModel @Inject constructor(
Function6(::combineLibrarySources)
)
.doOnNext { libraryListIsRefreshing.postValue(false) }
.doOnError { throwable ->
if (throwable is OutOfMemoryError) {
Log.e("ZimManageViewModel", "Error----${throwable.printStackTrace()}")
}
}
.subscribeOn(Schedulers.io())
.subscribe(
libraryItems::postValue,
Expand Down Expand Up @@ -512,4 +525,35 @@ class ZimManageViewModel @Inject constructor(
}
)
}

private fun setUpUncaughtErrorHandlerForOnlineLibrary(
library: PublishProcessor<LibraryNetworkEntity>
) {
RxJavaPlugins.setErrorHandler { exception ->
if (exception is RuntimeException && exception.cause == IOException()) {
Log.i(
TAG_RX_JAVA_DEFAULT_ERROR_HANDLER,
"Caught undeliverable exception: ${exception.cause}"
)
}
when (exception) {
is UndeliverableException -> {
library.onNext(
LibraryNetworkEntity().apply { book = LinkedList() }
).also {
Log.i(
TAG_RX_JAVA_DEFAULT_ERROR_HANDLER,
"Caught undeliverable exception: ${exception.cause}"
)
}
}

else -> {
Thread.currentThread().also { thread ->
thread.uncaughtExceptionHandler?.uncaughtException(thread, exception)
}
}
}
}
}
}

0 comments on commit f29634e

Please sign in to comment.