From a6a7cbad72c463bb68480df2239e47aa8c26fb6c Mon Sep 17 00:00:00 2001 From: Dariusz Rybicki <dariusz@elixxir.io> Date: Thu, 2 Jun 2022 14:32:09 +0200 Subject: [PATCH] Add ClientError and ClientErrorListener --- Sources/ElixxirDAppsSDK/Client.swift | 3 ++ Sources/ElixxirDAppsSDK/ClientError.swift | 11 +++++ .../ElixxirDAppsSDK/ClientErrorListener.swift | 48 +++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 Sources/ElixxirDAppsSDK/ClientError.swift create mode 100644 Sources/ElixxirDAppsSDK/ClientErrorListener.swift diff --git a/Sources/ElixxirDAppsSDK/Client.swift b/Sources/ElixxirDAppsSDK/Client.swift index a3b790a8..30ac66be 100644 --- a/Sources/ElixxirDAppsSDK/Client.swift +++ b/Sources/ElixxirDAppsSDK/Client.swift @@ -5,6 +5,7 @@ public struct Client { public var waitForNetwork: NetworkWaiter public var isNetworkHealthy: NetworkHealthProvider public var monitorNetworkHealth: NetworkHealthListener + public var listenErrors: ClientErrorListener public var makeIdentity: IdentityMaker public var connect: ConnectionMaker public var waitForDelivery: MessageDeliveryWaiter @@ -17,6 +18,7 @@ extension Client { waitForNetwork: .live(bindingsClient: bindingsClient), isNetworkHealthy: .live(bindingsClient: bindingsClient), monitorNetworkHealth: .live(bindingsClient: bindingsClient), + listenErrors: .live(bindingsClient: bindingsClient), makeIdentity: .live(bindingsClient: bindingsClient), connect: .live(bindingsClient: bindingsClient), waitForDelivery: .live(bindingsClient: bindingsClient) @@ -31,6 +33,7 @@ extension Client { waitForNetwork: .failing, isNetworkHealthy: .failing, monitorNetworkHealth: .failing, + listenErrors: .failing, makeIdentity: .failing, connect: .failing, waitForDelivery: .failing diff --git a/Sources/ElixxirDAppsSDK/ClientError.swift b/Sources/ElixxirDAppsSDK/ClientError.swift new file mode 100644 index 00000000..a0a91178 --- /dev/null +++ b/Sources/ElixxirDAppsSDK/ClientError.swift @@ -0,0 +1,11 @@ +public struct ClientError: Error, Equatable { + public init(source: String, message: String, trace: String) { + self.source = source + self.message = message + self.trace = trace + } + + public var source: String + public var message: String + public var trace: String +} diff --git a/Sources/ElixxirDAppsSDK/ClientErrorListener.swift b/Sources/ElixxirDAppsSDK/ClientErrorListener.swift new file mode 100644 index 00000000..5e7b0446 --- /dev/null +++ b/Sources/ElixxirDAppsSDK/ClientErrorListener.swift @@ -0,0 +1,48 @@ +import Bindings + +public struct ClientErrorListener { + public var listen: (@escaping (ClientError) -> Void) -> Void + + public func callAsFunction(callback: @escaping (ClientError) -> Void) { + listen(callback) + } +} + +extension ClientErrorListener { + public static func live(bindingsClient: BindingsClient) -> ClientErrorListener { + ClientErrorListener { callback in + let listener = Listener(onReport: callback) + bindingsClient.registerErrorCallback(listener) + } + } +} + +private final class Listener: NSObject, BindingsClientErrorProtocol { + init(onReport: @escaping (ClientError) -> Void) { + self.onReport = onReport + super.init() + } + + let onReport: (ClientError) -> Void + + func report(_ source: String?, message: String?, trace: String?) { + guard let source = source else { + fatalError("BindingsClientError.source is `nil`") + } + guard let message = message else { + fatalError("BindingsClientError.message is `nil`") + } + guard let trace = trace else { + fatalError("BindingsClientError.trace is `nil`") + } + onReport(ClientError(source: source, message: message, trace: trace)) + } +} + +#if DEBUG +extension ClientErrorListener { + public static let failing = ClientErrorListener { _ in + fatalError("Not implemented") + } +} +#endif -- GitLab