Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new error for disabled service, with test #148

Merged
merged 6 commits into from
May 22, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,13 @@ private fun fullModelName(name: String): String = name.takeIf { it.contains("/")
private suspend fun validateResponse(response: HttpResponse) {
if (response.status == HttpStatusCode.OK) return
val text = response.bodyAsText()
val message =
val error =
try {
JSON.decodeFromString<GRpcErrorResponse>(text).error.message
JSON.decodeFromString<GRpcErrorResponse>(text).error
} catch (e: Throwable) {
"Unexpected Response:\n$text"
throw ServerException("Unexpected Response:\n$text $e")
}
val message = error.message
if (message.contains("API key not valid")) {
throw InvalidAPIKeyException(message)
}
Expand All @@ -239,6 +240,9 @@ private suspend fun validateResponse(response: HttpResponse) {
if (message.contains("quota")) {
throw QuotaExceededException(message)
}
if (error.details.any { "SERVICE_DISABLED" == it.reason }) {
throw ServiceDisabledException(message)
}
throw ServerException(message)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ class RequestTimeoutException(message: String, cause: Throwable? = null) :
class QuotaExceededException(message: String, cause: Throwable? = null) :
GoogleGenerativeAIException(message, cause)

/** The service is not enabled for this project. Visit the Firebase Console to enable it. */
class ServiceDisabledException(message: String, cause: Throwable? = null) :
GoogleGenerativeAIException(message, cause)

/** Catch all case for exceptions not explicitly expected. */
class UnknownException(message: String, cause: Throwable? = null) :
GoogleGenerativeAIException(message, cause)
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,7 @@ enum class FinishReason {
data class GRpcError(
val code: Int,
val message: String,
val details: List<GRpcErrorDetails>,
)

@Serializable data class GRpcErrorDetails(val reason: String? = null)
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,14 @@ internal class UnarySnapshotTests {
}
}
}

@Test
fun `service disabled`() =
goldenUnaryFile("failure-service-disabled.json", HttpStatusCode.Forbidden) {
withTimeout(testTimeout) {
shouldThrow<ServiceDisabledException> {
apiController.generateContent(textGenerateContentRequest("prompt"))
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"error": {
"code": 403,
"message": "Firebase ML API has not been used in project 12345 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/firebaseml.googleapis.com/overview?project=12345 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",
"status": "PERMISSION_DENIED",
"details": [
{
"@type": "type.googleapis.com/google.rpc.Help",
"links": [
{
"description": "Google developers console API activation",
"url": "https://console.developers.google.com/apis/api/firebaseml.googleapis.com/overview?project=12345"
}
]
},
{
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
"reason": "SERVICE_DISABLED",
"domain": "googleapis.com",
"metadata": {
"service": "firebaseml.googleapis.com",
"consumer": "projects/12345"
}
}
]
}
}
Loading