Skip to content

Commit 57ea58f

Browse files
Log vault deserialization errors (#4353)
1 parent 245fcd7 commit 57ea58f

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

app/src/main/java/com/x8bit/bitwarden/data/platform/util/JsonExtensions.kt

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,27 @@ inline fun <reified T> Json.decodeFromStringOrNull(
1212
): T? =
1313
try {
1414
decodeFromString(string = string)
15-
} catch (e: SerializationException) {
15+
} catch (_: SerializationException) {
1616
null
17-
} catch (e: IllegalArgumentException) {
17+
} catch (_: IllegalArgumentException) {
1818
null
1919
}
20+
21+
/**
22+
* Attempts to decode the given JSON [string] into the given type [T]. If there is an error in
23+
* processing the JSON or deserializing, the exception is still throw after [onFailure] lambda is
24+
* invoked.
25+
*/
26+
inline fun <reified T> Json.decodeFromStringWithErrorCallback(
27+
string: String,
28+
onFailure: (throwable: Throwable) -> Unit,
29+
): T =
30+
try {
31+
decodeFromString(string = string)
32+
} catch (se: SerializationException) {
33+
onFailure(se)
34+
throw se
35+
} catch (iae: IllegalArgumentException) {
36+
onFailure(iae)
37+
throw iae
38+
}

app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/disk/VaultDiskSourceImpl.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.x8bit.bitwarden.data.vault.datasource.disk
22

33
import com.x8bit.bitwarden.data.platform.manager.dispatcher.DispatcherManager
44
import com.x8bit.bitwarden.data.platform.repository.util.bufferedMutableSharedFlow
5+
import com.x8bit.bitwarden.data.platform.util.decodeFromStringWithErrorCallback
56
import com.x8bit.bitwarden.data.vault.datasource.disk.dao.CiphersDao
67
import com.x8bit.bitwarden.data.vault.datasource.disk.dao.CollectionsDao
78
import com.x8bit.bitwarden.data.vault.datasource.disk.dao.DomainsDao
@@ -24,6 +25,7 @@ import kotlinx.coroutines.launch
2425
import kotlinx.coroutines.withContext
2526
import kotlinx.serialization.encodeToString
2627
import kotlinx.serialization.json.Json
28+
import timber.log.Timber
2729

2830
/**
2931
* Default implementation of [VaultDiskSource].
@@ -70,9 +72,9 @@ class VaultDiskSourceImpl(
7072
entities
7173
.map { entity ->
7274
async {
73-
json.decodeFromString<SyncResponseJson.Cipher>(
75+
json.decodeFromStringWithErrorCallback<SyncResponseJson.Cipher>(
7476
string = entity.cipherJson,
75-
)
77+
) { Timber.e(it, "Failed to deserialize Cipher in Vault") }
7678
}
7779
}
7880
.awaitAll()
@@ -126,7 +128,11 @@ class VaultDiskSourceImpl(
126128
.getDomains(userId)
127129
.map { entity ->
128130
withContext(dispatcherManager.default) {
129-
entity?.domainsJson?.let { json.decodeFromString<SyncResponseJson.Domains>(it) }
131+
entity?.domainsJson?.let { domains ->
132+
json.decodeFromStringWithErrorCallback<SyncResponseJson.Domains>(
133+
string = domains,
134+
) { Timber.e(it, "Failed to deserialize Domains in Vault") }
135+
}
130136
}
131137
}
132138

@@ -192,7 +198,9 @@ class VaultDiskSourceImpl(
192198
entities
193199
.map { entity ->
194200
async {
195-
json.decodeFromString<SyncResponseJson.Send>(entity.sendJson)
201+
json.decodeFromStringWithErrorCallback<SyncResponseJson.Send>(
202+
string = entity.sendJson,
203+
) { Timber.e(it, "Failed to deserialize Send in Vault") }
196204
}
197205
}
198206
.awaitAll()

0 commit comments

Comments
 (0)