Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement error handling in RetryPolicyService #5

Merged
merged 2 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.
## [Unreleased]

## Added
- Implement error handling in RetryPolicyService
- Added in Pull Request [#5](https://github.com/space-code/typhoon/pull/5).
- Implement exponential backoff with jitter
- Added in Pull Request [#4](https://github.com/space-code/typhoon/pull/4).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ public protocol IRetryPolicyService {
///
/// - Parameters:
/// - strategy: The strategy defining the behavior of the retry policy.
/// - onFailure: An optional closure called on each failure to handle or log errors.
/// - closure: The closure that will be retried based on the specified strategy.
///
/// - Returns: The result of the closure's execution after retrying based on the policy.
func retry<T>(strategy: RetryPolicyStrategy?, _ closure: () async throws -> T) async throws -> T
func retry<T>(
strategy: RetryPolicyStrategy?,
onFailure: ((Error) async -> Void)?,
_ closure: () async throws -> T
) async throws -> T
}

public extension IRetryPolicyService {
Expand All @@ -27,6 +32,17 @@ public extension IRetryPolicyService {
///
/// - Returns: The result of the closure's execution after retrying based on the policy.
func retry<T>(_ closure: () async throws -> T) async throws -> T {
try await retry(strategy: nil, closure)
try await retry(strategy: nil, onFailure: nil, closure)
}

/// Retries a closure with a given strategy.
///
/// - Parameters:
/// - strategy: The strategy defining the behavior of the retry policy.
/// - closure: The closure that will be retried based on the specified strategy.
///
/// - Returns: The result of the closure's execution after retrying based on the policy.
func retry<T>(strategy: RetryPolicyStrategy?, _ closure: () async throws -> T) async throws -> T {
try await retry(strategy: strategy, onFailure: nil, closure)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@ public final class RetryPolicyService {
// MARK: IRetryPolicyService

extension RetryPolicyService: IRetryPolicyService {
public func retry<T>(strategy: RetryPolicyStrategy?, _ closure: () async throws -> T) async throws -> T {
public func retry<T>(
strategy: RetryPolicyStrategy?,
onFailure: ((Error) async -> Void)?,
_ closure: () async throws -> T
) async throws -> T {
for duration in RetrySequence(strategy: strategy ?? self.strategy) {
try Task.checkCancellation()

do {
return try await closure()
} catch {}
} catch {
await onFailure?(error)
}

try await Task.sleep(nanoseconds: duration)
}
Expand Down
16 changes: 16 additions & 0 deletions Tests/TyphoonTests/UnitTests/RetryPolicyServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ final class RetryPolicyServiceTests: XCTestCase {
// then
XCTAssertEqual(counter, .retry)
}

func test_thatRetryServiceHandlesErrorOnFailureCallback_whenErrorOcurred() async {
// when
var failureError: NSError?
do {
_ = try await sut.retry(
strategy: .constant(retry: .retry, duration: .nanoseconds(1)),
onFailure: { error in failureError = error as NSError }
) {
throw URLError(.unknown)
}
} catch {}

// then
XCTAssertEqual(failureError as? URLError, URLError(.unknown))
}
}

// MARK: - Constants
Expand Down
Loading