Skip to content

Commit 76abed5

Browse files
committed
error handling
1 parent 2b673b8 commit 76abed5

File tree

1 file changed

+41
-12
lines changed

1 file changed

+41
-12
lines changed

Coder Desktop/VPNLib/FileSync/FileSyncDaemon.swift

+41-12
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@ import os
66
@MainActor
77
public protocol FileSyncDaemon: ObservableObject {
88
var state: DaemonState { get }
9-
func start() async throws(DaemonError)
10-
func stop() async throws(DaemonError)
9+
func start() async
10+
func stop() async
1111
}
1212

1313
@MainActor
1414
public class MutagenDaemon: FileSyncDaemon {
1515
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "mutagen")
1616

17-
@Published public var state: DaemonState = .stopped
17+
@Published public var state: DaemonState = .stopped {
18+
didSet {
19+
logger.info("daemon state changed: \(self.state.description)")
20+
}
21+
}
1822

1923
private var mutagenProcess: Process?
2024
private var mutagenPipe: Pipe?
@@ -47,26 +51,24 @@ public class MutagenDaemon: FileSyncDaemon {
4751
}
4852
}
4953

50-
public func start() async throws(DaemonError) {
54+
public func start() async {
5155
if case .unavailable = state { return }
5256

5357
// Stop an orphaned daemon, if there is one
5458
try? await connect()
55-
try? await stop()
59+
await stop()
5660

5761
(mutagenProcess, mutagenPipe) = createMutagenProcess()
5862
do {
5963
try mutagenProcess?.run()
6064
} catch {
61-
state = .failed("Failed to start file sync daemon: \(error)")
62-
throw DaemonError.daemonStartFailure(error)
65+
state = .failed(DaemonError.daemonStartFailure(error))
6366
}
6467

6568
do {
6669
try await connect()
6770
} catch {
68-
state = .failed("failed to connect to file sync daemon: \(error)")
69-
throw DaemonError.daemonStartFailure(error)
71+
state = .failed(DaemonError.daemonStartFailure(error))
7072
}
7173

7274
state = .running
@@ -105,7 +107,7 @@ public class MutagenDaemon: FileSyncDaemon {
105107
group = nil
106108
}
107109

108-
public func stop() async throws(DaemonError) {
110+
public func stop() async {
109111
if case .unavailable = state { return }
110112
state = .stopped
111113
guard FileManager.default.fileExists(atPath: mutagenDaemonSocket.path) else {
@@ -155,7 +157,7 @@ public class MutagenDaemon: FileSyncDaemon {
155157
return
156158
default:
157159
logger.error("mutagen daemon exited unexpectedly")
158-
self.state = .failed("File sync daemon terminated unexpectedly")
160+
self.state = .failed(.terminatedUnexpectedly)
159161
}
160162
}
161163
}
@@ -170,11 +172,38 @@ public class MutagenDaemon: FileSyncDaemon {
170172
public enum DaemonState {
171173
case running
172174
case stopped
173-
case failed(String)
175+
case failed(DaemonError)
174176
case unavailable
177+
178+
var description: String {
179+
switch self {
180+
case .running:
181+
"Running"
182+
case .stopped:
183+
"Stopped"
184+
case let .failed(error):
185+
"Failed: \(error)"
186+
case .unavailable:
187+
"Unavailable"
188+
}
189+
}
175190
}
176191

177192
public enum DaemonError: Error {
178193
case daemonStartFailure(Error)
179194
case connectionFailure(Error)
195+
case terminatedUnexpectedly
196+
197+
var description: String {
198+
switch self {
199+
case let .daemonStartFailure(error):
200+
"Daemon start failure: \(error)"
201+
case let .connectionFailure(error):
202+
"Connection failure: \(error)"
203+
case .terminatedUnexpectedly:
204+
"Daemon terminated unexpectedly"
205+
}
206+
}
207+
208+
var localizedDescription: String { description }
180209
}

0 commit comments

Comments
 (0)