Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Verified Commit 7990f021 authored by Fahim M. Choudhury's avatar Fahim M. Choudhury
Browse files

refactor: reuse error handling in InstallOrchestrator

parent a5f3398b
Loading
Loading
Loading
Loading
+19 −33
Original line number Diff line number Diff line
@@ -49,11 +49,7 @@ class InstallOrchestrator @Inject constructor(
            runCatching {
                cancelFailedDownloads()
            }.onFailure { throwable ->
                when (throwable) {
                    is CancellationException -> throw throwable
                    is Exception -> Timber.e(throwable, "Failed to reconcile startup downloads")
                    else -> throw throwable
                }
                handleFailure(throwable, "Failed to reconcile startup downloads")
            }
            observeDownloads()
        }
@@ -67,14 +63,18 @@ class InstallOrchestrator @Inject constructor(
                        ?.let { queuedDownload -> trigger(queuedDownload) }
                }
            }.onFailure { throwable ->
                handleFailure(throwable, "Failed to enqueue download worker")
            }
        }.launchIn(scope)
    }

    private fun handleFailure(throwable: Throwable, errorMessage: String) {
        when (throwable) {
            is CancellationException -> throw throwable
                    is Exception -> Timber.e(throwable, "Failed to enqueue download worker")
            is Exception -> Timber.e(throwable, errorMessage)
            else -> throw throwable
        }
    }
        }.launchIn(scope)
    }

    private suspend fun trigger(download: AppInstall) {
        val uniqueWorkName = InstallWorkManager.getUniqueWorkName(download.packageName)
@@ -84,19 +84,12 @@ class InstallOrchestrator @Inject constructor(
            operation.await()
            Timber.d("INSTALL: Successfully enqueued unique work for ${download.name}: $uniqueWorkName")
        }.onFailure { throwable ->
            when (throwable) {
                is CancellationException -> throw throwable
                is Exception -> {
                    Timber.e(
            handleFailure(
                throwable,
                "INSTALL: Failed to enqueue unique work for ${download.name}: $uniqueWorkName"
            )
            appManagerWrapper.installationIssue(download)
        }

                else -> throw throwable
            }
        }
    }

    private suspend fun cancelFailedDownloads() {
@@ -136,18 +129,11 @@ class InstallOrchestrator @Inject constructor(
                    }
                }
            }.onFailure { throwable ->
                when (throwable) {
                    is CancellationException -> throw throwable
                    is Exception -> {
                        Timber.e(
                handleFailure(
                    throwable,
                    "INSTALL: Failed to reconcile startup state for ${app.name}/${app.packageName}"
                )
            }

                    else -> throw throwable
                }
            }
        }
    }