Skip to content

Commit

Permalink
feat: Translate some API error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeWeidmann committed May 11, 2023
1 parent 4a97e71 commit 294144b
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 20 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
35 changes: 28 additions & 7 deletions MailCore/API/MailApiError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

import Foundation
import MailResources

class MailApiError: MailError {
static let allErrors: [MailApiError] = [
Expand All @@ -32,7 +33,11 @@ class MailApiError: MailError {
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"),
MailApiError(
code: "folder__destination_already_exists",
localizedDescription: MailResourcesStrings.Localizable.errorNewFolderAlreadyExists,
shouldDisplay: true
),
MailApiError(code: "folder__not_exists"),

// Mail
Expand All @@ -52,18 +57,34 @@ class MailApiError: MailError {
MailApiError(code: "draft__attachment_not_found"),
MailApiError(code: "draft__not_found"),
MailApiError(code: "draft__message_not_found"),
MailApiError(code: "draft__to_many_recipients"),
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"),
MailApiError(code: "draft__cannot_modify_scheduled_or_already_sent_message"),
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"),
MailApiError(code: "send__server_rate_limit_exceeded"),
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"),
Expand All @@ -89,6 +110,6 @@ class MailApiError: MailError {
}

static func mailApiErrorWithFallback(apiErrorCode: String) -> MailError {
return allErrors.first { $0.code == apiErrorCode } ?? MailError.unknownError
return mailApiErrorFromCode(apiErrorCode) ?? MailError.unknownError
}
}
30 changes: 18 additions & 12 deletions MailCore/Utils/Error+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,38 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import CocoaLumberjackSwift
import Foundation
import InfomaniakCoreUI

public func tryOrDisplayError(_ body: () throws -> Void) {
do {
try body()
} catch {
if error.shouldDisplay {
Task.detached {
await IKSnackBar.showSnackBar(message: error.localizedDescription)
}
}
print("Error: \(error)")
displayErrorIfNeeded(error: error)
}
}

public func tryOrDisplayError(_ body: () async throws -> Void) async {
do {
try await body()
} catch {
if error.shouldDisplay {
Task.detached {
await IKSnackBar.showSnackBar(message: error.localizedDescription)
}
displayErrorIfNeeded(error: error)
}
}

private func displayErrorIfNeeded(error: Error) {
if let error = error as? MailError,
error.shouldDisplay {
Task.detached {
await IKSnackBar.showSnackBar(message: error.errorDescription)
}
DDLogError("MailError: \(error)")
} else if error.shouldDisplay {
Task.detached {
await IKSnackBar.showSnackBar(message: error.localizedDescription)
}
print("Error: \(error)")
DDLogError("Error: \(error)")
}
}

Expand All @@ -50,7 +56,7 @@ public extension Error {
switch asAFError {
case .explicitlyCancelled:
return false
case let .sessionTaskFailed(error):
case .sessionTaskFailed(let error):
return (error as NSError).code != NSURLErrorNotConnectedToInternet
default:
return true
Expand Down
Binary file modified MailResources/Localizable/de.lproj/Localizable.strings
Binary file not shown.
Binary file modified MailResources/Localizable/en.lproj/Localizable.strings
Binary file not shown.
Binary file modified MailResources/Localizable/es.lproj/Localizable.strings
Binary file not shown.
Binary file modified MailResources/Localizable/fr.lproj/Localizable.strings
Binary file not shown.
Binary file modified MailResources/Localizable/it.lproj/Localizable.strings
Binary file not shown.

0 comments on commit 294144b

Please sign in to comment.