Skip to content

Commit

Permalink
Merge pull request #3 from SwiftPackageIndex/change-retry-error
Browse files Browse the repository at this point in the history
Change retry error
  • Loading branch information
finestructure authored Jul 21, 2024
2 parents 88179ba + 44b5fcd commit 4fba683
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
17 changes: 11 additions & 6 deletions Sources/Retry/Retry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import Foundation


public enum Retry {
public enum Error: Swift.Error, Equatable {
case retryLimitExceeded(lastError: String?)
public enum Error: Swift.Error {
case abort(with: Swift.Error)
case retryLimitExceeded(lastError: Swift.Error? = nil)
}

public static func backedOffDelay(baseDelay: Double, attempt: Int) -> UInt32 {
Expand All @@ -32,16 +33,18 @@ public enum Retry {
_ block: () throws -> T) throws -> T {
var retriesLeft = retries
var currentTry = 1
var lastError: String?
var lastError: Swift.Error?
while true {
if currentTry > 1 {
logger.onStartOfRetry(label: label, attempt: currentTry)
}
do {
return try block()
} catch let Error.abort(with: error) {
throw Error.abort(with: error)
} catch {
logger.onError(label: label, error: error)
lastError = "\(error)"
lastError = error
guard retriesLeft > 0 else { break }
let delay = backedOffDelay(baseDelay: delay, attempt: currentTry)
logger.onStartOfDelay(label: label, delay: Double(delay))
Expand All @@ -61,16 +64,18 @@ public enum Retry {
_ block: () async throws -> T) async throws -> T {
var retriesLeft = retries
var currentTry = 1
var lastError: String?
var lastError: Swift.Error?
while true {
if currentTry > 1 {
logger.onStartOfRetry(label: label, attempt: currentTry)
}
do {
return try await block()
} catch let Error.abort(with: error) {
throw Error.abort(with: error)
} catch {
logger.onError(label: label, error: error)
lastError = "\(error)"
lastError = error
guard retriesLeft > 0 else { break }
let delay = backedOffDelay(baseDelay: delay, attempt: currentTry)
logger.onStartOfDelay(label: label, delay: Double(delay))
Expand Down
33 changes: 29 additions & 4 deletions Tests/RetryTests/RetryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,45 @@ final class RetryTests: XCTestCase {
}

// MUT
XCTAssertThrowsError(
do {
try Retry.attempt("", delay: 0, retries: 3) {
called += 1
throw Error()
}

) { error in
XCTAssertEqual(error as? Retry.Error, .retryLimitExceeded(lastError: "test error"))
XCTFail("expected an error to be thrown")
} catch let Retry.Error.retryLimitExceeded(lastError: .some(error)) {
XCTAssertEqual("\(error)", "test error")
} catch {
XCTFail("unexpected error: \(error)")
}

// validation
XCTAssertEqual(called, 4)
}

func test_attempt_abort() throws {
var called = 0
struct Error: Swift.Error, CustomStringConvertible {
var description: String { "test error" }
}

// MUT
do {
try Retry.attempt("", delay: 0, retries: 3) {
called += 1
throw Retry.Error.abort(with: Error())
}
XCTFail("expected an error to be thrown")
} catch let Retry.Error.abort(with: error) {
XCTAssertEqual("\(error)", "test error")
} catch {
XCTFail("unexpected error: \(error)")
}

// validation
XCTAssertEqual(called, 1)
}

func test_attempt_async() async throws {
func dummyAsyncFunction() async { }
var called = 0
Expand Down

0 comments on commit 4fba683

Please sign in to comment.