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

added quota exceeded exception #93

Merged
merged 4 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
added quota exceeded exception
  • Loading branch information
David Motsonashvili committed Mar 22, 2024
commit 536186f8aaa43884c1351c4eacb55c1a96a62932
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ private suspend fun validateResponse(response: HttpResponse) {
if (message == "User location is not supported for the API use.") {
throw UnsupportedUserLocationException()
}
if (message.contains("quota")) {
throw QuotaExceededException(message)
}
throw ServerException(message)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ sealed class GoogleGenerativeAIException(message: String, cause: Throwable? = nu
is kotlinx.serialization.SerializationException ->
SerializationException(
"Something went wrong while trying to deserialize a response from the server.",
cause
cause,
)
is TimeoutCancellationException ->
RequestTimeoutException("The request failed to complete in the allotted time.")
Expand Down Expand Up @@ -68,7 +68,7 @@ class InvalidAPIKeyException(message: String, cause: Throwable? = null) :
class PromptBlockedException(val response: GenerateContentResponse, cause: Throwable? = null) :
GoogleGenerativeAIException(
"Prompt was blocked: ${response.promptFeedback?.blockReason?.name}",
cause
cause,
)

/**
Expand Down Expand Up @@ -97,7 +97,7 @@ class InvalidStateException(message: String, cause: Throwable? = null) :
class ResponseStoppedException(val response: GenerateContentResponse, cause: Throwable? = null) :
GoogleGenerativeAIException(
"Content generation stopped. Reason: ${response.candidates?.first()?.finishReason?.name}",
cause
cause,
)

/**
Expand All @@ -108,6 +108,10 @@ class ResponseStoppedException(val response: GenerateContentResponse, cause: Thr
class RequestTimeoutException(message: String, cause: Throwable? = null) :
GoogleGenerativeAIException(message, cause)

/** The quota for this API key is depleted, retry this request at a later time. */
class QuotaExceededException(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 @@ -42,9 +42,7 @@ sealed class GoogleGenerativeAIException(message: String, cause: Throwable? = nu
is com.google.ai.client.generativeai.common.ServerException ->
ServerException(cause.message ?: "", cause.cause)
is com.google.ai.client.generativeai.common.InvalidAPIKeyException ->
InvalidAPIKeyException(
cause.message ?: "",
)
InvalidAPIKeyException(cause.message ?: "")
is com.google.ai.client.generativeai.common.PromptBlockedException ->
PromptBlockedException(cause.response.toPublic(), cause.cause)
is com.google.ai.client.generativeai.common.UnsupportedUserLocationException ->
Expand All @@ -57,6 +55,8 @@ sealed class GoogleGenerativeAIException(message: String, cause: Throwable? = nu
RequestTimeoutException(cause.message ?: "", cause.cause)
is com.google.ai.client.generativeai.common.UnknownException ->
UnknownException(cause.message ?: "", cause.cause)
is com.google.ai.client.generativeai.common.QuotaExceededException ->
QuotaExceededException(cause.message ?: "", cause.cause)
else -> UnknownException(cause.message ?: "", cause)
}
is TimeoutCancellationException ->
Expand Down Expand Up @@ -89,7 +89,7 @@ class InvalidAPIKeyException(message: String, cause: Throwable? = null) :
class PromptBlockedException(val response: GenerateContentResponse, cause: Throwable? = null) :
GoogleGenerativeAIException(
"Prompt was blocked: ${response.promptFeedback?.blockReason?.name}",
cause
cause,
)

/**
Expand Down Expand Up @@ -119,7 +119,7 @@ class InvalidStateException(message: String, cause: Throwable? = null) :
class ResponseStoppedException(val response: GenerateContentResponse, cause: Throwable? = null) :
GoogleGenerativeAIException(
"Content generation stopped. Reason: ${response.candidates.first().finishReason?.name}",
cause
cause,
)

/**
Expand All @@ -130,6 +130,10 @@ class ResponseStoppedException(val response: GenerateContentResponse, cause: Thr
class RequestTimeoutException(message: String, cause: Throwable? = null) :
GoogleGenerativeAIException(message, cause)

/** The quota for this API key is depleted, retry this request at a later time. */
class QuotaExceededException(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)
Loading