Skip to content

Commit 0726cc5

Browse files
fix: matching errors in the retry strategy (#817)
1 parent 98f97e9 commit 0726cc5

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed

Sources/AlgoliaSearchClient/Transport/RetryStrategy/AlgoliaRetryStrategy.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ class AlgoliaRetryStrategy: RetryStrategy {
7575

7676
func isRetryable(_ error: Error) -> Bool {
7777
switch error {
78-
case URLRequest.FormatError.badHost:
78+
case .requestError(let error) as TransportError where error is URLError:
7979
return true
8080

81-
case is URLError:
81+
case .httpError(let httpError) as TransportError where !httpError.statusCode.belongs(to: .success, .clientError):
8282
return true
8383

84-
case let httpError as HTTPError where !httpError.statusCode.belongs(to: .success, .clientError):
84+
case .badHost as URLRequest.FormatError:
8585
return true
8686

8787
default:
@@ -91,18 +91,18 @@ class AlgoliaRetryStrategy: RetryStrategy {
9191

9292
func isTimeout(_ error: Error) -> Bool {
9393
switch error {
94-
case let urlError as URLError where urlError.code == .timedOut:
94+
case .requestError(let error as URLError) as TransportError where error.code == .timedOut:
9595
return true
9696

97-
case let httpError as HTTPError where httpError.statusCode == .requestTimeout:
97+
case .httpError(let error) as TransportError where error.statusCode == .requestTimeout:
9898
return true
9999

100100
default:
101101
return false
102102
}
103103
}
104104

105-
func canRetry(inCaseOf error: Error) -> Bool {
105+
func canRetry<E: Error>(inCaseOf error: E) -> Bool {
106106
return isTimeout(error) || isRetryable(error)
107107
}
108108

Tests/AlgoliaSearchClientTests/Integration/PersonalizationIntegrationTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class PersonalizationIntegrationTests: IntegrationTestCase {
4141

4242
do {
4343
try personalizationClient.setPersonalizationStrategy(strategy)
44-
} catch let httpError as HTTPError where httpError.statusCode == HTTPStatusСode.tooManyRequests {
44+
} catch .httpError(let httpError) as TransportError where httpError.statusCode == HTTPStatusСode.tooManyRequests {
4545
// The personalization API is now limiting the number of setPersonalizationStrategy()` successful calls
4646
// to 15 per day. If the 429 error is returned, the response is considered a "success".
4747
} catch let error {

Tests/AlgoliaSearchClientTests/Unit/Transport/AlgoliaRetryStrategyTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class AlgoliaRetryStrategyTests: XCTestCase {
1818
let host5 = RetryableHost(url: URL(string: "algolia5.com")!, callType: .read)
1919

2020
let successResult = Result<String, Error>.success("success")
21-
let retryableErrorResult = Result<String, Error>.failure(URLError(.networkConnectionLost))
22-
let timeoutErrorResult = Result<String, Error>.failure(URLError(.timedOut))
21+
let retryableErrorResult = Result<String, Error>.failure(TransportError.requestError(URLError(.networkConnectionLost)))
22+
let timeoutErrorResult = Result<String, Error>.failure(TransportError.requestError(URLError(.timedOut)))
2323

2424
func testHostsRotation() {
2525

Tests/AlgoliaSearchClientTests/Unit/Transport/HTTPRequestTests.swift

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ class HTTPRequestTests: XCTestCase {
2727
count += 1
2828
switch count {
2929
case 1:
30-
completion(.failure(URLError(.timedOut)))
30+
completion(.failure(TransportError.requestError(URLError(.timedOut))))
3131
case 2:
32-
completion(.failure(HTTPError(statusCode: 503, message: nil)))
32+
completion(.failure(TransportError.httpError(HTTPError(statusCode: 503, message: nil))))
3333
case 3:
34-
completion(.failure(URLError(.cannotLoadFromNetwork)))
34+
completion(.failure(TransportError.requestError(URLError(.cannotLoadFromNetwork))))
3535
case 4:
36-
completion(.failure(URLError(.backgroundSessionWasDisconnected)))
36+
completion(.failure(TransportError.requestError(URLError(.backgroundSessionWasDisconnected))))
3737
default:
3838
break
3939
}
@@ -56,10 +56,20 @@ class HTTPRequestTests: XCTestCase {
5656
callType: .read,
5757
timeout: 10) { (result: Result<String, Error>) in
5858
if case .failure(TransportError.noReachableHosts(intermediateErrors: let errors)) = result {
59-
XCTAssertEqual(errors[0] as! URLError, URLError(.timedOut))
60-
XCTAssertEqual((errors[1] as! HTTPError).statusCode, 503)
61-
XCTAssertEqual(errors[2] as! URLError, URLError(.cannotLoadFromNetwork))
62-
XCTAssertEqual(errors[3] as! URLError, URLError(.backgroundSessionWasDisconnected))
59+
for (index, error) in errors.enumerated() {
60+
switch (index, error) {
61+
case (0, TransportError.requestError(URLError.timedOut)):
62+
break
63+
case (1, TransportError.httpError(let httpError)) where httpError.statusCode == 503:
64+
break
65+
case (2, TransportError.requestError(URLError.cannotLoadFromNetwork)):
66+
break
67+
case (3, TransportError.requestError(URLError.backgroundSessionWasDisconnected)):
68+
break
69+
default:
70+
XCTFail("Unexpected error at index \(index): \(error.localizedDescription)")
71+
}
72+
}
6373
} else {
6474
XCTFail("Unexpected success result")
6575
}

0 commit comments

Comments
 (0)