Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve DescriptorUpdateWorker exception handling to avoid misleading reports #303

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import androidx.work.ForegroundInfo
import androidx.work.WorkerParameters
import androidx.work.workDataOf
import co.touchlab.kermit.Logger
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.CancellationException
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 +42,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 +59,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