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 1ed073f commit bb9563f
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
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 @@ -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 bb9563f

Please sign in to comment.