Skip to content

Commit

Permalink
fix(MailApiFetcher): Let Alamofire handle 401 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeWeidmann committed Jun 14, 2023
1 parent 2fd6045 commit 33fed0c
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions MailCore/API/MailApiFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int> = {
var allStatus = Set(200 ... 500)
allStatus.remove(401)
return allStatus
}()

override public func perform<T: Decodable>(
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) {
Expand Down

0 comments on commit 33fed0c

Please sign in to comment.