From 33fed0c1b7e24fb224222fce43c78c6dcbd6fa5a Mon Sep 17 00:00:00 2001 From: Philippe Weidmann Date: Wed, 14 Jun 2023 13:19:54 +0200 Subject: [PATCH 1/2] fix(MailApiFetcher): Let Alamofire handle 401 errors --- MailCore/API/MailApiFetcher.swift | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/MailCore/API/MailApiFetcher.swift b/MailCore/API/MailApiFetcher.swift index 4f3078e15..78c4f4f05 100644 --- a/MailCore/API/MailApiFetcher.swift +++ b/MailCore/API/MailApiFetcher.swift @@ -36,20 +36,27 @@ public extension ApiFetcher { public class MailApiFetcher: ApiFetcher { public static let clientId = "E90BC22D-67A8-452C-BE93-28DA33588CA4" + /// All status except 401 are handled by our code, 401 status is handled by Alamofire's Authenticator code + private lazy var handledHttpStatus: Set = { + var allStatus = Set(200 ... 500) + allStatus.remove(401) + return allStatus + }() + override public func perform( request: DataRequest, decoder: JSONDecoder = ApiFetcher.decoder ) async throws -> (data: T, responseAt: Int?) { do { - return try await super.perform(request: request) - } catch let InfomaniakError.apiError(apiError) { + return try await super.perform(request: request.validate(statusCode: handledHttpStatus)) + } catch InfomaniakError.apiError(let apiError) { throw MailApiError.mailApiErrorWithFallback(apiErrorCode: apiError.code) - } catch let InfomaniakError.serverError(statusCode: statusCode) { + } catch InfomaniakError.serverError(statusCode: let statusCode) { throw MailServerError(httpStatus: statusCode) } catch { if let afError = error.asAFError { - if case let .responseSerializationFailed(reason) = afError, - case let .decodingFailed(error) = reason { + if case .responseSerializationFailed(let reason) = afError, + case .decodingFailed(let error) = reason { var rawJson = "No data" if let data = request.data, let stringData = String(data: data, encoding: .utf8) { From 4a741b2c6b9bf1fe64bb0ddb646de5b12212d9c4 Mon Sep 17 00:00:00 2001 From: Philippe Weidmann Date: Wed, 14 Jun 2023 14:58:38 +0200 Subject: [PATCH 2/2] fix(SceneDelegate): Add AccountManagerDelegate --- Mail/SceneDelegate.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Mail/SceneDelegate.swift b/Mail/SceneDelegate.swift index e522a28e7..110d2ef8e 100644 --- a/Mail/SceneDelegate.swift +++ b/Mail/SceneDelegate.swift @@ -37,6 +37,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate, AccountManagerDelegate // `application:configurationForConnectingSceneSession` instead). guard let _ = (scene as? UIWindowScene) else { return } accountManager = AccountManager.instance + accountManager.delegate = self updateWindowUI() setupLaunch() if let mailToURL = connectionOptions.urlContexts.first?.url { @@ -107,7 +108,9 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate, AccountManagerDelegate } func currentAccountNeedsAuthentication() { - showLoginView() + DispatchQueue.main.async { [weak self] in + self?.showLoginView() + } } private func setupLaunch() {