From bb9563feade02b79f0e69997362a05761d3da859 Mon Sep 17 00:00:00 2001 From: Paul Beusterien Date: Fri, 15 Mar 2024 08:44:35 -0700 Subject: [PATCH] Capture and share the backend's message for an invalid API key (#120) --- .../ChatSample/Views/ErrorDetailsView.swift | 2 +- Sources/GoogleAI/GenerateContentError.swift | 2 +- Sources/GoogleAI/GenerativeModel.swift | 2 +- Tests/GoogleAITests/GenerativeModelTests.swift | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Examples/GenerativeAISample/ChatSample/Views/ErrorDetailsView.swift b/Examples/GenerativeAISample/ChatSample/Views/ErrorDetailsView.swift index eaa1fca..5ec5ddc 100644 --- a/Examples/GenerativeAISample/ChatSample/Views/ErrorDetailsView.swift +++ b/Examples/GenerativeAISample/ChatSample/Views/ErrorDetailsView.swift @@ -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") { diff --git a/Sources/GoogleAI/GenerateContentError.swift b/Sources/GoogleAI/GenerateContentError.swift index 1f7b81f..055928e 100644 --- a/Sources/GoogleAI/GenerateContentError.swift +++ b/Sources/GoogleAI/GenerateContentError.swift @@ -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. /// diff --git a/Sources/GoogleAI/GenerativeModel.swift b/Sources/GoogleAI/GenerativeModel.swift index 39fdbb8..4eb5caa 100644 --- a/Sources/GoogleAI/GenerativeModel.swift +++ b/Sources/GoogleAI/GenerativeModel.swift @@ -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 } diff --git a/Tests/GoogleAITests/GenerativeModelTests.swift b/Tests/GoogleAITests/GenerativeModelTests.swift index 41f6844..5cd14d9 100644 --- a/Tests/GoogleAITests/GenerativeModelTests.swift +++ b/Tests/GoogleAITests/GenerativeModelTests.swift @@ -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)") }