diff --git a/common/src/main/kotlin/de/sipgate/federmappe/common/ErrorHandler.kt b/common/src/main/kotlin/de/sipgate/federmappe/common/ErrorHandler.kt new file mode 100644 index 0000000..0048a79 --- /dev/null +++ b/common/src/main/kotlin/de/sipgate/federmappe/common/ErrorHandler.kt @@ -0,0 +1,3 @@ +package de.sipgate.federmappe.common + +typealias ErrorHandler = (Throwable) -> T? diff --git a/firestore/src/main/java/de/sipgate/federmappe/firestore/DocumentSnapshotExt.kt b/firestore/src/main/java/de/sipgate/federmappe/firestore/DocumentSnapshotExt.kt index 97a851d..8ebc286 100644 --- a/firestore/src/main/java/de/sipgate/federmappe/firestore/DocumentSnapshotExt.kt +++ b/firestore/src/main/java/de/sipgate/federmappe/firestore/DocumentSnapshotExt.kt @@ -3,6 +3,7 @@ package de.sipgate.federmappe.firestore import com.google.firebase.Timestamp import com.google.firebase.firestore.DocumentSnapshot import de.sipgate.federmappe.common.DefaultSerializersModule +import de.sipgate.federmappe.common.ErrorHandler import de.sipgate.federmappe.common.toObjectWithSerializer import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.modules.SerializersModule @@ -10,12 +11,12 @@ import kotlinx.serialization.modules.SerializersModule @ExperimentalSerializationApi inline fun DocumentSnapshot.toObject( customSerializers: SerializersModule = DefaultSerializersModule, - errorHandler: (Throwable) -> T? = { throw it }): T? = - try { - data?.toObjectWithSerializer( - customSerializers = customSerializers, - subtypeDecoder = { (it as? Timestamp)?.let(::FirebaseTimestampDecoder) } - ) - } catch (ex: Throwable) { - errorHandler(ex) - } + errorHandler: ErrorHandler = { throw it } +): T? = try { + data?.toObjectWithSerializer( + customSerializers = customSerializers, + subtypeDecoder = { (it as? Timestamp)?.let(::FirebaseTimestampDecoder) } + ) +} catch (ex: Throwable) { + errorHandler(ex) +} diff --git a/firestore/src/main/java/de/sipgate/federmappe/firestore/QuerySnapshotExt.kt b/firestore/src/main/java/de/sipgate/federmappe/firestore/QuerySnapshotExt.kt index 8192645..f13f4a5 100644 --- a/firestore/src/main/java/de/sipgate/federmappe/firestore/QuerySnapshotExt.kt +++ b/firestore/src/main/java/de/sipgate/federmappe/firestore/QuerySnapshotExt.kt @@ -3,6 +3,7 @@ package de.sipgate.federmappe.firestore import com.google.firebase.Timestamp import com.google.firebase.firestore.QuerySnapshot import de.sipgate.federmappe.common.DefaultSerializersModule +import de.sipgate.federmappe.common.ErrorHandler import de.sipgate.federmappe.common.toObjectWithSerializer import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.modules.SerializersModule @@ -10,14 +11,13 @@ import kotlinx.serialization.modules.SerializersModule @ExperimentalSerializationApi inline fun QuerySnapshot.toObject( customSerializers: SerializersModule = DefaultSerializersModule, - errorHandler: (Throwable) -> T? = { throw it } -): List = - map { documentSnapshot -> - try { - documentSnapshot.data.toObjectWithSerializer( - customSerializers = customSerializers, - subtypeDecoder = { (it as? Timestamp)?.let(::FirebaseTimestampDecoder) }) - } catch (ex: Throwable) { - errorHandler(ex) - } + errorHandler: ErrorHandler = { throw it } +): List = map { documentSnapshot -> + try { + documentSnapshot.data.toObjectWithSerializer( + customSerializers = customSerializers, + subtypeDecoder = { (it as? Timestamp)?.let(::FirebaseTimestampDecoder) }) + } catch (ex: Throwable) { + errorHandler(ex) } +} diff --git a/realtimedb/src/main/kotlin/de/sipgate/federmappe/realtimedb/DataSnapshotExt.kt b/realtimedb/src/main/kotlin/de/sipgate/federmappe/realtimedb/DataSnapshotExt.kt index 5306ecd..c067cf8 100644 --- a/realtimedb/src/main/kotlin/de/sipgate/federmappe/realtimedb/DataSnapshotExt.kt +++ b/realtimedb/src/main/kotlin/de/sipgate/federmappe/realtimedb/DataSnapshotExt.kt @@ -1,6 +1,7 @@ package de.sipgate.federmappe.realtimedb import com.google.firebase.database.DataSnapshot +import de.sipgate.federmappe.common.ErrorHandler import de.sipgate.federmappe.common.StringMapToObjectDecoder import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.KSerializer @@ -11,7 +12,7 @@ import kotlinx.serialization.serializer @ExperimentalSerializationApi inline fun DataSnapshot.toObject( customSerializers: SerializersModule = EmptySerializersModule(), - crossinline errorHandler: (Throwable) -> T? = { throw it } + crossinline errorHandler: ErrorHandler = { throw it } ): T? = try { toObjectWithSerializer(customSerializers = customSerializers) } catch (ex: Throwable) { diff --git a/realtimedb/src/main/kotlin/de/sipgate/federmappe/realtimedb/DatabaseReferenceExt.kt b/realtimedb/src/main/kotlin/de/sipgate/federmappe/realtimedb/DatabaseReferenceExt.kt index ae003a6..e582158 100644 --- a/realtimedb/src/main/kotlin/de/sipgate/federmappe/realtimedb/DatabaseReferenceExt.kt +++ b/realtimedb/src/main/kotlin/de/sipgate/federmappe/realtimedb/DatabaseReferenceExt.kt @@ -1,17 +1,18 @@ package de.sipgate.federmappe.realtimedb import com.google.firebase.database.DatabaseReference +import de.sipgate.federmappe.common.ErrorHandler import kotlinx.coroutines.tasks.await import kotlinx.serialization.ExperimentalSerializationApi @ExperimentalSerializationApi suspend inline fun DatabaseReference.toObject( - crossinline errorHandler: (Throwable) -> T? = { throw it } + crossinline errorHandler: ErrorHandler = { throw it } ): T? = get().await().toObject(errorHandler = errorHandler) @ExperimentalSerializationApi suspend inline fun DatabaseReference.toObjects( - crossinline errorHandler: (Throwable) -> T? = { throw it } + crossinline errorHandler: ErrorHandler = { throw it } ): List = get().await().children.mapNotNull { childSnapshot -> childSnapshot.toObject(errorHandler = errorHandler) }