Skip to content

Commit d8ec120

Browse files
committed
add cause to all firebase exceptions
remove common subpackage update to kotlin 1.3.61 / coroutines 1.3.3 add custom js encoder/decoders/serializers
1 parent 7dcf0f5 commit d8ec120

File tree

24 files changed

+267
-220
lines changed

24 files changed

+267
-220
lines changed

build.gradle.kts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import org.jetbrains.kotlin.gradle.dsl.KotlinCompile
22

33
plugins {
4-
kotlin("multiplatform") version "1.3.60" apply false
4+
kotlin("multiplatform") version "1.3.61" apply false
55
}
66

77
buildscript {
@@ -39,15 +39,15 @@ subprojects {
3939
afterEvaluate {
4040
dependencies {
4141
"commonMainImplementation"(kotlin("stdlib-common"))
42-
"commonMainImplementation"("org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.3.2")
42+
"commonMainImplementation"("org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.3.3")
4343
"jsMainImplementation"(kotlin("stdlib-js"))
44-
"jsMainImplementation"("org.jetbrains.kotlinx:kotlinx-coroutines-core-js:1.3.2")
45-
"androidMainImplementation"("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2")
46-
"androidMainImplementation"("org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.3.2")
44+
"jsMainImplementation"("org.jetbrains.kotlinx:kotlinx-coroutines-core-js:1.3.3")
45+
"androidMainImplementation"("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3")
46+
"androidMainImplementation"("org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.3.3")
4747
"jvmMainImplementation"(kotlin("stdlib-jdk8"))
48-
"jvmMainImplementation"("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2")
48+
"jvmMainImplementation"("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3")
4949
"jvmMainApi"("app.teamhub:firebase-java:0.3.0")
50-
"jvmMainApi"("org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.3.2")
50+
"jvmMainApi"("org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.3.3")
5151
}
5252
}
5353
}

firebase-app/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@teamhubapp/firebase-app",
3-
"version": "0.1.0-dev1",
3+
"version": "0.1.0-dev9",
44
"description": "Wrapper around firebase for usage in Kotlin Multiplatform projects",
55
"main": "firebase-app.js",
66
"scripts": {
@@ -23,10 +23,10 @@
2323
},
2424
"homepage": "https://github.com/TeamHubApp/firebase-kotlin-multiplatform-sdk",
2525
"dependencies": {
26-
"@teamhubapp/firebase-common": "0.1.0-beta1",
26+
"@teamhubapp/firebase-common": "0.1.0-dev9",
2727
"firebase": "6.2.3",
28-
"kotlin": "1.3.60",
29-
"kotlinx-coroutines-core": "1.3.2"
28+
"kotlin": "1.3.61",
29+
"kotlinx-coroutines-core": "1.3.3"
3030
}
3131
}
3232

firebase-app/src/jsMain/kotlin/dev/teamhub/firebase/firebase.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package dev.teamhub.firebase
22

3-
import dev.teamhub.firebase.common.firebase
43
import kotlin.js.json
54

65
actual val Firebase.app: FirebaseApp
@@ -38,7 +37,7 @@ private fun FirebaseOptions.toJson() = json(
3837
"gaTrackingId" to gaTrackingId
3938
)
4039

41-
actual open class FirebaseException(code: String?, message: String?) : Exception("$code: $message")
42-
actual open class FirebaseNetworkException(code: String?, message: String?) : FirebaseException(code, message)
43-
actual open class FirebaseTooManyRequestsException(code: String?, message: String?) : FirebaseException(code, message)
44-
actual open class FirebaseApiNotAvailableException(code: String?, message: String?) : FirebaseException(code, message)
40+
actual open class FirebaseException(code: String?, cause: Throwable) : Exception("$code: ${cause.message}", cause)
41+
actual open class FirebaseNetworkException(code: String?, cause: Throwable) : FirebaseException(code, cause)
42+
actual open class FirebaseTooManyRequestsException(code: String?, cause: Throwable) : FirebaseException(code, cause)
43+
actual open class FirebaseApiNotAvailableException(code: String?, cause: Throwable) : FirebaseException(code, cause)

firebase-auth/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@teamhubapp/firebase-auth",
3-
"version": "0.1.0-dev1",
3+
"version": "0.1.0-dev9",
44
"description": "Wrapper around firebase for usage in Kotlin Multiplatform projects",
55
"main": "firebase-auth.js",
66
"scripts": {
@@ -23,10 +23,10 @@
2323
},
2424
"homepage": "https://github.com/TeamHubApp/firebase-kotlin-multiplatform-sdk",
2525
"dependencies": {
26-
"@teamhubapp/firebase-app": "0.1.0-beta1",
26+
"@teamhubapp/firebase-app": "0.1.0-dev9",
2727
"firebase": "6.2.3",
28-
"kotlin": "1.3.60",
29-
"kotlinx-coroutines-core": "1.3.2"
28+
"kotlin": "1.3.61",
29+
"kotlinx-coroutines-core": "1.3.3"
3030
}
3131
}
3232

