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

[Core] Networking error publisher #1070

Merged
merged 5 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions Sources/WalletConnectNetworking/NetworkInteracting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ public protocol NetworkInteracting {
func subscribeOnRequest<RequestParams: Codable>(
protocolMethod: ProtocolMethod,
requestOfType: RequestParams.Type,
errorHandler: ErrorHandler?,
subscription: @escaping (RequestSubscriptionPayload<RequestParams>) async throws -> Void
)

func subscribeOnResponse<Request: Codable, Response: Codable>(
protocolMethod: ProtocolMethod,
requestOfType: Request.Type,
responseOfType: Response.Type,
errorHandler: ErrorHandler?,
subscription: @escaping (ResponseSubscriptionPayload<Request, Response>) async throws -> Void
)

Expand Down
1 change: 0 additions & 1 deletion Sources/WalletConnectNetworking/NetworkingClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Combine
public protocol NetworkingClient {
var socketConnectionStatusPublisher: AnyPublisher<SocketConnectionStatus, Never> { get }
var logsPublisher: AnyPublisher<Log, Never> { get }
var errorPublisher: AnyPublisher<Error, Never> { get }
func setLogging(level: LoggingLevel)
func connect() throws
func disconnect(closeCode: URLSessionWebSocketTask.CloseCode) throws
Expand Down
16 changes: 6 additions & 10 deletions Sources/WalletConnectNetworking/NetworkingInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ public class NetworkingInteractor: NetworkInteracting {
private let rpcHistory: RPCHistory
private let logger: ConsoleLogging

private let errorPublisherSubject = PassthroughSubject<Error, Never>()

private let requestPublisherSubject = PassthroughSubject<(topic: String, request: RPCRequest, decryptedPayload: Data, publishedAt: Date, derivedTopic: String?), Never>()
private let responsePublisherSubject = PassthroughSubject<(topic: String, request: RPCRequest, response: RPCResponse, publishedAt: Date, derivedTopic: String?), Never>()

Expand All @@ -21,10 +19,6 @@ public class NetworkingInteractor: NetworkInteracting {
responsePublisherSubject.eraseToAnyPublisher()
}

public var errorPublisher: AnyPublisher<Error, Never> {
return errorPublisherSubject.eraseToAnyPublisher()
}

public var logsPublisher: AnyPublisher<Log, Never> {
logger.logsPublisher
.merge(with: serializer.logsPublisher)
Expand Down Expand Up @@ -93,15 +87,16 @@ public class NetworkingInteractor: NetworkInteracting {
public func subscribeOnRequest<RequestParams: Codable>(
protocolMethod: ProtocolMethod,
requestOfType: RequestParams.Type,
errorHandler: ErrorHandler?,
subscription: @escaping (RequestSubscriptionPayload<RequestParams>) async throws -> Void
) {
requestSubscription(on: protocolMethod)
.sink { [unowned self] (payload: RequestSubscriptionPayload<RequestParams>) in
.sink { (payload: RequestSubscriptionPayload<RequestParams>) in
Task(priority: .high) {
do {
try await subscription(payload)
} catch {
errorPublisherSubject.send(error)
errorHandler?.handle(error: error)
}
}
}.store(in: &publishers)
Expand All @@ -111,15 +106,16 @@ public class NetworkingInteractor: NetworkInteracting {
protocolMethod: ProtocolMethod,
requestOfType: Request.Type,
responseOfType: Response.Type,
errorHandler: ErrorHandler?,
subscription: @escaping (ResponseSubscriptionPayload<Request, Response>) async throws -> Void
) {
responseSubscription(on: protocolMethod)
.sink { [unowned self] (payload: ResponseSubscriptionPayload<Request, Response>) in
.sink { (payload: ResponseSubscriptionPayload<Request, Response>) in
Task(priority: .high) {
do {
try await subscription(payload)
} catch {
errorPublisherSubject.send(error)
errorHandler?.handle(error: error)
}
}
}.store(in: &publishers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class DeleteNotifySubscriptionSubscriber {
private func subscribeForDeleteSubscription() {
networkingInteractor.subscribeOnRequest(
protocolMethod: NotifyDeleteProtocolMethod(),
requestOfType: NotifyDeleteResponsePayload.Wrapper.self
requestOfType: NotifyDeleteResponsePayload.Wrapper.self,
errorHandler: logger
) { [unowned self] payload in
let (_, _) = try NotifyDeleteResponsePayload.decodeAndVerify(from: payload.request)
logger.debug("Peer deleted subscription")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class NotifyMessageSubscriber {
private func subscribeForNotifyMessages() {
networkingInteractor.subscribeOnRequest(
protocolMethod: NotifyMessageProtocolMethod(),
requestOfType: NotifyMessagePayload.Wrapper.self
requestOfType: NotifyMessagePayload.Wrapper.self,
errorHandler: logger
) { [unowned self] payload in
logger.debug("Received Notify Message on topic: \(payload.topic)", properties: ["topic": payload.topic])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ private extension NotifyUpdateResponseSubscriber {
networkingInteractor.subscribeOnResponse(
protocolMethod: NotifyUpdateProtocolMethod(),
requestOfType: NotifyUpdatePayload.Wrapper.self,
responseOfType: NotifyUpdateResponsePayload.Wrapper.self
responseOfType: NotifyUpdateResponsePayload.Wrapper.self,
errorHandler: logger
) { [unowned self] payload in
logger.debug("Received Notify Update response")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class NotifySubscribeResponseSubscriber {
networkingInteractor.subscribeOnResponse(
protocolMethod: NotifySubscribeProtocolMethod(),
requestOfType: NotifySubscriptionPayload.Wrapper.self,
responseOfType: NotifySubscriptionResponsePayload.Wrapper.self
responseOfType: NotifySubscriptionResponsePayload.Wrapper.self,
errorHandler: logger
) { [unowned self] payload in
logger.debug("Received Notify Subscribe response")

Expand Down
8 changes: 6 additions & 2 deletions Sources/WalletConnectUtils/Logger/ConsoleLogger.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation
import Combine

public protocol ConsoleLogging {
public protocol ConsoleLogging: ErrorHandler {
var logsPublisher: AnyPublisher<Log, Never> { get }
func debug(_ items: Any..., file: String, function: String, line: Int, properties: [String: String]?)
func info(_ items: Any..., file: String, function: String, line: Int)
Expand Down Expand Up @@ -104,9 +104,11 @@ extension ConsoleLogger: ConsoleLogging {
}
}

public func handle(error: Error) {
debug(error.localizedDescription)
flypaper0 marked this conversation as resolved.
Show resolved Hide resolved
}
}


#if DEBUG
public struct ConsoleLoggerMock: ConsoleLogging {
public var logsPublisher: AnyPublisher<WalletConnectUtils.Log, Never> {
Expand All @@ -121,6 +123,8 @@ public struct ConsoleLoggerMock: ConsoleLogging {
public func error(_ items: Any..., file: String, function: String, line: Int) { }

public func setLogging(level: LoggingLevel) { }

public func handle(error: Error) { }
}
#endif

5 changes: 5 additions & 0 deletions Sources/WalletConnectUtils/Logger/ErrorHandler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation

public protocol ErrorHandler {
func handle(error: Error)
}
10 changes: 6 additions & 4 deletions Tests/TestingUtils/NetworkingInteractorMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,16 @@ public class NetworkingInteractorMock: NetworkInteracting {
public func subscribeOnRequest<RequestParams: Codable>(
protocolMethod: ProtocolMethod,
requestOfType: RequestParams.Type,
errorHandler: ErrorHandler?,
subscription: @escaping (RequestSubscriptionPayload<RequestParams>) async throws -> Void
) {
requestSubscription(on: protocolMethod)
.sink { [unowned self] (payload: RequestSubscriptionPayload<RequestParams>) in
.sink { (payload: RequestSubscriptionPayload<RequestParams>) in
Task(priority: .high) {
do {
try await subscription(payload)
} catch {
errorPublisherSubject.send(error)
errorHandler?.handle(error: error)
}
}
}.store(in: &publishers)
Expand All @@ -118,15 +119,16 @@ public class NetworkingInteractorMock: NetworkInteracting {
protocolMethod: ProtocolMethod,
requestOfType: Request.Type,
responseOfType: Response.Type,
errorHandler: ErrorHandler?,
subscription: @escaping (ResponseSubscriptionPayload<Request, Response>) async throws -> Void
) {
responseSubscription(on: protocolMethod)
.sink { [unowned self] (payload: ResponseSubscriptionPayload<Request, Response>) in
.sink { (payload: ResponseSubscriptionPayload<Request, Response>) in
Task(priority: .high) {
do {
try await subscription(payload)
} catch {
errorPublisherSubject.send(error)
errorHandler?.handle(error: error)
}
}
}.store(in: &publishers)
Expand Down