Skip to content

Commit ca41f47

Browse files
committed
update exceptions
1 parent 9cdba61 commit ca41f47

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

firebase-vertexai/api.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,6 @@ package com.google.firebase.vertexai.type {
325325
property public final kotlinx.serialization.json.JsonObject response;
326326
}
327327

328-
public final class GeminiConnectionHandshakeFailed extends com.google.firebase.vertexai.type.FirebaseVertexAIException {
329-
ctor public GeminiConnectionHandshakeFailed();
330-
}
331-
332328
public final class GenerateContentResponse {
333329
ctor public GenerateContentResponse(java.util.List<com.google.firebase.vertexai.type.Candidate> candidates, com.google.firebase.vertexai.type.PromptFeedback? promptFeedback, com.google.firebase.vertexai.type.UsageMetadata? usageMetadata);
334330
method public java.util.List<com.google.firebase.vertexai.type.Candidate> getCandidates();
@@ -816,6 +812,10 @@ package com.google.firebase.vertexai.type {
816812
public final class ServerException extends com.google.firebase.vertexai.type.FirebaseVertexAIException {
817813
}
818814

815+
public final class ServiceConnectionHandshakeFailedException extends com.google.firebase.vertexai.type.FirebaseVertexAIException {
816+
ctor public ServiceConnectionHandshakeFailedException(String message, Throwable? cause = null);
817+
}
818+
819819
public final class ServiceDisabledException extends com.google.firebase.vertexai.type.FirebaseVertexAIException {
820820
}
821821

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/LiveGenerativeModel.kt

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import io.ktor.websocket.Frame
3333
import io.ktor.websocket.close
3434
import io.ktor.websocket.readBytes
3535
import kotlin.coroutines.CoroutineContext
36+
import kotlinx.coroutines.channels.ClosedReceiveChannelException
3637
import kotlinx.serialization.encodeToString
3738
import kotlinx.serialization.json.Json
3839

@@ -82,8 +83,8 @@ internal constructor(
8283
/**
8384
* Returns a LiveSession object using which you could send/receive messages from the server
8485
* @return LiveSession object created
85-
* @throws [BidiServerHandshakeFailed] if the handshake with the server failed.
86-
* @throws [ClosedReceiveChannelException] if the channel was closed by the server.
86+
* @throws [ServiceConnectionHandshakeFailedException] if the client was not able to establish a
87+
* connection with the server.
8788
*/
8889
public suspend fun connect(): LiveSession {
8990
val clientMessage =
@@ -95,15 +96,19 @@ internal constructor(
9596
)
9697
.toInternal()
9798
val data: String = Json.encodeToString(clientMessage)
98-
val webSession = controller.getWebSocketSession(location)
99-
webSession.send(Frame.Text(data))
100-
val receivedJson = webSession.incoming.receive().readBytes().toString(Charsets.UTF_8)
101-
// TODO: Try to decode the json instead of string matching.
102-
return if (receivedJson.contains("setupComplete")) {
103-
LiveSession(session = webSession, backgroundDispatcher = backgroundDispatcher)
104-
} else {
105-
webSession.close()
106-
throw ServiceConnectionHandshakeFailedException()
99+
try {
100+
val webSession = controller.getWebSocketSession(location)
101+
webSession.send(Frame.Text(data))
102+
val receivedJson = webSession.incoming.receive().readBytes().toString(Charsets.UTF_8)
103+
// TODO: Try to decode the json instead of string matching.
104+
return if (receivedJson.contains("setupComplete")) {
105+
LiveSession(session = webSession, backgroundDispatcher = backgroundDispatcher)
106+
} else {
107+
webSession.close()
108+
throw ServiceConnectionHandshakeFailedException("Unable to connect to the server")
109+
}
110+
} catch (e: ClosedReceiveChannelException) {
111+
throw ServiceConnectionHandshakeFailedException("Channel was closed by the server", e)
107112
}
108113
}
109114

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/java/LiveModelFutures.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import com.google.common.util.concurrent.ListenableFuture
2121
import com.google.firebase.vertexai.GenerativeModel
2222
import com.google.firebase.vertexai.LiveGenerativeModel
2323
import com.google.firebase.vertexai.type.LiveSession
24-
import java.nio.channels.ClosedChannelException
24+
import com.google.firebase.vertexai.type.ServiceConnectionHandshakeFailedException
2525

2626
/**
2727
* Wrapper class providing Java compatible methods for [GenerativeModel].
@@ -33,7 +33,8 @@ public abstract class LiveModelFutures internal constructor() {
3333
/**
3434
* Returns a LiveSession object using which you could send/receive messages from the server
3535
* @return LiveSession object created. Returns null if the object cannot be created.
36-
* @throws [ClosedChannelException] if channel was closed before creating a websocket connection.
36+
* @throws [ServiceConnectionHandshakeFailedException] if the client was not able to establish a
37+
* connection with the server.
3738
*/
3839
public abstract fun connect(): ListenableFuture<LiveSession>
3940

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Exceptions.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ public class AudioRecordInitializationFailedException(message: String) :
186186
FirebaseVertexAIException(message)
187187

188188
/** Handshake failed with the server */
189-
public class ServiceConnectionHandshakeFailedException :
190-
FirebaseVertexAIException("Handshake failed with the server.")
189+
public class ServiceConnectionHandshakeFailedException(message: String, cause: Throwable? = null) :
190+
FirebaseVertexAIException(message, cause)
191191

192192
/** Catch all case for exceptions not explicitly expected. */
193193
public class UnknownException internal constructor(message: String, cause: Throwable? = null) :

0 commit comments

Comments
 (0)