Skip to content

Commit 560d163

Browse files
Replace throwing IllegalStateException with Result.Error or handle it properly (#1576)
Co-authored-by: Aleksandar Apostolov <[email protected]>
1 parent cf87035 commit 560d163

File tree

3 files changed

+80
-60
lines changed

3 files changed

+80
-60
lines changed

stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/Call.kt

Lines changed: 74 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -631,29 +631,28 @@ public class Call(
631631
val sfuUrl = result.value.credentials.server.url
632632
val sfuWsUrl = result.value.credentials.server.wsEndpoint
633633
val iceServers = result.value.credentials.iceServers.map { it.toIceServer() }
634+
try {
635+
session = if (testInstanceProvider.rtcSessionCreator != null) {
636+
testInstanceProvider.rtcSessionCreator!!.invoke()
637+
} else {
638+
RtcSession(
639+
sessionId = this.sessionId,
640+
apiKey = clientImpl.apiKey,
641+
lifecycle = clientImpl.coordinatorConnectionModule.lifecycle,
642+
client = client,
643+
call = this,
644+
sfuUrl = sfuUrl,
645+
sfuWsUrl = sfuWsUrl,
646+
sfuToken = sfuToken,
647+
remoteIceServers = iceServers,
648+
powerManager = powerManager,
649+
)
650+
}
634651

635-
session = if (testInstanceProvider.rtcSessionCreator != null) {
636-
testInstanceProvider.rtcSessionCreator!!.invoke()
637-
} else {
638-
RtcSession(
639-
sessionId = this.sessionId,
640-
apiKey = clientImpl.apiKey,
641-
lifecycle = clientImpl.coordinatorConnectionModule.lifecycle,
642-
client = client,
643-
call = this,
644-
sfuUrl = sfuUrl,
645-
sfuWsUrl = sfuWsUrl,
646-
sfuToken = sfuToken,
647-
remoteIceServers = iceServers,
648-
powerManager = powerManager,
649-
)
650-
}
651-
652-
session?.let {
653-
state._connection.value = RealtimeConnection.Joined(it)
654-
}
652+
session?.let {
653+
state._connection.value = RealtimeConnection.Joined(it)
654+
}
655655

656-
try {
657656
session?.connect()
658657
} catch (e: Exception) {
659658
return Failure(Error.GenericError(e.message ?: "RtcSession error occurred."))
@@ -791,24 +790,31 @@ public class Call(
791790
)
792791
this.state.removeParticipant(prevSessionId)
793792
session.prepareRejoin()
794-
this.session = RtcSession(
795-
clientImpl,
796-
reconnectAttepmts,
797-
powerManager,
798-
this,
799-
sessionId,
800-
clientImpl.apiKey,
801-
clientImpl.coordinatorConnectionModule.lifecycle,
802-
cred.server.url,
803-
cred.server.wsEndpoint,
804-
cred.token,
805-
cred.iceServers.map { ice ->
806-
ice.toIceServer()
807-
},
808-
)
809-
this.session?.connect(reconnectDetails, currentOptions)
810-
session.cleanup()
811-
monitorSession(joinResponse.value)
793+
try {
794+
this.session = RtcSession(
795+
clientImpl,
796+
reconnectAttepmts,
797+
powerManager,
798+
this,
799+
sessionId,
800+
clientImpl.apiKey,
801+
clientImpl.coordinatorConnectionModule.lifecycle,
802+
cred.server.url,
803+
cred.server.wsEndpoint,
804+
cred.token,
805+
cred.iceServers.map { ice ->
806+
ice.toIceServer()
807+
},
808+
)
809+
this.session?.connect(reconnectDetails, currentOptions)
810+
session.cleanup()
811+
monitorSession(joinResponse.value)
812+
} catch (ex: Exception) {
813+
logger.e(ex) {
814+
"[rejoin] Failed to join response with ex: ${ex.message}"
815+
}
816+
state._connection.value = RealtimeConnection.Failed(ex)
817+
}
812818
} else {
813819
logger.e {
814820
"[rejoin] Failed to get a join response ${joinResponse.errorOrNull()}"
@@ -847,27 +853,35 @@ public class Call(
847853
reconnect_attempt = reconnectAttepmts,
848854
)
849855
session.prepareRejoin()
850-
val newSession = RtcSession(
851-
clientImpl,
852-
reconnectAttepmts,
853-
powerManager,
854-
this,
855-
sessionId,
856-
clientImpl.apiKey,
857-
clientImpl.coordinatorConnectionModule.lifecycle,
858-
cred.server.url,
859-
cred.server.wsEndpoint,
860-
cred.token,
861-
cred.iceServers.map { ice ->
862-
ice.toIceServer()
863-
},
864-
)
865-
val oldSession = this.session
866-
this.session = newSession
867-
this.session?.connect(reconnectDetails, currentOptions)
868-
monitorSession(joinResponse.value)
869-
oldSession?.leaveWithReason("migrating")
870-
oldSession?.cleanup()
856+
try {
857+
val newSession = RtcSession(
858+
clientImpl,
859+
reconnectAttepmts,
860+
powerManager,
861+
this,
862+
sessionId,
863+
clientImpl.apiKey,
864+
clientImpl.coordinatorConnectionModule.lifecycle,
865+
cred.server.url,
866+
cred.server.wsEndpoint,
867+
cred.token,
868+
cred.iceServers.map { ice ->
869+
ice.toIceServer()
870+
},
871+
)
872+
val oldSession = this.session
873+
this.session = newSession
874+
this.session?.connect(reconnectDetails, currentOptions)
875+
monitorSession(joinResponse.value)
876+
oldSession?.leaveWithReason("migrating")
877+
oldSession?.cleanup()
878+
} catch (ex: Exception) {
879+
logger.e(ex) {
880+
"[switchSfu] Failed to join during " +
881+
"migration - Error ${ex.message}"
882+
}
883+
state._connection.value = RealtimeConnection.Failed(ex)
884+
}
871885
} else {
872886
logger.e {
873887
"[switchSfu] Failed to get a join response during " +

stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/RtcSession.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ data class TrackDimensions(
197197
* * Or when the UI layout changes
198198
* * The SFU tells us what resolution to publish using the ChangePublishQualityEvent event
199199
*
200+
* For developers: RtcSession throws [IllegalStateException] because its [coroutineScope] & [rtcSessionScope] throws it
200201
*/
201202
public class RtcSession internal constructor(
202203
client: StreamVideo,

stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/scope/ScopeProviderImpl.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import kotlinx.coroutines.CoroutineScope
2323
import kotlinx.coroutines.asCoroutineDispatcher
2424
import java.util.concurrent.ExecutorService
2525
import java.util.concurrent.Executors
26+
import kotlin.jvm.Throws
2627

2728
/**
2829
* Implementation of [ScopeProvider] that manages coroutine scopes for RTC sessions.
@@ -41,7 +42,9 @@ internal class ScopeProviderImpl(
4142
private var executor: ExecutorService? = null
4243
private var isCleanedUp = false
4344

45+
@Throws(IllegalStateException::class)
4446
override fun getCoroutineScope(supervisorJob: CompletableJob): CoroutineScope {
47+
// Future: We should not throw exception. Instead return Result.Fail
4548
check(!isCleanedUp) { "ScopeProvider has been cleaned up" }
4649
logger.d { "Creating coroutine scope for RTC session main" }
4750
return CoroutineScope(
@@ -51,7 +54,9 @@ internal class ScopeProviderImpl(
5154
)
5255
}
5356

57+
@Throws(IllegalStateException::class)
5458
override fun getRtcSessionScope(supervisorJob: CompletableJob, callId: String): CoroutineScope {
59+
// Future: We should not throw exception. Instead return Result.Fail
5560
check(!isCleanedUp) { "ScopeProvider has been cleaned up" }
5661
logger.d { "Creating RTC session scope for callId: $callId" }
5762

0 commit comments

Comments
 (0)