Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions firebase-ai/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

- [changed] Removed redundant internal exception types. (#7475)

# 17.4.0

- [changed] **Breaking Change**: Removed the `candidateCount` option from `LiveGenerationConfig`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package com.google.firebase.ai
import com.google.firebase.FirebaseApp
import com.google.firebase.ai.common.APIController
import com.google.firebase.ai.common.AppCheckHeaderProvider
import com.google.firebase.ai.common.ContentBlockedException
import com.google.firebase.ai.common.GenerateImageRequest
import com.google.firebase.ai.type.ContentBlockedException
import com.google.firebase.ai.type.Dimensions
import com.google.firebase.ai.type.FirebaseAIException
import com.google.firebase.ai.type.ImagenEditMode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,26 @@ import com.google.firebase.Firebase
import com.google.firebase.FirebaseApp
import com.google.firebase.ai.common.util.decodeToFlow
import com.google.firebase.ai.common.util.fullModelName
import com.google.firebase.ai.type.APINotConfiguredException
import com.google.firebase.ai.type.CountTokensResponse
import com.google.firebase.ai.type.FinishReason
import com.google.firebase.ai.type.FirebaseAIException
import com.google.firebase.ai.type.GRpcErrorResponse
import com.google.firebase.ai.type.GenerateContentResponse
import com.google.firebase.ai.type.GenerativeBackend
import com.google.firebase.ai.type.GenerativeBackendEnum
import com.google.firebase.ai.type.ImagenGenerationResponse
import com.google.firebase.ai.type.InvalidAPIKeyException
import com.google.firebase.ai.type.PromptBlockedException
import com.google.firebase.ai.type.PublicPreviewAPI
import com.google.firebase.ai.type.QuotaExceededException
import com.google.firebase.ai.type.RequestOptions
import com.google.firebase.ai.type.Response
import com.google.firebase.ai.type.ResponseStoppedException
import com.google.firebase.ai.type.SerializationException
import com.google.firebase.ai.type.ServerException
import com.google.firebase.ai.type.ServiceDisabledException
import com.google.firebase.ai.type.UnsupportedUserLocationException
import com.google.firebase.options
import io.ktor.client.HttpClient
import io.ktor.client.call.body
Expand Down Expand Up @@ -149,7 +159,7 @@ internal constructor(
.body<GenerateContentResponse.Internal>()
.validate()
} catch (e: Throwable) {
throw FirebaseCommonAIException.from(e)
throw FirebaseAIException.from(e)
}

suspend fun generateImage(request: GenerateImageRequest): ImagenGenerationResponse.Internal =
Expand All @@ -162,7 +172,7 @@ internal constructor(
.also { validateResponse(it) }
.body<ImagenGenerationResponse.Internal>()
} catch (e: Throwable) {
throw FirebaseCommonAIException.from(e)
throw FirebaseAIException.from(e)
}

private fun getBidiEndpoint(location: String): String =
Expand All @@ -187,7 +197,7 @@ internal constructor(
applyCommonConfiguration(request)
}
.map { it.validate() }
.catch { throw FirebaseCommonAIException.from(it) }
.catch { throw FirebaseAIException.from(it) }

suspend fun countTokens(request: CountTokensRequest): CountTokensResponse.Internal =
try {
Expand All @@ -199,7 +209,7 @@ internal constructor(
.also { validateResponse(it) }
.body()
} catch (e: Throwable) {
throw FirebaseCommonAIException.from(e)
throw FirebaseAIException.from(e)
}

private fun HttpRequestBuilder.applyCommonHeaders() {
Expand Down Expand Up @@ -372,9 +382,9 @@ private fun GenerateContentResponse.Internal.validate() = apply {
if ((candidates?.isEmpty() != false) && promptFeedback == null) {
throw SerializationException("Error deserializing response, found no valid fields")
}
promptFeedback?.blockReason?.let { throw PromptBlockedException(this) }
promptFeedback?.blockReason?.let { throw PromptBlockedException(this.toPublic(), null, null) }
candidates
?.mapNotNull { it.finishReason }
?.firstOrNull { it != FinishReason.Internal.STOP }
?.let { throw ResponseStoppedException(this) }
?.let { throw ResponseStoppedException(this.toPublic()) }
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.google.firebase.ai.common.util

import android.util.Log
import com.google.firebase.ai.common.SerializationException
import com.google.firebase.ai.type.SerializationException
import kotlin.reflect.KClass
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.google.firebase.ai.type

import com.google.firebase.ai.FirebaseAI
import com.google.firebase.ai.common.FirebaseCommonAIException
import kotlinx.coroutines.TimeoutCancellationException

/** Parent class for any errors that occur from the [FirebaseAI] SDK. */
Expand All @@ -35,36 +34,6 @@ internal constructor(message: String, cause: Throwable? = null) : RuntimeExcepti
internal fun from(cause: Throwable): FirebaseAIException =
when (cause) {
is FirebaseAIException -> cause
is FirebaseCommonAIException ->
when (cause) {
is com.google.firebase.ai.common.SerializationException ->
SerializationException(cause.message ?: "", cause.cause)
is com.google.firebase.ai.common.ServerException ->
ServerException(cause.message ?: "", cause.cause)
is com.google.firebase.ai.common.InvalidAPIKeyException ->
InvalidAPIKeyException(cause.message ?: "")
is com.google.firebase.ai.common.PromptBlockedException ->
PromptBlockedException(cause.response?.toPublic(), cause.cause)
is com.google.firebase.ai.common.UnsupportedUserLocationException ->
UnsupportedUserLocationException(cause.cause)
is com.google.firebase.ai.common.InvalidStateException ->
InvalidStateException(cause.message ?: "", cause)
is com.google.firebase.ai.common.ResponseStoppedException ->
ResponseStoppedException(cause.response.toPublic(), cause.cause)
is com.google.firebase.ai.common.RequestTimeoutException ->
RequestTimeoutException(cause.message ?: "", cause.cause)
is com.google.firebase.ai.common.ServiceDisabledException ->
ServiceDisabledException(cause.message ?: "", cause.cause)
is com.google.firebase.ai.common.UnknownException ->
UnknownException(cause.message ?: "", cause.cause)
is com.google.firebase.ai.common.ContentBlockedException ->
ContentBlockedException(cause.message ?: "", cause.cause)
is com.google.firebase.ai.common.QuotaExceededException ->
QuotaExceededException(cause.message ?: "", cause.cause)
is com.google.firebase.ai.common.APINotConfiguredException ->
APINotConfiguredException(cause.message ?: "", cause.cause)
else -> UnknownException(cause.message ?: "", cause)
}
is TimeoutCancellationException ->
RequestTimeoutException("The request failed to complete in the allotted time.")
else -> UnknownException("Something unexpected happened.", cause)
Expand Down Expand Up @@ -238,3 +207,18 @@ public class PermissionMissingException(message: String, cause: Throwable? = nul
/** Catch all case for exceptions not explicitly expected. */
public class UnknownException internal constructor(message: String, cause: Throwable? = null) :
FirebaseAIException(message, cause)

internal fun makeMissingCaseException(
source: String,
ordinal: Int
): com.google.firebase.ai.type.SerializationException {
return com.google.firebase.ai.type.SerializationException(
"""
|Missing case for a $source: $ordinal
|This error indicates that one of the `toInternal` conversions needs updating.
|If you're a developer seeing this exception, please file an issue on our GitHub repo:
|https://github.com/firebase/firebase-android-sdk
"""
.trimMargin()
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.google.firebase.ai.type

import com.google.firebase.ai.common.makeMissingCaseException
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.google.firebase.ai.type

import com.google.firebase.ai.common.makeMissingCaseException
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.google.firebase.ai.type

import com.google.firebase.ai.common.makeMissingCaseException
import com.google.firebase.ai.common.util.FirstOrdinalSerializer
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.google.firebase.ai.type.FunctionCallingConfig
import com.google.firebase.ai.type.GoogleSearch
import com.google.firebase.ai.type.PublicPreviewAPI
import com.google.firebase.ai.type.RequestOptions
import com.google.firebase.ai.type.RequestTimeoutException
import com.google.firebase.ai.type.TextPart
import com.google.firebase.ai.type.Tool
import com.google.firebase.ai.type.ToolConfig
Expand Down
Loading