Log vault deserialization errors (#4353)

This commit is contained in:
David Perez
2024-11-21 13:35:57 -06:00
committed by GitHub
parent 245fcd7502
commit 57ea58fc3c
2 changed files with 33 additions and 6 deletions

View File

@@ -12,8 +12,27 @@ inline fun <reified T> Json.decodeFromStringOrNull(
): T? =
try {
decodeFromString(string = string)
} catch (e: SerializationException) {
} catch (_: SerializationException) {
null
} catch (e: IllegalArgumentException) {
} catch (_: IllegalArgumentException) {
null
}
/**
* Attempts to decode the given JSON [string] into the given type [T]. If there is an error in
* processing the JSON or deserializing, the exception is still throw after [onFailure] lambda is
* invoked.
*/
inline fun <reified T> Json.decodeFromStringWithErrorCallback(
string: String,
onFailure: (throwable: Throwable) -> Unit,
): T =
try {
decodeFromString(string = string)
} catch (se: SerializationException) {
onFailure(se)
throw se
} catch (iae: IllegalArgumentException) {
onFailure(iae)
throw iae
}

View File

@@ -2,6 +2,7 @@ package com.x8bit.bitwarden.data.vault.datasource.disk
import com.x8bit.bitwarden.data.platform.manager.dispatcher.DispatcherManager
import com.x8bit.bitwarden.data.platform.repository.util.bufferedMutableSharedFlow
import com.x8bit.bitwarden.data.platform.util.decodeFromStringWithErrorCallback
import com.x8bit.bitwarden.data.vault.datasource.disk.dao.CiphersDao
import com.x8bit.bitwarden.data.vault.datasource.disk.dao.CollectionsDao
import com.x8bit.bitwarden.data.vault.datasource.disk.dao.DomainsDao
@@ -24,6 +25,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import timber.log.Timber
/**
* Default implementation of [VaultDiskSource].
@@ -70,9 +72,9 @@ class VaultDiskSourceImpl(
entities
.map { entity ->
async {
json.decodeFromString<SyncResponseJson.Cipher>(
json.decodeFromStringWithErrorCallback<SyncResponseJson.Cipher>(
string = entity.cipherJson,
)
) { Timber.e(it, "Failed to deserialize Cipher in Vault") }
}
}
.awaitAll()
@@ -126,7 +128,11 @@ class VaultDiskSourceImpl(
.getDomains(userId)
.map { entity ->
withContext(dispatcherManager.default) {
entity?.domainsJson?.let { json.decodeFromString<SyncResponseJson.Domains>(it) }
entity?.domainsJson?.let { domains ->
json.decodeFromStringWithErrorCallback<SyncResponseJson.Domains>(
string = domains,
) { Timber.e(it, "Failed to deserialize Domains in Vault") }
}
}
}
@@ -192,7 +198,9 @@ class VaultDiskSourceImpl(
entities
.map { entity ->
async {
json.decodeFromString<SyncResponseJson.Send>(entity.sendJson)
json.decodeFromStringWithErrorCallback<SyncResponseJson.Send>(
string = entity.sendJson,
) { Timber.e(it, "Failed to deserialize Send in Vault") }
}
}
.awaitAll()