Skip to content
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
10 changes: 10 additions & 0 deletions FirebaseAI/Sources/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,13 @@ enum InvalidCandidateError: Error {
case emptyContent(underlyingError: Error)
case malformedContent(underlyingError: Error)
}

struct UnrecognizedRPCError: Error {
let responseBody: String
}

extension UnrecognizedRPCError: LocalizedError {
var errorDescription: String? {
return "Unrecognized error payload: \(responseBody)"
}
}
4 changes: 2 additions & 2 deletions FirebaseAI/Sources/GenerativeAIService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ struct GenerativeAIService {
logRPCError(rpcError)
return rpcError
} catch {
// TODO: Return an error about an unrecognized error payload with the response body
return error
let responseString = String(data: responseData, encoding: .utf8) ?? ""
return UnrecognizedRPCError(responseBody: responseString)
}
}

Expand Down
87 changes: 87 additions & 0 deletions FirebaseAI/Tests/Unit/GenerativeAIServiceTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import FirebaseAppCheckInterop
import FirebaseAuthInterop
import FirebaseCore
import XCTest

@testable import FirebaseAILogic

#if !os(watchOS)
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
final class GenerativeAIServiceTests: XCTestCase {
let testModelName = "test-model"
let testModelResourceName =
"projects/test-project-id/locations/test-location/publishers/google/models/test-model"
let apiConfig = FirebaseAI.defaultVertexAIAPIConfig

var urlSession: URLSession!
var model: GenerativeModel!

override func setUp() async throws {
let configuration = URLSessionConfiguration.default
configuration.protocolClasses = [MockURLProtocol.self]
urlSession = try XCTUnwrap(URLSession(configuration: configuration))
model = GenerativeModel(
modelName: testModelName,
modelResourceName: testModelResourceName,
firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(),
apiConfig: apiConfig,
tools: nil,
requestOptions: RequestOptions(),
urlSession: urlSession
)
}

override func tearDown() {
MockURLProtocol.requestHandler = nil
}

func testGenerateContent_failure_unrecognizedErrorPayload() async throws {
let expectedStatusCode = 500
let responseBody = "Internal Server Error"

// We need to construct the handler to return specific data
let tempURL = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
addTeardownBlock {
try? FileManager.default.removeItem(at: tempURL)
}

MockURLProtocol.requestHandler = { request in
let response = HTTPURLResponse(
url: request.url!,
statusCode: expectedStatusCode,
httpVersion: nil,
headerFields: nil
)!

try responseBody.write(to: tempURL, atomically: true, encoding: .utf8)
let stream = URL(fileURLWithPath: tempURL.path).lines
return (response, stream)
}

do {
_ = try await model.generateContent("test")
XCTFail("An error should have been thrown, but no error was thrown.")
} catch let GenerateContentError
.internalError(underlying: unrecognizedError as UnrecognizedRPCError) {
// MockURLProtocol appends a newline to the response.
XCTAssertEqual(unrecognizedError.responseBody, responseBody + "\n")
} catch {
XCTFail("Caught unexpected error: \(error)")
}
}
}
#endif // !os(watchOS)
Loading