2019-05-26 18:54:32 +02:00
//
// A c c o u n t E r r o r . s w i f t
2019-07-09 07:58:19 +02:00
// N e t N e w s W i r e
2019-05-26 18:54:32 +02:00
//
// C r e a t e d b y M a u r i c e P a r k e r o n 5 / 2 6 / 1 9 .
// C o p y r i g h t © 2 0 1 9 R a n c h e r o S o f t w a r e , L L C . A l l r i g h t s r e s e r v e d .
//
import Foundation
import RSWeb
public enum AccountError : LocalizedError {
case createErrorNotFound
case createErrorAlreadySubscribed
case opmlImportInProgress
case wrappedError ( error : Error , account : Account )
2020-01-16 21:50:30 +01:00
public var acount : Account ? {
if case . wrappedError ( _ , let account ) = self {
return account
} else {
return nil
}
}
public var isCredentialsError : Bool {
if case . wrappedError ( let error , _ ) = self {
if case TransportError . httpError ( let status ) = error {
return isCredentialsError ( status : status )
}
}
return false
}
2019-05-26 18:54:32 +02:00
public var errorDescription : String ? {
switch self {
2019-06-01 00:24:43 +02:00
case . createErrorNotFound :
2019-07-09 07:59:29 +02:00
return NSLocalizedString ( " The feed couldn’ t be found and can’ t be added. " , comment : " Not found " )
2019-06-01 00:24:43 +02:00
case . createErrorAlreadySubscribed :
2019-07-09 07:59:29 +02:00
return NSLocalizedString ( " You are already subscribed to this feed and can’ t add it again. " , comment : " Already subscribed " )
2019-05-26 18:54:32 +02:00
case . opmlImportInProgress :
return NSLocalizedString ( " An OPML import for this account is already running. " , comment : " Import running " )
case . wrappedError ( let error , let account ) :
switch error {
case TransportError . httpError ( let status ) :
2020-01-16 21:50:30 +01:00
if isCredentialsError ( status : status ) {
2019-10-22 12:58:05 +02:00
let localizedText = NSLocalizedString ( " Your “%@” credentials are invalid or expired. " , comment : " Invalid or expired " )
2019-05-26 18:54:32 +02:00
return NSString . localizedStringWithFormat ( localizedText as NSString , account . nameForDisplay ) as String
} else {
return unknownError ( error , account )
}
default :
return unknownError ( error , account )
}
}
}
public var recoverySuggestion : String ? {
switch self {
2019-06-01 00:24:43 +02:00
case . createErrorNotFound :
return nil
case . createErrorAlreadySubscribed :
return nil
2019-05-26 18:54:32 +02:00
case . wrappedError ( let error , _ ) :
switch error {
case TransportError . httpError ( let status ) :
2020-01-16 21:50:30 +01:00
if isCredentialsError ( status : status ) {
2019-08-28 17:40:12 +02:00
return NSLocalizedString ( " Please update your credentials for this account, or ensure that your account with this service is still valid. " , comment : " Expired credentials " )
2019-05-26 18:54:32 +02:00
} 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 " )
}
}
2020-01-16 21:50:30 +01:00
}
// MARK: P r i v a t e
private extension AccountError {
func unknownError ( _ error : Error , _ account : Account ) -> String {
2019-10-22 12:58:05 +02:00
let localizedText = NSLocalizedString ( " An error occurred while processing the “%@” account: %@ " , comment : " Unknown error " )
2019-05-26 18:54:32 +02:00
return NSString . localizedStringWithFormat ( localizedText as NSString , account . nameForDisplay , error . localizedDescription ) as String
}
2020-01-16 21:50:30 +01:00
func isCredentialsError ( status : Int ) -> Bool {
return status = = 401 || status = = 403
}
2019-05-26 18:54:32 +02:00
}