refactor(apple): tidy up error handling around Session.start (#5968)

Whilst investigating #5965, I noticed that the code around
`Adapter.start` on the Swift side can be tidied up a bit to prevent
duplicate logs.
This commit is contained in:
Thomas Eizinger
2024-07-23 16:22:50 +10:00
committed by GitHub
parent c94f9edb27
commit 86549378c9
2 changed files with 9 additions and 14 deletions

View File

@@ -106,14 +106,10 @@ class Adapter {
}
/// Start the tunnel.
/// - Parameters:
/// - completionHandler: completion handler.
public func start(completionHandler: @escaping (AdapterError?) -> Void) throws {
public func start() throws {
Log.tunnel.log("Adapter.start")
guard case .tunnelStopped = self.state else {
Log.tunnel.error("\(#function): Invalid Adapter state")
completionHandler(.invalidState)
return
throw AdapterError.invalidState
}
callbackHandler.delegate = self
@@ -136,20 +132,15 @@ class Adapter {
logFilter,
callbackHandler
)
// Update our internal state
self.state = .tunnelStarted(session: session)
// Start listening for network change events. The first few will be our
// tunnel interface coming up, but that's ok -- it will trigger a `set_dns`
// connlib.
beginPathMonitoring()
// Tell the system the tunnel is up, moving the tunnelManager status to
// `connected`.
completionHandler(nil)
// Update state in case everything succeeded
self.state = .tunnelStarted(session: session)
} catch let error {
Log.tunnel.error("\(#function): Adapter.start: Error: \(error)")
state = .tunnelStopped
throw AdapterError.connlibConnectError(error)
}
}

View File

@@ -82,7 +82,11 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
apiURL: apiURL, token: token, logFilter: logFilter, packetTunnelProvider: self)
self.adapter = adapter
try adapter.start(completionHandler: completionHandler)
try adapter.start()
// Tell the system the tunnel is up, moving the tunnelManager status to
// `connected`.
completionHandler(nil)
} catch {
Log.tunnel.error("\(#function): Error! \(error)")
completionHandler(error)