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

Improved Error Handling #277

Merged
merged 1 commit into from
Mar 6, 2024
Merged
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
66 changes: 45 additions & 21 deletions Sources/XMTPiOS/ApiClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,46 @@ public typealias QueryRequest = Xmtp_MessageApi_V1_QueryRequest
public typealias QueryResponse = Xmtp_MessageApi_V1_QueryResponse
public typealias SubscribeRequest = Xmtp_MessageApi_V1_SubscribeRequest

public enum ApiClientError: Error, CustomStringConvertible {
case batchQueryError(String)
case queryError(String)
case publishError(String)
case subscribeError(String)
// This protocol is in place to enable extending error handling for errors
// thrown via call sites to LibXMTP.FfiConverterTypeGenericError. Adopting
// the GenericErrorDescribing protocol will catch all instances of the enum
// and generate the string descriptions.
protocol GenericErrorDescribing {
func generateDescription(error: GenericError) -> String
}

extension GenericErrorDescribing {
func generateDescription(error: GenericError) -> String {
switch error {
case let .Client(message),
let .ClientBuilder(message),
let .Storage(message),
let .ApiError(message),
let .GroupError(message),
let .Signature(message),
let .GroupMetadata(message),
let .Generic(message):
return message
}
}
}

public enum ApiClientError: Error, CustomStringConvertible, GenericErrorDescribing {
case batchQueryError(GenericError)
case queryError(GenericError)
case publishError(GenericError)
case subscribeError(GenericError)

public var description: String {
switch self {
case .batchQueryError(let err):
return "ApiClientError.batchQueryError: \(err)"
case .queryError(let err):
return "ApiClientError.queryError: \(err)"
case .publishError(let err):
return "ApiClientError.publishError: \(err)"
case .subscribeError(let err):
return "ApiClientError.subscribeError: \(err)"
case .batchQueryError(let error):
return "ApiClientError.batchQueryError: \(generateDescription(error: error))"
case .queryError(let error):
return "ApiClientError.queryError: \(generateDescription(error: error))"
case .publishError(let error):
return "ApiClientError.publishError: \(generateDescription(error: error))"
case .subscribeError(let error):
return "ApiClientError.subscribeError: \(generateDescription(error: error))"
}
}
}
Expand Down Expand Up @@ -95,16 +119,16 @@ final class GRPCApiClient: ApiClient {
func batchQuery(request: BatchQueryRequest) async throws -> BatchQueryResponse {
do {
return try await rustClient.batchQuery(req: request.toFFI).fromFFI
} catch {
throw ApiClientError.batchQueryError(error.localizedDescription)
} catch let error as GenericError {
throw ApiClientError.batchQueryError(error)
}
}

func query(request: QueryRequest) async throws -> QueryResponse {
do {
return try await rustClient.query(request: request.toFFI).fromFFI
} catch {
throw ApiClientError.queryError(error.localizedDescription)
} catch let error as GenericError {
throw ApiClientError.queryError(error)
}
}

Expand Down Expand Up @@ -155,8 +179,8 @@ final class GRPCApiClient: ApiClient {
let nextEnvelope = try await subscription.next()
continuation.yield(nextEnvelope.fromFFI)
}
} catch {
throw ApiClientError.subscribeError(error.localizedDescription)
} catch let error as GenericError {
throw ApiClientError.subscribeError(error)
}
}
}
Expand All @@ -165,8 +189,8 @@ final class GRPCApiClient: ApiClient {
func publish(request: PublishRequest) async throws {
do {
try await rustClient.publish(request: request.toFFI, authToken: authToken)
} catch {
throw ApiClientError.publishError(error.localizedDescription)
} catch let error as GenericError {
throw ApiClientError.publishError(error)
}
}

Expand Down
Loading