mirror of
https://codeberg.org/secana/Forji.git
synced 2026-07-04 01:35:46 -07:00
20 lines
597 B
Swift
20 lines
597 B
Swift
import Foundation
|
|
|
|
func withRetry<T>(
|
|
maxAttempts: Int, delay: Duration,
|
|
operation: String,
|
|
body: () async throws -> T,
|
|
) async throws -> T {
|
|
var lastError: any Error = SeedError.retryExhausted(operation: operation, attempts: maxAttempts)
|
|
for attempt in 1 ... maxAttempts {
|
|
do {
|
|
return try await body()
|
|
} catch {
|
|
lastError = error
|
|
if attempt == maxAttempts { break }
|
|
print(" \(operation) attempt \(attempt) failed, retrying...")
|
|
try await Task.sleep(for: delay)
|
|
}
|
|
}
|
|
throw lastError
|
|
}
|