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 UnsupportedUserLocationException for better error handling #60

Merged
merged 2 commits into from
Feb 14, 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 @@ -19,6 +19,7 @@ package com.google.ai.client.generativeai.internal.api
import com.google.ai.client.generativeai.BuildConfig
import com.google.ai.client.generativeai.internal.util.decodeToFlow
import com.google.ai.client.generativeai.type.ServerException
import com.google.ai.client.generativeai.type.UnsupportedUserLocationException
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.engine.HttpClientEngine
Expand Down Expand Up @@ -174,6 +175,10 @@ private suspend fun validateResponse(response: HttpResponse) {
"Unexpected Response:\n$text"
}

// TODO (b/325117891): Use a better method than string matching.
if (message == "User location is not supported for the API use.") {
throw UnsupportedUserLocationException()
}
throw ServerException(message)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ class PromptBlockedException(val response: GenerateContentResponse, cause: Throw
cause
)

/**
* The user's location (region) is not supported by the API.
*
* See the Google documentation for a
* [list of regions](https://ai.google.dev/available_regions#available_regions) (countries and
* territories) where the API is available.
*/
class UnsupportedUserLocationException(cause: Throwable? = null) :
GoogleGenerativeAIException("User location is not supported for the API use.", cause)

/**
* Some form of state occurred that shouldn't have.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.google.ai.client.generativeai.type.PromptBlockedException
import com.google.ai.client.generativeai.type.ResponseStoppedException
import com.google.ai.client.generativeai.type.SerializationException
import com.google.ai.client.generativeai.type.ServerException
import com.google.ai.client.generativeai.type.UnsupportedUserLocationException
import com.google.ai.client.generativeai.util.goldenUnaryFile
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.matchers.should
Expand Down Expand Up @@ -94,6 +95,14 @@ internal class UnarySnapshotTests {
withTimeout(testTimeout) { shouldThrow<ServerException> { model.generateContent() } }
}

@Test
fun `user location error`() =
goldenUnaryFile("failure-unsupported-user-location.json", HttpStatusCode.PreconditionFailed) {
withTimeout(testTimeout) {
shouldThrow<UnsupportedUserLocationException> { model.generateContent() }
}
}

@Test
fun `stopped for safety`() =
goldenUnaryFile("failure-finish-reason-safety.json") {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"error": {
"code": 400,
"message": "User location is not supported for the API use.",
"status": "FAILED_PRECONDITION",
"details": [
{
"@type": "type.googleapis.com/google.rpc.DebugInfo",
"detail": "[ORIGINAL ERROR] generic::failed_precondition: User location is not supported for the API use. [google.rpc.error_details_ext] { message: \"User location is not supported for the API use.\" }"
}
]
}
}
Loading