Better error handling

This commit is contained in:
Leon Böttger committed 2025-08-09 19:56:55 +02:00
1 parent a0c6770c51
commit aff2cc18d4
1 file changed
+40 -13
+40 -13
View File
@@ -10,7 +10,6 @@ import CryptoKit
import Foundation import Foundation
import Network import Network
import System import System
import BigInt import BigInt
import SwiftECC import SwiftECC
@@ -76,12 +75,10 @@ class NearbyConnection {
// If connection reset by peer, it could still be a valid file transfer that has not been processed yet. => Wait // If connection reset by peer, it could still be a valid file transfer that has not been processed yet. => Wait
if err == .posix(.ECONNRESET) { if err == .posix(.ECONNRESET) {
NearbyConnection.dispatchQueue.asyncAfter(deadline: .now() + 0.5) {
log("[NearbyConnection] Network connection reset error detected.")
// If already closed, ignore the error, as file transfer has been processed self.connectionClosedByPeer {
if !self.connectionClosed { recordErrorAndDisconnect(err: err)
recordErrorAndDisconnect(err: err)
}
} }
} }
else { else {
@@ -102,12 +99,14 @@ class NearbyConnection {
// If the error is a connection reset, it could be due to firewall issues. // If the error is a connection reset, it could be due to firewall issues.
if err == .posix(.ENOTCONN) { if err == .posix(.ENOTCONN) {
log("[NearbyConnection] Network not connected error detected.")
ConnectionFailureTracker.shared.recordFailure { ConnectionFailureTracker.shared.recordFailure {
NearbyConnectionManager.shared.mainAppDelegate?.showFirewallAlert() NearbyConnectionManager.shared.mainAppDelegate?.showFirewallAlert()
} }
} }
if err == .posix(.ENETDOWN) { if err == .posix(.ENETDOWN) {
log("[NearbyConnection] Network down error detected.")
self.lastError = NearbyError.protocolError("Error.NetworkDown".localized()) self.lastError = NearbyError.protocolError("Error.NetworkDown".localized())
} }
@@ -116,6 +115,17 @@ class NearbyConnection {
} }
func connectionClosedByPeer(onError: @escaping () -> Void) {
NearbyConnection.dispatchQueue.asyncAfter(deadline: .now() + 0.5) {
// If already closed, ignore the error, as file transfer has been processed
if !self.connectionClosed {
onError()
}
}
}
func connectionReady() {} func connectionReady() {}
@@ -156,17 +166,21 @@ class NearbyConnection {
return return
} }
if isComplete { if isComplete {
log("[NearbyConnection] Connection closed by peer during receiveFrameAsync") log("[NearbyConnection] Connection closed by peer during receiveFrameAsync()")
self.lastError = NearbyError.protocolError("Error.ClosedByPeer".localized()) self.connectionClosedByPeer {
self.disconnect() self.lastError = NearbyError.protocolError("Error.ClosedByPeer".localized())
self.disconnect()
}
return return
} }
if !(error == nil) { if !(error == nil) {
log("[NearbyConnection] Error during receiveFrameAsync(): \(String(describing: error))")
self.lastError = error self.lastError = error
self.protocolError() self.protocolError()
return return
} }
guard let content = content else { guard let content = content else {
log("[NearbyConnection] Received nil content during receiveFrameAsync(). IsComplete: \(isComplete)")
assertionFailure() assertionFailure()
return return
} }
@@ -189,12 +203,25 @@ class NearbyConnection {
return return
} }
if isComplete { if isComplete {
log("[NearbyConnection] Connection closed by peer during receiveFrameAsync") log("[NearbyConnection] Connection closed by peer during receiveFrameAsync(length:)")
self.lastError = NearbyError.protocolError("Error.ClosedByPeer".localized())
self.disconnect() if let content = content, !content.isEmpty {
log("[NearbyConnection] Connection closed by peer during receiveFrameAsync(length:), but trying to process the frame anyway. Frame: \(content.hex)")
self.processReceivedFrame(frameData: content)
}
else {
log("[NearbyConnection] Connection closed by peer during receiveFrameAsync(length:), but no content received anymore (\(String(describing: content))).")
}
self.connectionClosedByPeer {
self.lastError = NearbyError.protocolError("Error.ClosedByPeer".localized())
self.disconnect()
}
return return
} }
guard let content = content else { guard let content = content else {
log("[NearbyConnection] Received nil content during receiveFrameAsync(length:). IsComplete: \(isComplete)")
self.protocolError() self.protocolError()
return return
} }