firebase-auth/src/jsMain/kotlin/dev/teamhub/firebase/auth/auth.kt

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
package dev.teamhub.firebase.auth
22

3-
import dev.teamhub.firebase.Firebase
4-
import dev.teamhub.firebase.FirebaseApp
5-
import dev.teamhub.firebase.FirebaseException
6-
import dev.teamhub.firebase.FirebaseNetworkException
7-
import dev.teamhub.firebase.common.firebase
3+
import dev.teamhub.firebase.*
84
import kotlinx.coroutines.await
95
import kotlinx.coroutines.channels.awaitClose
106
import kotlinx.coroutines.flow.callbackFlow
117

128
actual val Firebase.auth
13-
get() = rethrow { dev.teamhub.firebase.common.auth; FirebaseAuth(firebase.auth()) }
9+
get() = rethrow { dev.teamhub.firebase.auth; FirebaseAuth(firebase.auth()) }
1410

1511
actual fun Firebase.auth(app: FirebaseApp) =
16-
rethrow { dev.teamhub.firebase.common.auth; FirebaseAuth(firebase.auth(app.js)) }
12+
rethrow { dev.teamhub.firebase.auth; FirebaseAuth(firebase.auth(app.js)) }
1713

1814
actual class FirebaseAuth internal constructor(val js: firebase.auth.Auth) {
1915

@@ -50,14 +46,14 @@ actual class FirebaseUser internal constructor(val js: firebase.user.User) {
5046
actual suspend fun reload() = rethrow { js.reload().await() }
5147
}
5248

53-
actual open class FirebaseAuthException(code: String?, message: String?): FirebaseException(code, message)
54-
actual open class FirebaseAuthActionCodeException(code: String?, message: String?): FirebaseAuthException(code, message)
55-
actual open class FirebaseAuthEmailException(code: String?, message: String?): FirebaseAuthException(code, message)
56-
actual open class FirebaseAuthInvalidCredentialsException(code: String?, message: String?): FirebaseAuthException(code, message)
57-
actual open class FirebaseAuthInvalidUserException(code: String?, message: String?): FirebaseAuthException(code, message)
58-
actual open class FirebaseAuthRecentLoginRequiredException(code: String?, message: String?): FirebaseAuthException(code, message)
59-
actual open class FirebaseAuthUserCollisionException(code: String?, message: String?): FirebaseAuthException(code, message)
60-
actual open class FirebaseAuthWebException(code: String?, message: String?): FirebaseAuthException(code, message)
49+
actual open class FirebaseAuthException(code: String?, cause: Throwable): FirebaseException(code, cause)
50+
actual open class FirebaseAuthActionCodeException(code: String?, cause: Throwable): FirebaseAuthException(code, cause)
51+
actual open class FirebaseAuthEmailException(code: String?, cause: Throwable): FirebaseAuthException(code, cause)
52+
actual open class FirebaseAuthInvalidCredentialsException(code: String?, cause: Throwable): FirebaseAuthException(code, cause)
53+
actual open class FirebaseAuthInvalidUserException(code: String?, cause: Throwable): FirebaseAuthException(code, cause)
54+
actual open class FirebaseAuthRecentLoginRequiredException(code: String?, cause: Throwable): FirebaseAuthException(code, cause)
55+
actual open class FirebaseAuthUserCollisionException(code: String?, cause: Throwable): FirebaseAuthException(code, cause)
56+
actual open class FirebaseAuthWebException(code: String?, cause: Throwable): FirebaseAuthException(code, cause)
6157

6258
private inline fun <T, R> T.rethrow(function: T.() -> R): R = dev.teamhub.firebase.auth.rethrow { function() }
6359

@@ -71,19 +67,19 @@ private inline fun <R> rethrow(function: () -> R): R {
7167
}
7268
}
7369

