Skip to content

Commit d4ea316

Browse files
committed
refactoring-function-to-throw
1 parent 879eb58 commit d4ea316

File tree

3 files changed

+32
-32
lines changed

3 files changed

+32
-32
lines changed

Sources/Swiftly/Install.swift

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -350,22 +350,26 @@ struct Install: SwiftlyCommand {
350350

351351
lastUpdate = Date()
352352

353-
await animation.update(
354-
step: progress.receivedBytes,
355-
total: progress.totalBytes!,
356-
text:
357-
"Downloaded \(String(format: "%.1f", downloadedMiB)) MiB of \(String(format: "%.1f", totalMiB)) MiB"
358-
)
353+
do {
354+
try await animation.update(
355+
step: progress.receivedBytes,
356+
total: progress.totalBytes!,
357+
text:
358+
"Downloaded \(String(format: "%.1f", downloadedMiB)) MiB of \(String(format: "%.1f", totalMiB)) MiB"
359+
)
360+
} catch {
361+
// Silently ignore progress update errors to avoid interrupting download
362+
}
359363
}
360364
)
361365
} catch let notFound as DownloadNotFoundError {
362366
throw SwiftlyError(
363367
message: "\(version) does not exist at URL \(notFound.url), exiting")
364368
} catch {
365-
await animation.complete(success: false)
369+
try? await animation.complete(success: false)
366370
throw error
367371
}
368-
await animation.complete(success: true)
372+
try await animation.complete(success: true)
369373

370374
if verifySignature {
371375
try await Swiftly.currentPlatform.verifyToolchainSignature(

Sources/Swiftly/ProgressReporter.swift

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import TSCUtility
66

77
public protocol ProgressReporterProtocol {
88
/// Updates the progress animation with the current step, total steps, and an optional text message.
9-
func update(step: Int, total: Int, text: String) async
9+
func update(step: Int, total: Int, text: String) async throws
1010

1111
/// Completes the progress animation, indicating success or failure.
12-
func complete(success: Bool) async
12+
func complete(success: Bool) async throws
1313

1414
/// Closes any resources used by the reporter, if applicable.
1515
func close() throws
@@ -23,15 +23,15 @@ struct ConsoleProgressReporter: ProgressReporterProtocol {
2323
self.reporter = PercentProgressAnimation(stream: stream, header: header)
2424
}
2525

26-
func update(step: Int, total: Int, text: String) async {
26+
func update(step: Int, total: Int, text: String) async throws {
2727
self.reporter.update(step: step, total: total, text: text)
2828
}
2929

30-
func complete(success: Bool) async {
30+
func complete(success: Bool) async throws {
3131
self.reporter.complete(success: success)
3232
}
3333

34-
func close() {
34+
func close() throws {
3535
// No resources to close for console reporter
3636
}
3737
}
@@ -55,23 +55,19 @@ struct JsonFileProgressReporter: ProgressReporterProtocol {
5555
self.fileHandle = try FileHandle(forWritingTo: URL(fileURLWithPath: filePath.string))
5656
}
5757

58-
private func writeProgress(_ progress: ProgressInfo) async {
59-
let jsonData = try? self.encoder.encode(progress)
60-
guard let jsonData = jsonData else {
61-
await self.ctx.message("Failed to encode progress entry to JSON")
62-
return
63-
}
58+
private func writeProgress(_ progress: ProgressInfo) async throws {
59+
let jsonData = try self.encoder.encode(progress)
6460

6561
self.fileHandle.write(jsonData)
6662
self.fileHandle.write("\n".data(using: .utf8) ?? Data())
67-
try? self.fileHandle.synchronize()
63+
try self.fileHandle.synchronize()
6864
}
6965

70-
func update(step: Int, total: Int, text: String) async {
66+
func update(step: Int, total: Int, text: String) async throws {
7167
guard total > 0 && step <= total else {
7268
return
7369
}
74-
await self.writeProgress(
70+
try await self.writeProgress(
7571
ProgressInfo.step(
7672
timestamp: Date(),
7773
percent: Int(Double(step) / Double(total) * 100),
@@ -80,8 +76,8 @@ struct JsonFileProgressReporter: ProgressReporterProtocol {
8076
)
8177
}
8278

83-
func complete(success: Bool) async {
84-
await self.writeProgress(ProgressInfo.complete(success: success))
79+
func complete(success: Bool) async throws {
80+
try await self.writeProgress(ProgressInfo.complete(success: success))
8581
}
8682

8783
func close() throws {

Tests/SwiftlyTests/JsonFileProgressReporterTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Testing
1313
defer { try? FileManager.default.removeItem(atPath: tempFile.string) }
1414
let reporter = try JsonFileProgressReporter(SwiftlyTests.ctx, filePath: tempFile)
1515

16-
await reporter.update(step: 1, total: 10, text: "Processing item 1")
16+
try await reporter.update(step: 1, total: 10, text: "Processing item 1")
1717
try reporter.close()
1818

1919
let decoder = JSONDecoder()
@@ -49,7 +49,7 @@ import Testing
4949
let reporter = try JsonFileProgressReporter(SwiftlyTests.ctx, filePath: tempFile)
5050

5151
let status = Bool.random()
52-
await reporter.complete(success: status)
52+
try await reporter.complete(success: status)
5353
try reporter.close()
5454

5555
let decoder = JSONDecoder()
@@ -78,7 +78,7 @@ import Testing
7878
defer { try? FileManager.default.removeItem(atPath: tempFile.string) }
7979
let reporter = try JsonFileProgressReporter(SwiftlyTests.ctx, filePath: tempFile)
8080

81-
await reporter.update(step: 25, total: 100, text: "Quarter way")
81+
try await reporter.update(step: 25, total: 100, text: "Quarter way")
8282
try reporter.close()
8383

8484
let decoder = JSONDecoder()
@@ -108,12 +108,12 @@ import Testing
108108

109109
let reporter = try JsonFileProgressReporter(SwiftlyTests.ctx, filePath: tempFile)
110110

111-
await reporter.update(step: 5, total: 100, text: "Processing item 5")
112-
await reporter.update(step: 10, total: 100, text: "Processing item 10")
113-
await reporter.update(step: 50, total: 100, text: "Processing item 50")
114-
await reporter.update(step: 100, total: 100, text: "Processing item 100")
111+
try await reporter.update(step: 5, total: 100, text: "Processing item 5")
112+
try await reporter.update(step: 10, total: 100, text: "Processing item 10")
113+
try await reporter.update(step: 50, total: 100, text: "Processing item 50")
114+
try await reporter.update(step: 100, total: 100, text: "Processing item 100")
115115

116-
await reporter.complete(success: true)
116+
try await reporter.complete(success: true)
117117
try? reporter.close()
118118

119119
let decoder = JSONDecoder()

0 commit comments

Comments
 (0)