Skip to content

Commit 300c6d9

Browse files
authored
fix(api): Fix the handling of exceptions when subscribing with Kotlin Facade (#2821)
1 parent 9f900a0 commit 300c6d9

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

core-kotlin/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ dependencies {
3333
testImplementation(libs.test.mockk)
3434
testImplementation(libs.test.kotlin.coroutines)
3535
testImplementation(project(":testmodels"))
36+
testImplementation(libs.test.kotest.assertions)
3637
}
3738

3839
android.kotlinOptions {

core-kotlin/src/main/java/com/amplifyframework/kotlin/api/KotlinApiFacade.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ class KotlinApiFacade(private val delegate: Delegate = Amplify.API) : Api {
108108
{ subscription.completions.tryEmit(Unit) }
109109
)
110110
}
111-
subscription.cancelable = operation as Cancelable
111+
// If subscribe fails it does not return an operation, and instead invokes the onSubscriptionFailure callback
112+
operation?.let { subscription.cancelable = operation }
112113
return subscription.awaitStart()
113114
}
114115

core-kotlin/src/test/java/com/amplifyframework/kotlin/api/KotlinApiFacadeTest.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.amplifyframework.api.graphql.GraphQLResponse
2323
import com.amplifyframework.api.rest.RestOptions
2424
import com.amplifyframework.api.rest.RestResponse
2525
import com.amplifyframework.core.Consumer
26+
import io.kotest.assertions.throwables.shouldThrow
2627
import io.mockk.every
2728
import io.mockk.mockk
2829
import kotlinx.coroutines.Dispatchers
@@ -32,6 +33,7 @@ import kotlinx.coroutines.GlobalScope
3233
import kotlinx.coroutines.flow.first
3334
import kotlinx.coroutines.launch
3435
import kotlinx.coroutines.runBlocking
36+
import kotlinx.coroutines.test.runTest
3537
import org.junit.Assert.assertEquals
3638
import org.junit.Test
3739

@@ -174,6 +176,25 @@ class KotlinApiFacadeTest {
174176
api.subscribe(request).first()
175177
}
176178

179+
@Test
180+
fun `subscribe throws when exception occurs during subscription establishment`() = runTest {
181+
val request = mockk<GraphQLRequest<String>>()
182+
val expectedFailure = ApiException("uh", "oh")
183+
184+
every {
185+
delegate.subscribe(eq(request), any(), any(), any(), any())
186+
} answers {
187+
val indexOfErrorConsumer = 3
188+
val onError = it.invocation.args[indexOfErrorConsumer] as Consumer<ApiException>
189+
GlobalScope.launch(Dispatchers.IO) { onError.accept(expectedFailure) }
190+
null
191+
}
192+
193+
shouldThrow<ApiException> {
194+
api.subscribe(request).first()
195+
}
196+
}
197+
177198
/**
178199
* When the underlying get() emits a response,
179200
* it should be returned from the coroutine API.

0 commit comments

Comments
 (0)