Skip to content

Commit

Permalink
Merge pull request #739 from Infomaniak/refactor-error
Browse files Browse the repository at this point in the history
feat: Improve error handling
  • Loading branch information
valentinperignon committed May 11, 2023
2 parents 3b3ee29 + 49265e9 commit 02a1e4a
Show file tree
Hide file tree
Showing 13 changed files with 196 additions and 250 deletions.
2 changes: 1 addition & 1 deletion Mail/Views/Bottom sheets/RestoreEmailsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ struct RestoreEmailsView: View {
Task {
await tryOrDisplayError {
try await mailboxManager.apiFetcher.restoreBackup(mailbox: mailboxManager.mailbox, date: selectedDate)
await IKSnackBar.showSnackBar(message: MailResourcesStrings.Localizable.snackbarSuccessfulRestoration)
IKSnackBar.showSnackBar(message: MailResourcesStrings.Localizable.snackbarRestorationLaunched)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Mail/Views/Onboarding/OnboardingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import AuthenticationServices
import InfomaniakCoreUI
import InfomaniakDI
import InfomaniakCreateAccount
import InfomaniakDI
import InfomaniakLogin
import Lottie
import MailCore
Expand Down Expand Up @@ -71,7 +71,7 @@ struct Slide: Identifiable {
class LoginHandler: InfomaniakLoginDelegate, ObservableObject {
@LazyInjectService var loginService: InfomaniakLoginable
@LazyInjectService var matomo: MatomoUtils

@Published var isLoading = false
@Published var isPresentingErrorAlert = false
var sceneDelegate: SceneDelegate?
Expand Down Expand Up @@ -129,7 +129,7 @@ class LoginHandler: InfomaniakLoginDelegate, ObservableObject {
_ = try await AccountManager.instance.createAndSetCurrentAccount(code: code, codeVerifier: verifier)
sceneDelegate?.showMainView()
UIApplication.shared.registerForRemoteNotifications()
} catch MailError.noMailbox {
} catch let error as MailError where error == MailError.noMailbox {
sceneDelegate?.showNoMailboxView()
} catch {
if let previousAccount = previousAccount {
Expand Down
2 changes: 1 addition & 1 deletion Mail/Views/SplitView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ struct SplitView: View {
if let tappedNotificationThread = tappedNotificationMessage?.originalThread {
navigationStore.threadPath = [tappedNotificationThread]
} else {
IKSnackBar.showSnackBar(message: MailError.messageNotFound.errorDescription ?? "")
IKSnackBar.showSnackBar(message: MailError.messageNotFound.errorDescription)
}
}
.onAppear {
Expand Down
187 changes: 0 additions & 187 deletions MailCore/API/ApiError.swift

This file was deleted.

117 changes: 117 additions & 0 deletions MailCore/API/MailApiError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
Infomaniak Mail - iOS App
Copyright (C) 2022 Infomaniak Network SA
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import Foundation
import MailResources

class MailApiError: MailError {
static let allErrors: [MailApiError] = [
// General
MailApiError(code: "not_authorized"),

// Folder
MailApiError(code: "folder__unable_to_create"),
MailApiError(code: "folder__unable_to_update"),
MailApiError(code: "folder__unable_to_delete"),
MailApiError(code: "folder__unable_to_flush"),
MailApiError(code: "folder__protected_folder"),
MailApiError(code: "folder__unable_to_move_folder_in_its_sub_folders"),
MailApiError(code: "folder__destination_folder_already_exists"),
MailApiError(code: "folder__root_destination_not_exists"),
MailApiError(
code: "folder__destination_already_exists",
localizedDescription: MailResourcesStrings.Localizable.errorNewFolderAlreadyExists,
shouldDisplay: true
),
MailApiError(code: "folder__not_exists",
localizedDescription: MailResourcesStrings.Localizable.errorFolderNotFound,
shouldDisplay: true),

// Mail
MailApiError(code: "mail__move_destination_folder_not_found"),
MailApiError(code: "mail__cannot_connect_to_server"),
MailApiError(code: "mail__imap_authentication_failed"),
MailApiError(code: "mail__imap_unable_to_parse_response"),
MailApiError(code: "mail__imap_connection_timedout"),
MailApiError(code: "mail__cannot_connect_to_smtp_server"),
MailApiError(code: "mail__smtp_authentication_failed"),
MailApiError(code: "mail__message_not_found"),
MailApiError(code: "mail__message_attachment_not_found"),
MailApiError(code: "mail__unable_to_undo_move_action"),
MailApiError(code: "mail__unable_to_move_emails"),

// Draft
MailApiError(code: "draft__attachment_not_found"),
MailApiError(code: "draft__not_found"),
MailApiError(code: "draft__message_not_found"),
MailApiError(
code: "draft__to_many_recipients",
localizedDescription: MailResourcesStrings.Localizable.tooManyRecipients,
shouldDisplay: true
),
MailApiError(code: "draft__max_attachments_size_reached"),
MailApiError(
code: "draft__need_at_least_one_recipient_to_be_sent",
localizedDescription: MailResourcesStrings.Localizable.errorAtLeastOneRecipient,
shouldDisplay: true
),
MailApiError(
code: "draft__cannot_modify_scheduled_or_already_sent_message",
localizedDescription: MailResourcesStrings.Localizable.errorEditScheduledMessage,
shouldDisplay: true
),
MailApiError(code: "draft__cannot_cancel_non_scheduled_message"),
MailApiError(code: "draft__cannot_forward_more_than_one_message_inline"),
MailApiError(code: "draft__cannot_move_scheduled_message"),

// Send
MailApiError(code: "send__server_refused_from"),
MailApiError(code: "send__server_refused_all_recipients",
localizedDescription: MailResourcesStrings.Localizable.errorRefusedRecipients,
shouldDisplay: true),
MailApiError(code: "send__server_rate_limit_exceeded",
localizedDescription: MailResourcesStrings.Localizable.errorSendLimitExceeded,
shouldDisplay: true),
MailApiError(code: "send__server_unknown_error"),
MailApiError(code: "send__server_daily_limit_reached"),
MailApiError(code: "send__spam_rejected"),
MailApiError(code: "send__sender_mismatch"),

// Attachment
MailApiError(code: "attachment__not_valid"),
MailApiError(code: "attachment__not_found"),
MailApiError(code: "attachment__cannot_render"),
MailApiError(code: "attachment__error_while_render"),
MailApiError(code: "attachment__missing_filename_or_mimetype"),
MailApiError(code: "attachment__incorrect_disposition"),
MailApiError(code: "attachment__content_id_not_valid"),
MailApiError(code: "attachment__add_attachment_from_drive_fail"),
MailApiError(code: "attachment__store_to_drive_fail"),

// Message
MailApiError(code: "message__uid_is_not_valid")
]

static func mailApiErrorFromCode(_ code: String) -> MailApiError? {
return allErrors.first { $0.code == code }
}

static func mailApiErrorWithFallback(apiErrorCode: String) -> MailError {
return mailApiErrorFromCode(apiErrorCode) ?? MailError.unknownError
}
}
4 changes: 2 additions & 2 deletions MailCore/API/MailApiFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public class MailApiFetcher: ApiFetcher {
do {
return try await super.perform(request: request)
} catch InfomaniakError.apiError(let apiError) {
throw MailError.apiError(apiError)
throw MailApiError.mailApiErrorWithFallback(apiErrorCode: apiError.code)
} catch InfomaniakError.serverError(statusCode: let statusCode) {
throw MailError.serverError(statusCode: statusCode)
throw MailServerError(httpStatus: statusCode)
} catch {
if let afError = error.asAFError {
if case .responseSerializationFailed(let reason) = afError,
Expand Down
Loading

0 comments on commit 02a1e4a

Please sign in to comment.