mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-28 08:17:16 +00:00
139 lines
5.1 KiB
Swift
139 lines
5.1 KiB
Swift
//
|
||
// AccountError.swift
|
||
// NetNewsWire
|
||
//
|
||
// Created by Maurice Parker on 5/26/19.
|
||
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
||
//
|
||
|
||
import Foundation
|
||
import RSWeb
|
||
|
||
public enum AccountError: LocalizedError {
|
||
|
||
case createErrorNotFound
|
||
case createErrorAlreadySubscribed
|
||
case opmlImportInProgress
|
||
case invalidParameter
|
||
case invalidResponse
|
||
case urlNotFound
|
||
case unknown
|
||
case wrappedError(error: Error, accountID: String, accountName: String, accountType: AccountType)
|
||
|
||
public var isCredentialsError: Bool {
|
||
if case .wrappedError(let error, _, _, _) = self {
|
||
if case WebserviceError.httpError(let status, _) = error {
|
||
return isCredentialsError(status: status)
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
@MainActor static func wrapped(_ error: Error, _ account: Account) -> AccountError {
|
||
AccountError.wrappedError(error: error, accountID: account.accountID, accountName: account.nameForDisplay, accountType: account.type)
|
||
}
|
||
|
||
@MainActor public static func account(from error: AccountError?) -> Account? {
|
||
if case let .wrappedError(_, accountID, _, _) = error {
|
||
return AccountManager.shared.existingAccount(accountID: accountID)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
public var errorDescription: String? {
|
||
switch self {
|
||
case .createErrorNotFound:
|
||
return NSLocalizedString("The feed couldn’t be found and can’t be added.", comment: "Not found")
|
||
case .createErrorAlreadySubscribed:
|
||
return NSLocalizedString("You are already subscribed to this feed and can’t add it again.", comment: "Already subscribed")
|
||
case .opmlImportInProgress:
|
||
return NSLocalizedString("An OPML import for this account is already running.", comment: "Import running")
|
||
case .invalidParameter:
|
||
return NSLocalizedString("Couldn’t fulfill the request due to an invalid parameter.", comment: "Invalid parameter")
|
||
case .invalidResponse:
|
||
return NSLocalizedString("There was an invalid response from the server.", comment: "Invalid response")
|
||
case .urlNotFound:
|
||
return NSLocalizedString("The URL request resulted in a not found error.", comment: "URL not found")
|
||
case .unknown:
|
||
return NSLocalizedString("Unknown error", comment: "Unknown error")
|
||
case .wrappedError(let error, _, let accountName, let accountType):
|
||
switch error {
|
||
case WebserviceError.httpError(let status, _):
|
||
if isCredentialsError(status: status) {
|
||
let localizedText = NSLocalizedString("Your “%@” credentials are invalid or expired.", comment: "Invalid or expired")
|
||
return NSString.localizedStringWithFormat(localizedText as NSString, accountName) as String
|
||
} else {
|
||
return syncError(error, accountName, accountType)
|
||
}
|
||
default:
|
||
return syncError(error, accountName, accountType)
|
||
}
|
||
}
|
||
}
|
||
|
||
public var recoverySuggestion: String? {
|
||
switch self {
|
||
case .createErrorNotFound:
|
||
return nil
|
||
case .createErrorAlreadySubscribed:
|
||
return nil
|
||
case .wrappedError(let error, _, _, _):
|
||
switch error {
|
||
case WebserviceError.httpError(let status, _):
|
||
if isCredentialsError(status: status) {
|
||
return NSLocalizedString("Please update your credentials for this account, or ensure that your account with this service is still valid.", comment: "Expired credentials")
|
||
} else {
|
||
return NSLocalizedString("Please try again later.", comment: "Try later")
|
||
}
|
||
default:
|
||
return NSLocalizedString("Please try again later.", comment: "Try later")
|
||
}
|
||
default:
|
||
return NSLocalizedString("Please try again later.", comment: "Try later")
|
||
}
|
||
}
|
||
}
|
||
|
||
// MARK: - Detailed Error Message
|
||
|
||
extension AccountError {
|
||
|
||
/// Returns a description that includes the error domain, code, and any underlying error.
|
||
static func detailedErrorMessage(_ error: Error) -> String {
|
||
// A parser diagnostic wouldn't help anybody — this usually means the
|
||
// service is down and returning an error page.
|
||
if error is DecodingError {
|
||
return NSLocalizedString("The service returned an invalid response.", comment: "Invalid response from service")
|
||
}
|
||
|
||
let nsError = error as NSError
|
||
var message = error.localizedDescription
|
||
if !nsError.domain.isEmpty {
|
||
message += " (\(nsError.domain) \(nsError.code))"
|
||
}
|
||
if let underlyingError = nsError.userInfo[NSUnderlyingErrorKey] as? Error {
|
||
let underlyingNSError = underlyingError as NSError
|
||
message += " -- underlying: \(underlyingError.localizedDescription) (\(underlyingNSError.domain) \(underlyingNSError.code))"
|
||
}
|
||
if case WebserviceError.httpError(_, let responseBody) = error, let responseBody {
|
||
message += " -- server response: \(responseBody)"
|
||
}
|
||
return message
|
||
}
|
||
}
|
||
|
||
// MARK: - Private
|
||
|
||
private extension AccountError {
|
||
|
||
func syncError(_ error: Error, _ accountName: String, _ accountType: AccountType) -> String {
|
||
let localizedText = NSLocalizedString("An error occurred while syncing the %1$@ account “%2$@”: %3$@", comment: "Account sync error — account type, account name, error details")
|
||
return NSString.localizedStringWithFormat(localizedText as NSString, accountType.displayName, accountName, AccountError.detailedErrorMessage(error)) as String
|
||
}
|
||
|
||
func isCredentialsError(status: Int) -> Bool {
|
||
return status == 401 || status == 403
|
||
}
|
||
|
||
}
|