Skip to content

Commit

Permalink
[Vertex AI] Add CustomNSError conformance to BackendError (#14029)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewheard authored Nov 5, 2024
1 parent bc41b66 commit db87a53
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
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 @@ final class GenerativeModelTests: XCTestCase {
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 @@ final class GenerativeModelTests: XCTestCase {
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

0 comments on commit db87a53

Please sign in to comment.