74-
private fun errorToException(e: Throwable) = when(val code = e.asDynamic().code as String?) {
75-
"auth/invalid-user-token" -> FirebaseAuthInvalidUserException(code, e.message)
76-
"auth/requires-recent-login" -> FirebaseAuthRecentLoginRequiredException(code, e.message)
77-
"auth/user-disabled" -> FirebaseAuthInvalidUserException(code, e.message)
78-
"auth/user-token-expired" -> FirebaseAuthInvalidUserException(code, e.message)
79-
"auth/web-storage-unsupported" -> FirebaseAuthWebException(code, e.message)
80-
"auth/network-request-failed" -> FirebaseNetworkException(code, e.message)
70+
private fun errorToException(cause: Throwable) = when(val code = cause.asDynamic().code as String?) {
71+
"auth/invalid-user-token" -> FirebaseAuthInvalidUserException(code, cause)
72+
"auth/requires-recent-login" -> FirebaseAuthRecentLoginRequiredException(code, cause)
73+
"auth/user-disabled" -> FirebaseAuthInvalidUserException(code, cause)
74+
"auth/user-token-expired" -> FirebaseAuthInvalidUserException(code, cause)
75+
"auth/web-storage-unsupported" -> FirebaseAuthWebException(code, cause)
76+
"auth/network-request-failed" -> FirebaseNetworkException(code, cause)
8177
// "auth/app-deleted" ->
8278
// "auth/app-not-authorized" ->
8379
// "auth/argument-error" ->
8480
// "auth/invalid-api-key" ->
8581
// "auth/operation-not-allowed" ->
8682
// "auth/too-many-arguments" ->
8783
// "auth/unauthorized-domain" ->
88-
else -> FirebaseAuthException(code, e.message)
84+
else -> FirebaseAuthException(code, cause)
8985
}

firebase-common/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ kotlin {
4444
}
4545

4646
sourceSets {
47+
val commonMain by getting {
48+
dependencies {
49+
implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:0.14.0")
50+
}
51+
}
4752
val androidMain by getting {
4853
dependencies {
4954
api("com.google.firebase:firebase-common:19.2.0")

firebase-common/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@teamhubapp/firebase-common",
3-
"version": "0.1.0-dev1",
3+
"version": "0.1.0-dev9",
44
"description": "Wrapper around firebase for usage in Kotlin Multiplatform projects",
55
"main": "firebase-common.js",
66
"scripts": {
@@ -24,9 +24,9 @@
2424
"homepage": "https://github.com/TeamHubApp/firebase-kotlin-multiplatform-sdk",
2525
"dependencies": {
2626
"firebase": "6.2.3",
27-
"kotlin": "1.3.60",
28-
"kotlinx-coroutines-core": "1.3.2",
29-
"@cachet/kotlinx-serialization-runtime": "0.14.0-rc.1"
27+
"kotlin": "1.3.61",
28+
"kotlinx-coroutines-core": "1.3.3",
29+
"@cachet/kotlinx-serialization-runtime": "0.14.0-rc.2"
3030
}
3131
}
3232

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package dev.teamhub.firebase
2+
3+
import kotlinx.serialization.CompositeDecoder
4+
import kotlinx.serialization.KSerializer
5+
import kotlinx.serialization.SerialDescriptor
6+
import kotlinx.serialization.StructureKind
7+
8+
actual fun FirebaseDecoder.structureDecoder(desc: SerialDescriptor, vararg typeParams: KSerializer<*>): CompositeDecoder = when(desc.kind as StructureKind) {
9+
StructureKind.CLASS -> (value as Map<*, *>).let { map ->
10+
FirebaseClassDecoder(map.size, { map.containsKey(it) }) { desc, index -> map[desc.getElementName(index)] }
11+
}
12+
StructureKind.LIST -> (value as List<*>).let {
13+
FirebaseCompositeDecoder(it.size) { _, index -> it[index] }
14+
}
15+
StructureKind.MAP -> (value as Map<*, *>).entries.toList().let {
16+
FirebaseCompositeDecoder(it.size) { _, index -> it[index/2].run { if(index % 2 == 0) key else value } }
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package dev.teamhub.firebase
2+
3+
import kotlinx.serialization.CompositeEncoder
4+
import kotlinx.serialization.KSerializer
5+
import kotlinx.serialization.SerialDescriptor
6+
import kotlinx.serialization.StructureKind
7+
import kotlin.collections.mutableListOf
8+
import kotlin.collections.mutableMapOf
9+
import kotlin.collections.set
10+
11+
actual fun FirebaseEncoder.structureEncoder(desc: SerialDescriptor, vararg typeParams: KSerializer<*>): CompositeEncoder = when(desc.kind as StructureKind) {
12+
StructureKind.LIST -> mutableListOf<Any?>()
13+
.also { value = it }
14+
.let { FirebaseCompositeEncoder { _, index, value -> it.add(index, value) } }
15+
StructureKind.MAP, StructureKind.CLASS -> mutableMapOf<Any?, Any?>()
16+
.also { value = it }
17+
.let { FirebaseCompositeEncoder { _, index, value -> it[desc.getElementName(index)] = value } }
18+
}

firebase-common/src/androidMain/kotlin/dev/teamhub/firebase/decoders.kt renamed to firebase-common/src/commonMain/kotlin/dev/teamhub/firebase/decoders.kt

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,17 @@ inline fun <reified T> decode(strategy: DeserializationStrategy<T> = EmptyModule
1616
return FirebaseDecoder(value).decode(strategy)
1717
}
1818

19-
class FirebaseDecoder(private val value: Any?) : Decoder {
19+
expect fun FirebaseDecoder.structureDecoder(desc: SerialDescriptor, vararg typeParams: KSerializer<*>): CompositeDecoder
20+
21+
class FirebaseDecoder(internal val value: Any?) : Decoder {
2022

2123
override val context: SerialModule
2224
get() = EmptyModule
2325

2426
override val updateMode: UpdateMode = UpdateMode.BANNED
2527

2628
@Suppress("UNCHECKED_CAST")
27-
override fun beginStructure(desc: SerialDescriptor, vararg typeParams: KSerializer<*>) = when(desc.kind as StructureKind) {
28-
StructureKind.CLASS -> FirebaseClassDecoder(value as Map<String, Any?>)
29-
StructureKind.LIST -> FirebaseCompositeDecoder(value as List<*>)
30-
StructureKind.MAP -> (value as Map<*, *>).let { map ->
31-
FirebaseCompositeDecoder(map.flatMap { listOf(it.key, it.value) }, map.size)
32-
}
33-
}
29+
override fun beginStructure(desc: SerialDescriptor, vararg typeParams: KSerializer<*>) = structureDecoder(desc, *typeParams)
3430

3531
override fun decodeString() = decodeString(value)
3632

@@ -60,27 +56,27 @@ class FirebaseDecoder(private val value: Any?) : Decoder {
6056

6157
}
6258

63-
class FirebaseClassDecoder(private val map: Map<String, Any?>) : FirebaseCompositeDecoder(
64-
map.size, { desc, index -> map[desc.getElementName(index)] }
65-
) {
59+
class FirebaseClassDecoder(
60+
size: Int,
61+
private val containsKey: (name: String) -> Boolean,
62+
get: (desc: SerialDescriptor, index: Int) -> Any?
63+
) : FirebaseCompositeDecoder(size, get) {
6664
private var index: Int = 0
6765

6866
override fun decodeElementIndex(desc: SerialDescriptor): Int =
6967
(index until desc.elementsCount)
70-
.firstOrNull { !desc.isElementOptional(it) || map.containsKey(desc.getElementName(it)) }
68+
.firstOrNull { !desc.isElementOptional(it) || containsKey(desc.getElementName(it)) }
7169
?.also { index = it + 1 }
7270
?: READ_DONE
7371
}
7472

75-
open class FirebaseCompositeDecoder protected constructor(
73+
open class FirebaseCompositeDecoder constructor(
7674
private val size: Int,
7775
private val get: (desc: SerialDescriptor, index: Int) -> Any?
7876
): CompositeDecoder {
7977

80-
constructor(list: List<Any?>, size: Int = list.size): this(size, { _, index -> list[index] })
81-
8278
override val context = EmptyModule
83-
override val updateMode = UpdateMode.BANNED
79+
override val updateMode = UpdateMode.OVERWRITE
8480

8581
override fun decodeElementIndex(desc: SerialDescriptor) = READ_ALL
8682

@@ -97,10 +93,10 @@ open class FirebaseCompositeDecoder protected constructor(
9793
}
9894

9995
override fun <T> updateSerializableElement(desc: SerialDescriptor, index: Int, deserializer: DeserializationStrategy<T>, old: T): T =
100-
throw UpdateNotSupportedException(deserializer.descriptor.name)
96+
deserializer.deserialize(FirebaseDecoder(get(desc, index)))
10197

10298
override fun <T : Any> updateNullableSerializableElement(desc: SerialDescriptor, index: Int, deserializer: DeserializationStrategy<T?>, old: T?): T? =
103-
throw UpdateNotSupportedException(deserializer.descriptor.name)
99+
if(decodeNotNullMark(get(desc, index))) decodeSerializableElement(desc, index, deserializer) else decodeNull(get(desc, index))
104100

105101
override fun decodeBooleanElement(desc: SerialDescriptor, index: Int) = decodeBoolean(get(desc, index))
106102

0 commit comments

Comments
 (0)