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

Refactor ApiClientError #285

Merged
merged 1 commit into from
Mar 13, 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
34 changes: 11 additions & 23 deletions Sources/XMTPiOS/ApiClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public typealias SubscribeRequest = Xmtp_MessageApi_V1_SubscribeRequest
// the GenericErrorDescribing protocol will catch all instances of the enum
// and generate the string descriptions.
protocol GenericErrorDescribing {
func generateDescription(error: GenericError) -> String
func generateApiDescription(error: GenericError) -> String
}

extension GenericErrorDescribing {
func generateDescription(error: GenericError) -> String {
func generateApiDescription(error: GenericError) -> String {
switch error {
case let .Client(message),
let .ClientBuilder(message),
Expand All @@ -41,23 +41,11 @@ extension GenericErrorDescribing {
}
}

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 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))"
}
struct ApiClientError: LocalizedError, GenericErrorDescribing {
var errorDescription: String?

init(error: GenericError, description: String) {
self.errorDescription = "\(description) \(generateApiDescription(error: error))"
}
}

Expand Down Expand Up @@ -120,15 +108,15 @@ final class GRPCApiClient: ApiClient {
do {
return try await rustClient.batchQuery(req: request.toFFI).fromFFI
} catch let error as GenericError {
throw ApiClientError.batchQueryError(error)
throw ApiClientError(error: error, description: "ApiClientError.batchQueryError:")
}
}

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

Expand Down Expand Up @@ -180,7 +168,7 @@ final class GRPCApiClient: ApiClient {
continuation.yield(nextEnvelope.fromFFI)
}
} catch let error as GenericError {
throw ApiClientError.subscribeError(error)
throw ApiClientError(error: error, description: "ApiClientError.subscribeError:")
}
}
}
Expand All @@ -190,7 +178,7 @@ final class GRPCApiClient: ApiClient {
do {
try await rustClient.publish(request: request.toFFI, authToken: authToken)
} catch let error as GenericError {
throw ApiClientError.publishError(error)
throw ApiClientError(error: error, description: "ApiClientError.publishError:")
}
}

Expand Down
Loading