Skip to content

Commit db87a53

Browse files
authored
[Vertex AI] Add CustomNSError conformance to BackendError (#14029)
1 parent bc41b66 commit db87a53

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

FirebaseVertexAI/Sources/Constants.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,10 @@ import Foundation
1818
enum Constants {
1919
/// The Vertex AI backend endpoint URL.
2020
static let baseURL = "https://firebasevertexai.googleapis.com"
21+
22+
/// The base reverse-DNS name for `NSError` or `CustomNSError` error domains.
23+
///
24+
/// - Important: A suffix must be appended to produce an error domain (e.g.,
25+
/// "com.google.firebase.vertexai.ExampleError").
26+
static let baseErrorDomain = "com.google.firebase.vertexai"
2127
}

FirebaseVertexAI/Sources/Types/Internal/Errors/BackendError.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,27 @@ struct BackendError: Error {
3636
}
3737
}
3838

39+
// MARK: - CustomNSError Conformance
40+
41+
extension BackendError: CustomNSError {
42+
public static var errorDomain: String {
43+
return "\(Constants.baseErrorDomain).\(Self.self)"
44+
}
45+
46+
var errorCode: Int {
47+
return httpResponseCode
48+
}
49+
50+
var errorUserInfo: [String: Any] {
51+
return [
52+
NSLocalizedDescriptionKey:
53+
"\(message) (\(Self.errorDomain) - HTTP \(httpResponseCode) \(status.rawValue))",
54+
]
55+
}
56+
}
57+
58+
// MARK: - Decodable Conformance
59+
3960
extension BackendError: Decodable {
4061
enum CodingKeys: CodingKey {
4162
case error

FirebaseVertexAI/Tests/Unit/GenerativeModelTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,12 @@ final class GenerativeModelTests: XCTestCase {
506506
XCTAssertEqual(error.httpResponseCode, 400)
507507
XCTAssertEqual(error.status, .invalidArgument)
508508
XCTAssertEqual(error.message, "API key not valid. Please pass a valid API key.")
509+
XCTAssertTrue(error.localizedDescription.contains(error.message))
510+
XCTAssertTrue(error.localizedDescription.contains(error.status.rawValue))
511+
XCTAssertTrue(error.localizedDescription.contains("\(error.httpResponseCode)"))
512+
let nsError = error as NSError
513+
XCTAssertEqual(nsError.domain, "\(Constants.baseErrorDomain).\(BackendError.self)")
514+
XCTAssertEqual(nsError.code, error.httpResponseCode)
509515
return
510516
} catch {
511517
XCTFail("Should throw GenerateContentError.internalError(RPCError); error thrown: \(error)")
@@ -853,6 +859,12 @@ final class GenerativeModelTests: XCTestCase {
853859
XCTAssertEqual(error.httpResponseCode, 400)
854860
XCTAssertEqual(error.status, .invalidArgument)
855861
XCTAssertEqual(error.message, "API key not valid. Please pass a valid API key.")
862+
XCTAssertTrue(error.localizedDescription.contains(error.message))
863+
XCTAssertTrue(error.localizedDescription.contains(error.status.rawValue))
864+
XCTAssertTrue(error.localizedDescription.contains("\(error.httpResponseCode)"))
865+
let nsError = error as NSError
866+
XCTAssertEqual(nsError.domain, "\(Constants.baseErrorDomain).\(BackendError.self)")
867+
XCTAssertEqual(nsError.code, error.httpResponseCode)
856868
return
857869
}
858870

0 commit comments

Comments
 (0)