Source code for the WriteFreely SwiftUI app for iOS, iPadOS, and macOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

174 regels
6.2 KiB

  1. import Foundation
  2. // MARK: - Network Errors
  3. enum NetworkError: Error {
  4. case noConnectionError
  5. }
  6. extension NetworkError: LocalizedError {
  7. public var errorDescription: String? {
  8. switch self {
  9. case .noConnectionError:
  10. return NSLocalizedString(
  11. "There is no internet connection at the moment. Please reconnect or try again later.",
  12. comment: ""
  13. )
  14. }
  15. }
  16. }
  17. // MARK: - Keychain Errors
  18. enum KeychainError: Error {
  19. case couldNotStoreAccessToken
  20. case couldNotPurgeAccessToken
  21. case couldNotFetchAccessToken
  22. }
  23. extension KeychainError: LocalizedError {
  24. public var errorDescription: String? {
  25. switch self {
  26. case .couldNotStoreAccessToken:
  27. return NSLocalizedString("There was a problem storing your access token in the Keychain.", comment: "")
  28. case .couldNotPurgeAccessToken:
  29. return NSLocalizedString("Something went wrong purging the token from the Keychain.", comment: "")
  30. case .couldNotFetchAccessToken:
  31. return NSLocalizedString("Something went wrong fetching the token from the Keychain.", comment: "")
  32. }
  33. }
  34. }
  35. // MARK: - Account Errors
  36. enum AccountError: Error {
  37. case invalidPassword
  38. case usernameNotFound
  39. case serverNotFound
  40. case invalidServerURL
  41. case unknownLoginError
  42. case genericAuthError
  43. }
  44. extension AccountError: LocalizedError {
  45. public var errorDescription: String? {
  46. switch self {
  47. case .serverNotFound:
  48. return NSLocalizedString(
  49. "The server could not be found. Please check the information you've entered and try again.",
  50. comment: ""
  51. )
  52. case .invalidPassword:
  53. return NSLocalizedString(
  54. "Invalid password. Please check that you've entered your password correctly and try logging in again.",
  55. comment: ""
  56. )
  57. case .usernameNotFound:
  58. return NSLocalizedString(
  59. "Username not found. Did you use your email address by mistake?",
  60. comment: ""
  61. )
  62. case .invalidServerURL:
  63. return NSLocalizedString(
  64. "Please enter a valid instance domain name. It should look like \"https://example.com\" or \"write.as\".", // swiftlint:disable:this line_length
  65. comment: ""
  66. )
  67. case .genericAuthError:
  68. return NSLocalizedString("Something went wrong, please try logging in again.", comment: "")
  69. case .unknownLoginError:
  70. return NSLocalizedString("An unknown error occurred while trying to login.", comment: "")
  71. }
  72. }
  73. }
  74. // MARK: - User Defaults Errors
  75. enum UserDefaultsError: Error {
  76. case couldNotMigrateStandardDefaults
  77. }
  78. extension UserDefaultsError: LocalizedError {
  79. public var errorDescription: String? {
  80. switch self {
  81. case .couldNotMigrateStandardDefaults:
  82. return NSLocalizedString("Could not migrate user defaults to group container", comment: "")
  83. }
  84. }
  85. }
  86. // MARK: - Local Store Errors
  87. enum LocalStoreError: Error {
  88. case couldNotSaveContext
  89. case couldNotFetchCollections
  90. case couldNotFetchPosts(String = "")
  91. case couldNotPurgePosts(String = "")
  92. case couldNotPurgeCollections
  93. case couldNotLoadStore(String)
  94. case couldNotMigrateStore(String)
  95. case couldNotDeleteStoreAfterMigration(String)
  96. case genericError(String = "")
  97. }
  98. extension LocalStoreError: LocalizedError {
  99. public var errorDescription: String? {
  100. switch self {
  101. case .couldNotSaveContext:
  102. return NSLocalizedString("Error saving context", comment: "")
  103. case .couldNotFetchCollections:
  104. return NSLocalizedString("Failed to fetch blogs from local store.", comment: "")
  105. case .couldNotFetchPosts(let postFilter):
  106. if postFilter.isEmpty {
  107. return NSLocalizedString("Failed to fetch posts from local store.", comment: "")
  108. } else {
  109. return NSLocalizedString("Failed to fetch \(postFilter) posts from local store.", comment: "")
  110. }
  111. case .couldNotPurgePosts(let postFilter):
  112. if postFilter.isEmpty {
  113. return NSLocalizedString("Failed to purge \(postFilter) posts from local store.", comment: "")
  114. } else {
  115. return NSLocalizedString("Failed to purge posts from local store.", comment: "")
  116. }
  117. case .couldNotPurgeCollections:
  118. return NSLocalizedString("Failed to purge cached collections", comment: "")
  119. case .couldNotLoadStore(let errorDescription):
  120. return NSLocalizedString("Something went wrong loading local store: \(errorDescription)", comment: "")
  121. case .couldNotMigrateStore(let errorDescription):
  122. return NSLocalizedString("Something went wrong migrating local store: \(errorDescription)", comment: "")
  123. case .couldNotDeleteStoreAfterMigration(let errorDescription):
  124. return NSLocalizedString("Something went wrong deleting old store: \(errorDescription)", comment: "")
  125. case .genericError(let customContent):
  126. if customContent.isEmpty {
  127. return NSLocalizedString("Something went wrong accessing device storage", comment: "")
  128. } else {
  129. return NSLocalizedString(customContent, comment: "")
  130. }
  131. }
  132. }
  133. }
  134. // MARK: - Application Errors
  135. enum AppError: Error {
  136. case couldNotGetLoggedInClient
  137. case couldNotGetPostId
  138. case genericError(String = "")
  139. }
  140. extension AppError: LocalizedError {
  141. public var errorDescription: String? {
  142. switch self {
  143. case .couldNotGetLoggedInClient:
  144. return NSLocalizedString("Something went wrong trying to access the WriteFreely client.", comment: "")
  145. case .couldNotGetPostId:
  146. return NSLocalizedString("Something went wrong trying to get the post's unique ID.", comment: "")
  147. case .genericError(let customContent):
  148. if customContent.isEmpty {
  149. return NSLocalizedString("Something went wrong", comment: "")
  150. } else {
  151. return NSLocalizedString(customContent, comment: "")
  152. }
  153. }
  154. }
  155. }