Skip to content

Commit

Permalink
fix: matching errors in the retry strategy (#817)
Browse files Browse the repository at this point in the history
  • Loading branch information
VladislavFitz committed Jun 22, 2023
1 parent 98f97e9 commit 0726cc5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
Expand Up @@ -75,13 +75,13 @@ class AlgoliaRetryStrategy: RetryStrategy {

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

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

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

default:
Expand All @@ -91,18 +91,18 @@ class AlgoliaRetryStrategy: RetryStrategy {

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

case let httpError as HTTPError where httpError.statusCode == .requestTimeout:
case .httpError(let error) as TransportError where error.statusCode == .requestTimeout:
return true

default:
return false
}
}

func canRetry(inCaseOf error: Error) -> Bool {
func canRetry<E: Error>(inCaseOf error: E) -> Bool {
return isTimeout(error) || isRetryable(error)
}

Expand Down
Expand Up @@ -41,7 +41,7 @@ class PersonalizationIntegrationTests: IntegrationTestCase {

do {
try personalizationClient.setPersonalizationStrategy(strategy)
} catch let httpError as HTTPError where httpError.statusCode == HTTPStatusСode.tooManyRequests {
} catch .httpError(let httpError) as TransportError where httpError.statusCode == HTTPStatusСode.tooManyRequests {
// The personalization API is now limiting the number of setPersonalizationStrategy()` successful calls
// to 15 per day. If the 429 error is returned, the response is considered a "success".
} catch let error {
Expand Down
Expand Up @@ -18,8 +18,8 @@ class AlgoliaRetryStrategyTests: XCTestCase {
let host5 = RetryableHost(url: URL(string: "algolia5.com")!, callType: .read)

let successResult = Result<String, Error>.success("success")
let retryableErrorResult = Result<String, Error>.failure(URLError(.networkConnectionLost))
let timeoutErrorResult = Result<String, Error>.failure(URLError(.timedOut))
let retryableErrorResult = Result<String, Error>.failure(TransportError.requestError(URLError(.networkConnectionLost)))
let timeoutErrorResult = Result<String, Error>.failure(TransportError.requestError(URLError(.timedOut)))

func testHostsRotation() {

Expand Down
Expand Up @@ -27,13 +27,13 @@ class HTTPRequestTests: XCTestCase {
count += 1
switch count {
case 1:
completion(.failure(URLError(.timedOut)))
completion(.failure(TransportError.requestError(URLError(.timedOut))))
case 2:
completion(.failure(HTTPError(statusCode: 503, message: nil)))
completion(.failure(TransportError.httpError(HTTPError(statusCode: 503, message: nil))))
case 3:
completion(.failure(URLError(.cannotLoadFromNetwork)))
completion(.failure(TransportError.requestError(URLError(.cannotLoadFromNetwork))))
case 4:
completion(.failure(URLError(.backgroundSessionWasDisconnected)))
completion(.failure(TransportError.requestError(URLError(.backgroundSessionWasDisconnected))))
default:
break
}
Expand All @@ -56,10 +56,20 @@ class HTTPRequestTests: XCTestCase {
callType: .read,
timeout: 10) { (result: Result<String, Error>) in
if case .failure(TransportError.noReachableHosts(intermediateErrors: let errors)) = result {
XCTAssertEqual(errors[0] as! URLError, URLError(.timedOut))
XCTAssertEqual((errors[1] as! HTTPError).statusCode, 503)
XCTAssertEqual(errors[2] as! URLError, URLError(.cannotLoadFromNetwork))
XCTAssertEqual(errors[3] as! URLError, URLError(.backgroundSessionWasDisconnected))
for (index, error) in errors.enumerated() {
switch (index, error) {
case (0, TransportError.requestError(URLError.timedOut)):
break
case (1, TransportError.httpError(let httpError)) where httpError.statusCode == 503:
break
case (2, TransportError.requestError(URLError.cannotLoadFromNetwork)):
break
case (3, TransportError.requestError(URLError.backgroundSessionWasDisconnected)):
break
default:
XCTFail("Unexpected error at index \(index): \(error.localizedDescription)")
}
}
} else {
XCTFail("Unexpected success result")
}
Expand Down

0 comments on commit 0726cc5

Please sign in to comment.