Skip to content

Commit

Permalink
Use ConnectionErrorEvent model instead of APIError
Browse files Browse the repository at this point in the history
  • Loading branch information
liviu-timar committed Jun 6, 2024
1 parent d4de2b5 commit 6238de2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 47 deletions.
17 changes: 5 additions & 12 deletions stream-video-android-core/api/stream-video-android-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -8831,18 +8831,6 @@ public final class org/openapitools/client/models/UnblockedUserEvent : org/opena
public fun toString ()Ljava/lang/String;
}

public final class org/openapitools/client/models/UnknownVideoEvent : org/openapitools/client/models/VideoEvent {
public fun <init> (Ljava/lang/String;)V
public final fun component1 ()Ljava/lang/String;
public final fun copy (Ljava/lang/String;)Lorg/openapitools/client/models/UnknownVideoEvent;
public static synthetic fun copy$default (Lorg/openapitools/client/models/UnknownVideoEvent;Ljava/lang/String;ILjava/lang/Object;)Lorg/openapitools/client/models/UnknownVideoEvent;
public fun equals (Ljava/lang/Object;)Z
public fun getEventType ()Ljava/lang/String;
public final fun getExpectedType ()Ljava/lang/String;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class org/openapitools/client/models/UnpinRequest {
public fun <init> (Ljava/lang/String;Ljava/lang/String;)V
public final fun component1 ()Ljava/lang/String;
Expand All @@ -8867,6 +8855,11 @@ public final class org/openapitools/client/models/UnpinResponse {
public fun toString ()Ljava/lang/String;
}

public final class org/openapitools/client/models/UnsupportedVideoEventException : java/lang/Exception {
public fun <init> (Ljava/lang/String;)V
public final fun getType ()Ljava/lang/String;
}

public final class org/openapitools/client/models/UpdateCallMembersRequest {
public fun <init> ()V
public fun <init> (Ljava/util/List;Ljava/util/List;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
import okhttp3.WebSocket
import org.openapitools.client.infrastructure.Serializer
import org.openapitools.client.models.APIError
import org.openapitools.client.models.ConnectUserDetailsRequest
import org.openapitools.client.models.ConnectedEvent
import org.openapitools.client.models.UnknownVideoEvent
import org.openapitools.client.models.ConnectionErrorEvent
import org.openapitools.client.models.UnsupportedVideoEventException
import org.openapitools.client.models.VideoEvent
import org.openapitools.client.models.WSAuthMessageRequest

Expand Down Expand Up @@ -89,41 +89,35 @@ public class CoordinatorSocket(

scope.launch(singleThreadDispatcher) {
try {
// Try to parse an APIError first
Serializer.moshi.adapter(APIError::class.java).let { errorAdapter ->
errorAdapter.fromJson(text)?.let { parsedError ->
handleError(
ErrorResponse(
code = parsedError.code,
message = parsedError.message,
statusCode = parsedError.statusCode,
exceptionFields = parsedError.exceptionFields ?: emptyMap(),
moreInfo = parsedError.moreInfo,
),
)
}
Serializer.moshi.adapter(VideoEvent::class.java).let { eventAdapter ->
eventAdapter.fromJson(text)?.let { parsedEvent -> processEvent(parsedEvent) }
}
} catch (e: Throwable) {
// If parsing an APIError fails, try to parse a VideoEvent
// This will also catch unmapped events, that's why we parse APIError first
try {
Serializer.moshi.adapter(VideoEvent::class.java).let { eventAdapter ->
eventAdapter.fromJson(text)?.let { parsedEvent ->
if (parsedEvent is UnknownVideoEvent) {
logger.w { "[onMessage] Received unknown VideoEvent type: ${parsedEvent.expectedType}" }
} else {
processEvent(parsedEvent)
}
}
}
} catch (e: Throwable) {
logger.w { "[onMessage] VideoEvent parsing error ${e.message}" }
if (e.cause is UnsupportedVideoEventException) {
val ex = e.cause as UnsupportedVideoEventException
logger.w { "[onMessage] Received unsupported VideoEvent type: ${ex.type}. Ignoring." }
} else {
logger.w { "[onMessage] VideoEvent parsing error ${e.message}." }
handleError(e)
}
}
}
}

private suspend fun processEvent(parsedEvent: VideoEvent) {
if (parsedEvent is ConnectionErrorEvent) {
handleError(
ErrorResponse(
code = parsedEvent.error?.code ?: -1,
message = parsedEvent.error?.message ?: "",
statusCode = parsedEvent.error?.statusCode ?: -1,
exceptionFields = parsedEvent.error?.exceptionFields ?: emptyMap(),
moreInfo = parsedEvent.error?.moreInfo ?: "",
),
)
return
}

if (parsedEvent is ConnectedEvent) {
_connectionId.value = parsedEvent.connectionId
setConnectedStateAndContinue(parsedEvent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,9 @@ class VideoEventAdapter : JsonAdapter<VideoEvent>() {
"connection.ok" -> ConnectedEvent::class.java
"custom" -> CustomVideoEvent::class.java
"health.check" -> HealthCheckEvent::class.java
else -> UnknownVideoEvent(type)::class.java
else -> throw UnsupportedVideoEventException(type)
}
}
}

data class UnknownVideoEvent(
@Json(name = "type") val expectedType: String
) : VideoEvent() {
override fun getEventType() = "unknown"
}
class UnsupportedVideoEventException(val type: String) : Exception()

0 comments on commit 6238de2

Please sign in to comment.