Don't ignore GNCCoreAdapter completion handler errors in Swift APIs.

PiperOrigin-RevId: 508760708
This commit is contained in:
hai007
2023-02-10 14:53:35 -08:00
committed by Copybara-Service
parent 68739bab45
commit b204aed4cb
6 changed files with 125 additions and 44 deletions
@@ -44,21 +44,26 @@ public class Advertiser {
/// delegates `advertiser(_:didReceiveConnectionRequestFrom:with:connectionRequestHandler:)`
/// method when remote endpoints request a connection.
///
/// - Parameter context: An arbitrary piece of data that is advertised to the nearby endpoint.
/// - Parameters:
/// - context: An arbitrary piece of data that is advertised to the nearby endpoint.
/// This can be used to provide further information to the user about the nature of the
/// advertisement.
public func startAdvertising(using context: Data) {
/// - completionHandler: Called with `nil` if advertising started, or an error if advertising
/// failed to start.
///
public func startAdvertising(using context: Data, completionHandler: ((Error?) -> Void)? = nil) {
let options = GNCAdvertisingOptions(strategy: connectionManager.strategy.objc)
// TODO(b/257824412): Handle errors from completion handler.
GNCCoreAdapter.shared.startAdvertising(
asService: connectionManager.serviceID, endpointInfo: context,
options: options, delegate: connection)
options: options, delegate: connection, withCompletionHandler: completionHandler)
}
/// Stops advertising the local endpoint.
public func stopAdvertising() {
// TODO(b/257824412): Handle errors from completion handler.
GNCCoreAdapter.shared.stopAdvertising()
///
/// - Parameter completionHandler: Called with `nil` if advertising stopped, or an error if
/// advertising failed to stop.
public func stopAdvertising(completionHandler: ((Error?) -> Void)? = nil) {
GNCCoreAdapter.shared.stopAdvertising(completionHandler: completionHandler)
}
deinit {
@@ -76,8 +81,16 @@ extension Advertiser: InternalConnectionDelegate {
self.delegate?.advertiser(self, didReceiveConnectionRequestFrom: endpointID, with: info) {
(accept) in
guard accept else {
// TODO(b/257824412): Handle errors from completion handler.
GNCCoreAdapter.shared.rejectConnectionRequest(fromEndpoint: endpointID)
GNCCoreAdapter.shared.rejectConnectionRequest(fromEndpoint: endpointID) {
(error: Error?) in
if let error = error {
NSLog(
"""
Encountered an error while attempting to reject a connection request for \
endpoint %@: %@
""", endpointID, "\(error)")
}
}
return
}
self.connectionManager.delegate?.connectionManager(
@@ -86,13 +99,29 @@ extension Advertiser: InternalConnectionDelegate {
self.connectionManager, didReceive: authenticationToken, from: endpointID
) { accept in
guard accept else {
// TODO(b/257824412): Handle errors from completion handler.
GNCCoreAdapter.shared.rejectConnectionRequest(fromEndpoint: endpointID)
GNCCoreAdapter.shared.rejectConnectionRequest(fromEndpoint: endpointID) {
(error: Error?) in
if let error = error {
NSLog(
"""
Encountered an error while attempting to reject a connection request for \
endpoint %@: %@
""", endpointID, "\(error)")
}
}
return
}
// TODO(b/257824412): Handle errors from completion handler.
GNCCoreAdapter.shared.acceptConnectionRequest(
fromEndpoint: endpointID, delegate: self.connectionManager.payload)
fromEndpoint: endpointID, delegate: self.connectionManager.payload
) { (error: Error?) in
if let error = error {
NSLog(
"""
Encountered an error while attempting to accept a connection request for \
endpoint %@: %@
""", endpointID, "\(error)")
}
}
}
}
@@ -23,8 +23,10 @@ public class CancellationToken {
}
/// Cancel the ongoing transfer.
public func cancel() {
// TODO(b/257824412): Handle errors from completion handler.
GNCCoreAdapter.shared.cancelPayload(payloadID)
///
/// - Parameter completionHandler: Called with `nil` if the transfer was cancelled, or an error
/// if an error occurred.
public func cancel(completionHandler: ((Error?) -> Void)? = nil) {
GNCCoreAdapter.shared.cancelPayload(payloadID, withCompletionHandler: completionHandler)
}
}
@@ -189,20 +189,29 @@ public class ConnectionManager {
transfers[payload.identifier]?[endpointID] = Progress()
}
GNCCoreAdapter.shared.send(payload, toEndpoints: endpointIDs) { _ in
// TODO(b/257824412): Handle errors from completion handler.
completionHandler?(nil)
}
GNCCoreAdapter.shared.send(
payload,
toEndpoints: endpointIDs,
withCompletionHandler: completionHandler
)
return CancellationToken(payload.identifier)
}
/// Disconnect from a remote endpoint.
///
/// - Parameter endpointID: The ID of the remote endpoint.
public func disconnect(from endpointID: EndpointID) {
// TODO(b/257824412): Handle errors from completion handler.
GNCCoreAdapter.shared.disconnect(fromEndpoint: endpointID)
/// - Parameters:
/// - endpointID: The ID of the remote endpoint.
/// - completionHandler: Called with `nil` when the endpoint has been disconnected,
/// or an error if disconnecting failed
public func disconnect(
from endpointID: EndpointID,
completionHandler: ((Error?) -> Void)? = nil
) {
GNCCoreAdapter.shared.disconnect(
fromEndpoint: endpointID,
withCompletionHandler: completionHandler
)
}
}
@@ -251,10 +260,17 @@ extension ConnectionManager: InternalPayloadDelegate {
case .success:
self.transfers[payloadID]?.removeValue(forKey: endpointID)
self.delegate?.connectionManager(
self, didReceiveTransferUpdate: .success, from: endpointID, forPayload: payloadID)
self, didReceiveTransferUpdate: .success, from: endpointID, forPayload: payloadID
)
case .failure:
self.transfers[payloadID]?.removeValue(forKey: endpointID)
// TODO(b/257824412): Handle errors from completion handler.
self.delegate?.connectionManager(
self,
didReceiveTransferUpdate: .failure,
from: endpointID,
forPayload: payloadID
)
break
case .canceled:
self.transfers[payloadID]?.removeValue(forKey: endpointID)
@@ -48,17 +48,23 @@ public class Discoverer {
/// After this method is called (until you call `stopDiscovery()`), the framework calls your
/// delegates `discoverer(_:didFind:with:)` and `discoverer(_:didLose:)` methods as new endpoints
/// are found and lost.
public func startDiscovery() {
///
/// - Parameter completionHandler: Called with `nil` if discovery starts, or an error if
/// discovery failed to start.
public func startDiscovery(completionHandler: ((Error?) -> Void)? = nil) {
let options = GNCDiscoveryOptions(strategy: connectionManager.strategy.objc)
// TODO(b/257824412): Handle errors from completion handler.
GNCCoreAdapter.shared.startDiscovery(
asService: connectionManager.serviceID, options: options, delegate: discovery)
asService: connectionManager.serviceID, options: options, delegate: discovery,
withCompletionHandler: completionHandler
)
}
/// Stops searching for nearby remote endpoints.
public func stopDiscovery() {
// TODO(b/257824412): Handle errors from completion handler.
GNCCoreAdapter.shared.stopDiscovery()
///
/// - Parameter completionHandler: Called with `nil` if discovery stopped, or an error if
/// discovery failed to stop.
public func stopDiscovery(completionHandler: ((Error?) -> Void)? = nil) {
GNCCoreAdapter.shared.stopDiscovery(completionHandler: completionHandler)
}
/// Requests a connection to a discovered remote endpoint.
@@ -67,12 +73,15 @@ public class Discoverer {
/// - endpointID: The ID of the endpoint to request a connection to.
/// - context: An arbitrary piece of data that is passed to the nearby endpoint. This can be
/// used to provide further information to the user about the nature of the invitation.
public func requestConnection(to endpointID: EndpointID, using context: Data) {
/// - completionHandler: Called with `nil` when the endpoint has been disconnected,
/// or an error if disconnecting failed
public func requestConnection(
to endpointID: EndpointID, using context: Data, completionHandler: ((Error?) -> Void)? = nil
) {
let options = GNCConnectionOptions()
// TODO(b/257824412): Handle errors from completion handler.
GNCCoreAdapter.shared.requestConnection(
toEndpoint: endpointID, endpointInfo: context,
options: options, delegate: connection)
options: options, delegate: connection, withCompletionHandler: completionHandler)
}
deinit {
@@ -93,13 +102,30 @@ extension Discoverer: InternalConnectionDelegate {
connectionManager, didReceive: authenticationToken, from: endpointID
) { accept in
guard accept else {
// TODO(b/257824412): Handle errors from completion handler.
GNCCoreAdapter.shared.rejectConnectionRequest(fromEndpoint: endpointID)
GNCCoreAdapter.shared.rejectConnectionRequest(fromEndpoint: endpointID) {
(error: Error?) in
if let error = error {
NSLog(
"""
Encountered an error while attempting to reject a connection request for \
endpoint %@: %@
""", endpointID, "\(error)")
}
}
return
}
// TODO(b/257824412): Handle errors from completion handler.
GNCCoreAdapter.shared.acceptConnectionRequest(
fromEndpoint: endpointID, delegate: connectionManager.payload)
fromEndpoint: endpointID,
delegate: connectionManager.payload
) { (error: Error?) in
if let error = error {
NSLog(
"""
Encountered an error while attempting to accept a connection request for \
endpoint %@: %@
""", endpointID, "\(error)")
}
}
}
}
}
@@ -18,10 +18,15 @@ class InternalConnection: GNCConnectionDelegate {
weak var delegate: InternalConnectionDelegate?
func connected(
toEndpoint endpointID: String, withEndpointInfo info: Data, authenticationToken: String
toEndpoint endpointID: String,
withEndpointInfo info: Data,
authenticationToken: String
) {
delegate?.connected(
toEndpoint: endpointID, withEndpointInfo: info, authenticationToken: authenticationToken)
toEndpoint: endpointID,
withEndpointInfo: info,
authenticationToken: authenticationToken
)
}
func acceptedConnection(toEndpoint endpointID: String) {
@@ -39,7 +44,10 @@ class InternalConnection: GNCConnectionDelegate {
protocol InternalConnectionDelegate: AnyObject {
func connected(
toEndpoint endpointID: String, withEndpointInfo info: Data, authenticationToken: String)
toEndpoint endpointID: String,
withEndpointInfo info: Data,
authenticationToken: String
)
func acceptedConnection(toEndpoint endpointID: String)
func rejectedConnection(toEndpoint endpointID: String, with status: GNCStatus)
func disconnected(fromEndpoint endpointID: String)
@@ -20,8 +20,8 @@ public enum TransferUpdate {
case success
/// Either the local or remote endpoint has canceled the transfer.
case canceled
/// The remote endpoint failed to receive the transfer with the associated error.
case failure(Error)
/// The remote endpoint failed to receive the transfer.
case failure
/// The the transfer is currently in progress with an associated progress value.
case progress(Progress)
}