Skip to content

Commit

Permalink
Capture and share the backend's message for an invalid API key (googl…
Browse files Browse the repository at this point in the history
  • Loading branch information
paulb777 authored and G.Dev.Ssomsak committed Jun 21, 2024
1 parent 23ae87d commit b8ebe9f
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Sources/GoogleAI/GenerativeModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,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 @@ -248,8 +248,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

0 comments on commit b8ebe9f

Please sign in to comment.