diff --git a/LeStorage/ApiCallCollection.swift b/LeStorage/ApiCallCollection.swift index 6d7adf8..b132a62 100644 --- a/LeStorage/ApiCallCollection.swift +++ b/LeStorage/ApiCallCollection.swift @@ -19,8 +19,15 @@ protocol SomeCallCollection { } -enum ApiCallError: Error { - case cantCreateCall +enum ApiCallError: Error, LocalizedError { + case encodingError(id: String, type: String) + + var errorDescription: String? { + switch self { + case .encodingError(let id, let type): + return "Can't encode instance \(type) with id: \(id)" + } + } } /// ApiCallCollection is an object communicating with a server to synchronize data managed locally @@ -236,7 +243,7 @@ actor ApiCallCollection: SomeCallCollection { return ApiCall(method: method, dataId: instance.stringId, body: jsonString) } catch { StoreCenter.main.log(message: "call could not be created for \(T.resourceName()): \(error.localizedDescription)") - throw ApiCallError.cantCreateCall + throw ApiCallError.encodingError(id: instance.stringId, type: T.resourceName()) } } diff --git a/LeStorage/Services.swift b/LeStorage/Services.swift index d6df69c..a52e38a 100644 --- a/LeStorage/Services.swift +++ b/LeStorage/Services.swift @@ -436,7 +436,7 @@ public class Services { /// - password: the account's password public func deleteAccount() async throws { guard let userId = StoreCenter.main.userId else { - throw ServiceError.missingUserId + throw StoreError.missingUserId } let path = "users/\(userId)/" let deleteAccount = ServiceCall(path: path, method: .delete, requiresToken: true) diff --git a/LeStorage/Store.swift b/LeStorage/Store.swift index 2bbca5b..f023778 100644 --- a/LeStorage/Store.swift +++ b/LeStorage/Store.swift @@ -8,13 +8,25 @@ import Foundation import UIKit -public enum StoreError: Error { +public enum StoreError: Error, LocalizedError { case missingService case missingUserId - case unexpectedCollectionType(name: String) - case apiCallCollectionNotRegistered(type: String) case collectionNotRegistered(type: String) case cannotSyncCollection(name: String) + + public var errorDescription: String? { + switch self { + case .missingService: + return "Services instance is nil" + case .missingUserId: + return "The user id is missing" + case .collectionNotRegistered(let type): + return "The collection \(type) is not registered" + case .cannotSyncCollection(let name): + return "Tries to load the collection \(name) from the server while it's not authorized" + } + } + } public struct StoreIdentifier { diff --git a/LeStorage/StoredCollection.swift b/LeStorage/StoredCollection.swift index 849b493..4c6165d 100644 --- a/LeStorage/StoredCollection.swift +++ b/LeStorage/StoredCollection.swift @@ -7,12 +7,6 @@ import Foundation -enum StoredCollectionError: Error { - case unmanagedHTTPMethod(method: String) - case missingApiCallCollection - case missingInstance -} - protocol CollectionHolder { associatedtype Item diff --git a/LeStorage/Utils/Errors.swift b/LeStorage/Utils/Errors.swift index 2eb0d9f..492fd86 100644 --- a/LeStorage/Utils/Errors.swift +++ b/LeStorage/Utils/Errors.swift @@ -20,14 +20,33 @@ public class ErrorUtils { } -public enum ServiceError: Error { +public enum ServiceError: Error, LocalizedError { case urlCreationError(url: String) case cantConvertToUUID(id: String) case missingUserName - case missingUserId case responseError(response: String) + + public var errorDescription: String? { + switch self { + case .urlCreationError(let url): + return "Can't create URL from \(url)" + case .cantConvertToUUID(let id): + return "Cant convert \(id) to UUID" + case .missingUserName: + return "There is no userName defined in the Settings" + case .responseError(let response): + return "The server returned an error: \(response)" + } + } } -public enum UUIDError: Error { +public enum UUIDError: Error, LocalizedError { case cantConvertString(string: String) + + public var errorDescription: String? { + switch self { + case .cantConvertString(let string): + return "cant convert string to UUID: \(string)" + } + } } diff --git a/LeStorage/Utils/FileUtils.swift b/LeStorage/Utils/FileUtils.swift index 5edf902..c906ab4 100644 --- a/LeStorage/Utils/FileUtils.swift +++ b/LeStorage/Utils/FileUtils.swift @@ -7,8 +7,15 @@ import Foundation -enum FileError : Error { +enum FileError: Error, LocalizedError { case documentDirectoryNotFound + + var errorDescription: String? { + switch self { + case .documentDirectoryNotFound: + return "The document directory has not been found" + } + } } class FileUtils { diff --git a/LeStorage/Utils/KeychainStore.swift b/LeStorage/Utils/KeychainStore.swift index 0f43c57..7e650b6 100644 --- a/LeStorage/Utils/KeychainStore.swift +++ b/LeStorage/Utils/KeychainStore.swift @@ -11,6 +11,17 @@ enum KeychainError: Error { case keychainItemNotFound(serverId: String) case unexpectedPasswordData case unhandledError(status: OSStatus) + + var errorDescription: String? { + switch self { + case .keychainItemNotFound(let serverId): + return "The keychainItem was not found: \(serverId)" + case .unexpectedPasswordData: + return "Keychain error: The data could not be converted to string" + case .unhandledError(let status): + return "Keychain error: Unmanaged status: \(status)" + } + } } class KeychainStore {