Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Vertex AI] Replace legacy errors in GenerativeAIService #14036

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 9 additions & 11 deletions FirebaseVertexAI/Sources/GenerativeAIService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,16 @@ struct GenerativeAIService {
}

private func httpResponse(urlResponse: URLResponse) throws -> HTTPURLResponse {
// Verify the status code is 200
// The following condition should always be true: "Whenever you make HTTP URL load requests, any
// response objects you get back from the URLSession, NSURLConnection, or NSURLDownload class
// are instances of the HTTPURLResponse class.
andrewheard marked this conversation as resolved.
Show resolved Hide resolved
guard let response = urlResponse as? HTTPURLResponse else {
VertexLog.error(
code: .generativeAIServiceNonHTTPResponse,
"Response wasn't an HTTP response, internal error \(urlResponse)"
)
throw NSError(
domain: "com.google.generative-ai",
code: -1,
throw URLError(
.badServerResponse,
userInfo: [NSLocalizedDescriptionKey: "Response was not an HTTP response."]
)
}
Expand All @@ -229,14 +230,11 @@ struct GenerativeAIService {

private func jsonData(jsonText: String) throws -> Data {
guard let data = jsonText.data(using: .utf8) else {
let error = NSError(
domain: "com.google.generative-ai",
code: -1,
userInfo: [NSLocalizedDescriptionKey: "Could not parse response as UTF8."]
)
throw error
throw DecodingError.dataCorrupted(DecodingError.Context(
codingPath: [],
debugDescription: "Could not parse response as UTF8."
))
}

return data
}

Expand Down
3 changes: 3 additions & 0 deletions FirebaseVertexAI/Tests/Unit/GenerativeModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,9 @@
return
}
XCTAssertEqual(underlyingError.localizedDescription, "Response was not an HTTP response.")
let underlyingNSError = underlyingError as NSError
XCTAssertEqual(underlyingNSError.domain, NSURLErrorDomain)
XCTAssertEqual(underlyingNSError.code, URLError.Code.badServerResponse.rawValue)
}

func testGenerateContent_failure_invalidResponse() async throws {
Expand Down Expand Up @@ -1432,7 +1435,7 @@
#if os(watchOS)
throw XCTSkip("Custom URL protocols are unsupported in watchOS 2 and later.")
#endif // os(watchOS)
return { request in

Check warning on line 1438 in FirebaseVertexAI/Tests/Unit/GenerativeModelTests.swift

View workflow job for this annotation

GitHub Actions / spm-unit (macos-15, Xcode_16, watchOS)

code after 'throw' will never be executed
// This is *not* an HTTPURLResponse
let response = URLResponse(
url: request.url!,
Expand All @@ -1458,7 +1461,7 @@
#if os(watchOS)
throw XCTSkip("Custom URL protocols are unsupported in watchOS 2 and later.")
#endif // os(watchOS)
let bundle = BundleTestUtil.bundle()

Check warning on line 1464 in FirebaseVertexAI/Tests/Unit/GenerativeModelTests.swift

View workflow job for this annotation

GitHub Actions / spm-unit (macos-15, Xcode_16, watchOS)

code after 'throw' will never be executed
let fileURL = try XCTUnwrap(bundle.url(forResource: name, withExtension: ext))
return { request in
let requestURL = try XCTUnwrap(request.url)
Expand Down
Loading