Skip to content

Commit

Permalink
Improve DescriptorUpdateWorker exception handling to avoid misleading…
Browse files Browse the repository at this point in the history
… reports
  • Loading branch information
sdsantos committed Nov 25, 2024
1 parent dac9752 commit 2027530
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import androidx.work.ForegroundInfo
import androidx.work.WorkerParameters
import androidx.work.workDataOf
import co.touchlab.kermit.Logger
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.first
import kotlinx.serialization.SerializationException
import kotlinx.serialization.encodeToString
import ooniprobe.composeapp.generated.resources.Dashboard_Running_Running
import ooniprobe.composeapp.generated.resources.Res
Expand Down Expand Up @@ -41,11 +43,14 @@ class DescriptorUpdateWorker(
}

override suspend fun doWork(): Result {
return coroutineScope {
val descriptors = getDescriptors() ?: return@coroutineScope Result.failure()
if (descriptors.isEmpty()) return@coroutineScope Result.success(buildWorkData(descriptors.map { it.id }))
try {
val descriptors = getDescriptors() ?: return Result.failure()
if (descriptors.isEmpty()) return Result.success(buildWorkData(descriptors.map { it.id }))
dependencies.getDescriptorUpdate.invoke(descriptors)
return@coroutineScope Result.success(buildWorkData(descriptors.map { it.id }))
return Result.success(buildWorkData(descriptors.map { it.id }))
} catch (e: CancellationException) {
Logger.w("DescriptorUpdateWorker: cancelled", e)
return Result.failure()
}
}

Expand All @@ -55,7 +60,10 @@ class DescriptorUpdateWorker(
try {
val ids = json.decodeFromString<List<InstalledTestDescriptorModel.Id>>(descriptorsJson)
return testDescriptorRepository.selectByRunIds(ids).first()
} catch (e: Exception) {
} catch (e: SerializationException) {
Logger.w("Could not start update worker: invalid configuration", e)
return null
}catch (e: IllegalArgumentException) {
Logger.w("Could not start update worker: invalid configuration", e)
return null
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.ooni.probe.domain

import co.touchlab.kermit.Logger
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.Json
import org.ooni.engine.Engine.MkException
import org.ooni.engine.models.OONIRunDescriptor
Expand All @@ -26,8 +27,11 @@ class FetchDescriptor(
json.decodeFromString<OONIRunDescriptor>(it).toModel().copy(
revisions = fetchRevisions(descriptorId).get()?.revisions,
)
} catch (e: Throwable) {
Logger.e(e) { "Failed to decode descriptor" }
} catch (e: SerializationException) {
Logger.e(e) { "Failed to decode descriptor $descriptorId" }
null
} catch (e: IllegalArgumentException) {
Logger.e(e) { "Failed to decode descriptor $descriptorId" }
null
}
} ?: throw MkException(Throwable("Failed to fetch descriptor"))
Expand All @@ -43,8 +47,11 @@ class FetchDescriptor(
result?.let {
try {
json.decodeFromString<OONIRunRevisions>(it)
} catch (e: Throwable) {
Logger.e(e) { "Failed to decode revisions" }
} catch (e: SerializationException) {
Logger.e(e) { "Failed to decode descriptor revisions $descriptorId" }
null
} catch (e: IllegalArgumentException) {
Logger.e(e) { "Failed to decode descriptor revisions $descriptorId" }
null
}
} ?: throw MkException(Throwable("Failed to fetch revision"))
Expand Down

0 comments on commit 2027530

Please sign in to comment.