More granular websocket error handling

This commit is contained in:
2025-06-20 14:55:55 -07:00
parent 751261ffc4
commit 82b5c886cb
3 changed files with 20 additions and 5 deletions

View File

@@ -162,8 +162,7 @@ struct API
websocketTask.sendPing { error in
if let error {
print("Ping error: \(error). Trying to reconnect.")
continuation.yield(.error(.websocketError(error)))
notifyError(error, continuation: continuation)
websocketTask = spawnWebsocketTask(with: continuation)
} else {
continuation.yield(.event(Event(type: .receivedWebsocketPong)))
@@ -204,14 +203,28 @@ struct API
}
}
} catch {
print("Websocket Error: \(error)")
continuation.yield(.error(API.Error.websocketError(error)))
notifyError(error, continuation: continuation)
}
}
return websocketTask
}
private func notifyError(_ error: any Swift.Error, continuation: AsyncStream<StreamEvent>.Continuation) {
print("Websocket Error: \(error)")
var shouldNotifyObservers = true
let nsError = error as NSError
if nsError.code == 53 {
// This is a "connection abort", caused by backgrounding.
// Don't notify UI, just silently reconnect.
shouldNotifyObservers = false
}
if shouldNotifyObservers {
continuation.yield(.error(.websocketError(error)))
}
}
private func request() -> RequestBuilder {
RequestBuilder(url: baseURL)
}
@@ -247,6 +260,7 @@ struct API
// Private UI events
case receivedWebsocketPong
case websocketReconnected
}
}
}