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] Add CustomNSError conformance to BackendError #14029

Merged
merged 1 commit into from
Nov 5, 2024
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
6 changes: 6 additions & 0 deletions FirebaseVertexAI/Sources/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@ import Foundation
enum Constants {
/// The Vertex AI backend endpoint URL.
static let baseURL = "https://firebasevertexai.googleapis.com"

/// The base reverse-DNS name for `NSError` or `CustomNSError` error domains.
///
/// - Important: A suffix must be appended to produce an error domain (e.g.,
/// "com.google.firebase.vertexai.ExampleError").
static let baseErrorDomain = "com.google.firebase.vertexai"
}
21 changes: 21 additions & 0 deletions FirebaseVertexAI/Sources/Types/Internal/Errors/BackendError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ struct BackendError: Error {
}
}

// MARK: - CustomNSError Conformance

extension BackendError: CustomNSError {
public static var errorDomain: String {
return "\(Constants.baseErrorDomain).\(Self.self)"
}

var errorCode: Int {
return httpResponseCode
}

var errorUserInfo: [String: Any] {
return [
NSLocalizedDescriptionKey:
"\(message) (\(Self.errorDomain) - HTTP \(httpResponseCode) \(status.rawValue))",
]
}
}

// MARK: - Decodable Conformance

extension BackendError: Decodable {
enum CodingKeys: CodingKey {
case error
Expand Down
12 changes: 12 additions & 0 deletions FirebaseVertexAI/Tests/Unit/GenerativeModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,12 @@
XCTAssertEqual(error.httpResponseCode, 400)
XCTAssertEqual(error.status, .invalidArgument)
XCTAssertEqual(error.message, "API key not valid. Please pass a valid API key.")
XCTAssertTrue(error.localizedDescription.contains(error.message))
XCTAssertTrue(error.localizedDescription.contains(error.status.rawValue))
XCTAssertTrue(error.localizedDescription.contains("\(error.httpResponseCode)"))
let nsError = error as NSError
XCTAssertEqual(nsError.domain, "\(Constants.baseErrorDomain).\(BackendError.self)")
XCTAssertEqual(nsError.code, error.httpResponseCode)
return
} catch {
XCTFail("Should throw GenerateContentError.internalError(RPCError); error thrown: \(error)")
Expand Down Expand Up @@ -853,6 +859,12 @@
XCTAssertEqual(error.httpResponseCode, 400)
XCTAssertEqual(error.status, .invalidArgument)
XCTAssertEqual(error.message, "API key not valid. Please pass a valid API key.")
XCTAssertTrue(error.localizedDescription.contains(error.message))
XCTAssertTrue(error.localizedDescription.contains(error.status.rawValue))
XCTAssertTrue(error.localizedDescription.contains("\(error.httpResponseCode)"))
let nsError = error as NSError
XCTAssertEqual(nsError.domain, "\(Constants.baseErrorDomain).\(BackendError.self)")
XCTAssertEqual(nsError.code, error.httpResponseCode)
return
}

Expand Down Expand Up @@ -1420,7 +1432,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 1435 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 @@ -1446,7 +1458,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 1461 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