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

Capture and share the backend's message for an invalid API key #120

Merged
merged 2 commits into from
Mar 15, 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 @@ -239,7 +239,7 @@ struct ErrorDetailsView: View {
}

#Preview("Invalid API Key") {
ErrorDetailsView(error: GenerateContentError.invalidAPIKey)
ErrorDetailsView(error: GenerateContentError.invalidAPIKey(message: "Fix API key placeholder"))
}

#Preview("Unsupported User Location") {
Expand Down
2 changes: 1 addition & 1 deletion Sources/GoogleAI/GenerateContentError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public enum GenerateContentError: Error {
case responseStoppedEarly(reason: FinishReason, response: GenerateContentResponse)

/// The provided API key is invalid.
case invalidAPIKey
case invalidAPIKey(message: String)

/// The user's location (region) is not supported by the API.
///
Expand Down
2 changes: 1 addition & 1 deletion Sources/GoogleAI/GenerativeModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public final class GenerativeModel {
if let error = error as? GenerateContentError {
return error
} else if let error = error as? RPCError, error.isInvalidAPIKeyError() {
return GenerateContentError.invalidAPIKey
return GenerateContentError.invalidAPIKey(message: error.message)
} else if let error = error as? RPCError, error.isUnsupportedUserLocationError() {
return GenerateContentError.unsupportedUserLocation
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/GoogleAITests/GenerativeModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ final class GenerativeModelTests: XCTestCase {
do {
_ = try await model.generateContent(testPrompt)
XCTFail("Should throw GenerateContentError.internalError; no error thrown.")
} catch GenerateContentError.invalidAPIKey {
// Do nothing, catching a GenerateContentError.invalidAPIKey error is expected.
} catch let GenerateContentError.invalidAPIKey(message) {
XCTAssertEqual(message, "API key not valid. Please pass a valid API key.")
} catch {
XCTFail("Should throw GenerateContentError.invalidAPIKey; error thrown: \(error)")
}
Expand Down
Loading