Skip to content

Commit

Permalink
add new error for disabled service, with test (#148)
Browse files Browse the repository at this point in the history
Co-authored-by: David Motsonashvili <davidmotson@google.com>
  • Loading branch information
davidmotson and David Motsonashvili authored May 22, 2024
1 parent 35ee750 commit 8868baa
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
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"
}
}
]
}
}

0 comments on commit 8868baa

Please sign in to comment.