Skip to content

Commit d0bfe58

Browse files
committed
storage exceptions
1 parent a245067 commit d0bfe58

File tree

4 files changed

+44
-22
lines changed

4 files changed

+44
-22
lines changed

shared-storage/schemas/org.asyncstorage.shared_storage.database.StorageDatabase/3.json renamed to shared-storage/schemas/org.asyncstorage.shared_storage.database.StorageDatabase/1.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"formatVersion": 1,
33
"database": {
4-
"version": 3,
4+
"version": 1,
55
"identityHash": "4d8baac8b3ec8e6bbde4d79eb33fbcd9",
66
"entities": [
77
{

shared-storage/src/commonMain/kotlin/org/asyncstorage/shared_storage/SharedStorageImpl.kt

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.asyncstorage.shared_storage
22

3-
import androidx.sqlite.SQLiteException
4-
import kotlinx.coroutines.CancellationException
53
import kotlinx.coroutines.flow.Flow
64
import kotlinx.coroutines.flow.distinctUntilChanged
75
import kotlinx.coroutines.flow.map
@@ -28,6 +26,7 @@ internal class SharedStorageImpl(val database: StorageDatabase) : SharedStorage
2826
.getValuesFlow(keys)
2927
.map { list -> list.map(StorageEntry::toEntry) }
3028
.distinctUntilChanged()
29+
.catchStorageException()
3130

3231
override suspend fun setValues(entries: List<Entry>): List<Entry> = catchStorageException {
3332
val values = entries.map(Entry::toStorageEntry)
@@ -40,22 +39,7 @@ internal class SharedStorageImpl(val database: StorageDatabase) : SharedStorage
4039

4140
override suspend fun getKeys(): List<String> = catchStorageException { storage.getKeys() }
4241

43-
override fun getKeysFlow(): Flow<List<String>> = storage.getKeysFlow()
42+
override fun getKeysFlow(): Flow<List<String>> = storage.getKeysFlow().catchStorageException()
4443

4544
override suspend fun clear() = catchStorageException { storage.clear() }
4645
}
47-
48-
private suspend fun <T> catchStorageException(block: suspend () -> T): T {
49-
try {
50-
return block()
51-
} catch (e: CancellationException) {
52-
throw e
53-
} catch (e: SQLiteException) {
54-
throw StorageException.SqliteException(e.message ?: "Unexcepted Sqlite exception", e.cause)
55-
} catch (e: Exception) {
56-
throw StorageException.OtherException(
57-
e.message ?: "Unknown exception: ${e::class.simpleName}",
58-
e.cause,
59-
)
60-
}
61-
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.asyncstorage.shared_storage
2+
3+
import androidx.sqlite.SQLiteException
4+
import kotlinx.coroutines.CancellationException
5+
import kotlinx.coroutines.flow.Flow
6+
import kotlinx.coroutines.flow.catch
7+
8+
sealed class StorageException(message: String, cause: Throwable?) : Exception(message, cause) {
9+
10+
/** Catch exception thrown by sqlite itself https://www.sqlite.org/rescode.html */
11+
class SqliteException(message: String, cause: Throwable?) : StorageException(message, cause)
12+
13+
class OtherException(message: String, cause: Throwable?) : StorageException(message, cause)
14+
}
15+
16+
internal suspend fun <T> catchStorageException(block: suspend () -> T): T {
17+
try {
18+
return block()
19+
} catch (e: CancellationException) {
20+
throw e
21+
} catch (e: Throwable) {
22+
throw e.asStorageException()
23+
}
24+
}
25+
26+
internal fun <T> Flow<T>.catchStorageException(): Flow<T> = catch { throw it }
27+
28+
private fun Throwable.asStorageException(): StorageException {
29+
if (this is SQLiteException) {
30+
return StorageException.SqliteException(
31+
message ?: "Unexcepted Sqlite exception: ${this::class.qualifiedName}",
32+
cause,
33+
)
34+
}
35+
36+
return StorageException.OtherException(
37+
message ?: "Unknown storage exception: ${this::class.qualifiedName}",
38+
cause,
39+
)
40+
}

shared-storage/src/commonMain/kotlin/org/asyncstorage/shared_storage/database/StorageDatabase.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package org.asyncstorage.shared_storage.database
33
import androidx.room.*
44
import kotlinx.coroutines.flow.Flow
55

6-
private const val DATABASE_VERSION = 3
6+
private const val DATABASE_VERSION = 1
77

88
@Entity(tableName = "storage")
99
internal data class StorageEntry(@PrimaryKey val key: String, val value: String?)
@@ -46,8 +46,6 @@ abstract class StorageDatabase : RoomDatabase() {
4646
companion object
4747
}
4848

49-
// The Room compiler generates the `actual` implementations.
50-
@Suppress("KotlinNoActualForExpect")
5149
expect object StorageDatabaseConstructor : RoomDatabaseConstructor<StorageDatabase> {
5250
override fun initialize(): StorageDatabase
5351
}

0 commit comments

Comments
 (0)