Skip to content

Commit

Permalink
Capture and share the backend's message for an invalid API key
Browse files Browse the repository at this point in the history
  • Loading branch information
paulb777 committed Mar 15, 2024
1 parent dfea475 commit 771bb77
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
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 GenerateContentError.invalidAPIKey(let message) {
XCTAssertEqual(message, "API key not valid. Please pass a valid API key.")
} catch {
XCTFail("Should throw GenerateContentError.invalidAPIKey; error thrown: \(error)")
}
Expand Down

0 comments on commit 771bb77

Please sign in to comment.