Skip to content

Commit c5e178c

Browse files
committed
Simplify error handling for RequestProcessor
1 parent eb41391 commit c5e178c

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

Modules/Sources/NetworkingCore/ApplicationPassword/RequestProcessor.swift

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ final class RequestProcessor: RequestInterceptor {
1616

1717
private let notificationCenter: NotificationCenter
1818

19-
private var isAuthenticatedWithWPCom = false
20-
2119
private var currentSiteID: Int64?
2220

2321
weak var delegate: RequestProcessorDelegate?
@@ -30,12 +28,6 @@ final class RequestProcessor: RequestInterceptor {
3028

3129
func updateAuthenticator(_ authenticator: RequestAuthenticator) {
3230
requestAuthenticator = authenticator
33-
isAuthenticatedWithWPCom = {
34-
switch authenticator.credentials {
35-
case .some(.wpcom): true
36-
default: false
37-
}
38-
}()
3931
currentSiteID = authenticator.jetpackSiteID
4032
}
4133
}
@@ -88,8 +80,9 @@ private extension RequestProcessor {
8880
// Post a notification for tracking
8981
notificationCenter.post(name: .ApplicationPasswordsGenerationFailed, object: error, userInfo: nil)
9082

91-
if isAuthenticatedWithWPCom, let currentSiteID {
92-
await retryPasswordGenerationIfNeeded(error: error, siteID: currentSiteID)
83+
let shouldRetry = await checkIfRetryingGenerationIsNeeded(error: error)
84+
if shouldRetry {
85+
generateApplicationPassword()
9386
} else {
9487
isAuthenticating = false
9588
completeRequests(false)
@@ -98,26 +91,28 @@ private extension RequestProcessor {
9891
}
9992
}
10093

94+
/// Checks error code to retry or mark site as unsupported for app password.
95+
/// Returns whether retry is needed.
10196
@MainActor
102-
func retryPasswordGenerationIfNeeded(error: Error, siteID: Int64) async {
103-
defer {
104-
isAuthenticating = false
97+
func checkIfRetryingGenerationIsNeeded(error: Error) async -> Bool {
98+
guard let currentSiteID else {
99+
return false
105100
}
106101
switch error {
107-
case AFError.responseValidationFailed(reason: .unacceptableStatusCode(code: 409)):
102+
case NetworkError.unacceptableStatusCode(let statusCode, _) where statusCode == 409:
108103
/// Password with the same name already exists. Request deletion remotely and retry.
109104
do {
110105
try await requestAuthenticator.deleteApplicationPassword()
111-
generateApplicationPassword()
106+
return true
112107
} catch {
113-
completeRequests(false)
108+
return false
114109
}
115-
case AFError.responseValidationFailed(reason: .unacceptableStatusCode(code: 404)):
110+
case NetworkError.notFound:
116111
/// Site doesn't support application password
117-
completeRequests(false)
118-
delegate?.didFailToAuthenticateRequestWithApplicationPassword(siteID: siteID)
112+
delegate?.didFailToAuthenticateRequestWithApplicationPassword(siteID: currentSiteID)
113+
return false
119114
default:
120-
completeRequests(false)
115+
return false
121116
}
122117
}
123118

Modules/Tests/NetworkingTests/ApplicationPassword/RequestProcessorTests.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,14 @@ final class RequestProcessorTests: XCTestCase {
301301
let session = Alamofire.Session(configuration: URLSessionConfiguration.default)
302302
let request = try mockRequest()
303303
let wpcomCredentials = Networking.Credentials.wpcom(username: "test", authToken: "token", siteAddress: "test.com")
304+
let mockDelegate = MockRequestProcessorDelegate()
305+
304306
mockRequestAuthenticator.credentials = wpcomCredentials
305307
mockRequestAuthenticator.jetpackSiteID = 123
306-
let mockDelegate = MockRequestProcessorDelegate()
307308
sut.updateAuthenticator(mockRequestAuthenticator)
308309
sut.delegate = mockDelegate
309310

310-
let notFound404Error = AFError.responseValidationFailed(reason: .unacceptableStatusCode(code: 404))
311+
let notFound404Error = NetworkError.notFound(response: nil)
311312
mockRequestAuthenticator.mockErrorWhileGeneratingPassword = notFound404Error
312313

313314
// When
@@ -330,7 +331,9 @@ final class RequestProcessorTests: XCTestCase {
330331
let session = Alamofire.Session(configuration: URLSessionConfiguration.default)
331332
let request = try mockRequest()
332333
let wpcomCredentials = Networking.Credentials.wpcom(username: "test", authToken: "token", siteAddress: "test.com")
334+
333335
mockRequestAuthenticator.credentials = wpcomCredentials
336+
mockRequestAuthenticator.jetpackSiteID = 123
334337
sut.updateAuthenticator(mockRequestAuthenticator)
335338

336339
let genericError = NSError(domain: "TestError", code: 500, userInfo: nil)
@@ -354,10 +357,12 @@ final class RequestProcessorTests: XCTestCase {
354357
let session = Alamofire.Session(configuration: URLSessionConfiguration.default)
355358
let request = try mockRequest()
356359
let wpcomCredentials = Networking.Credentials.wpcom(username: "test", authToken: "token", siteAddress: "test.com")
360+
357361
mockRequestAuthenticator.credentials = wpcomCredentials
362+
mockRequestAuthenticator.jetpackSiteID = 123
358363
sut.updateAuthenticator(mockRequestAuthenticator)
359364

360-
let conflict409Error = AFError.responseValidationFailed(reason: .unacceptableStatusCode(code: 409))
365+
let conflict409Error = NetworkError.unacceptableStatusCode(statusCode: 409, response: nil)
361366
let deletionError = NSError(domain: "TestError", code: 500, userInfo: nil)
362367
mockRequestAuthenticator.mockErrorWhileGeneratingPassword = conflict409Error
363368
mockRequestAuthenticator.mockErrorWhileDeletingPassword = deletionError

WooCommerce/WooCommerceTests/Yosemite/StoresManagerTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ final class StoresManagerTests: XCTestCase {
437437

438438
/// Verifies that user is logged out when application password regeneration fails if authenticated with wporg
439439
///
440+
@MainActor
440441
func test_it_deauthenticates_upon_receiving_application_password_generation_failure_notification_when_authenticated_without_wpcom() {
441442
// Given
442443
let manager = DefaultStoresManager.testingInstance
@@ -445,15 +446,14 @@ final class StoresManagerTests: XCTestCase {
445446
isLoggedInValues.append(isLoggedIn)
446447
}
447448
manager.authenticate(credentials: SessionSettings.wporgCredentials)
449+
XCTAssertTrue(manager.isAuthenticated)
448450

449451
// When
450452
let error = ApplicationPasswordUseCaseError.unauthorizedRequest
451453
MockNotificationCenter.testingInstance.post(name: .ApplicationPasswordsGenerationFailed, object: error, userInfo: nil)
452454

453455
// Assert
454-
waitUntil {
455-
manager.isAuthenticated == false
456-
}
456+
XCTAssertFalse(manager.isAuthenticated)
457457
XCTAssertEqual(isLoggedInValues, [false, true, false])
458458
}
459459

0 commit comments

Comments
 (0)