From 9c653db3b3c5213021266f2fcfcefab705bd209c Mon Sep 17 00:00:00 2001 From: Joan Martin Date: Tue, 26 Jun 2018 10:45:57 +0200 Subject: [PATCH] Improving error management in repository and data source --- .../Network/ItemNetworkDataSource.swift | 12 ++++---- .../Common/Repository/DataSource.swift | 28 +++++++++++++++++++ .../Repository/InMemoryDataSource.swift | 12 ++++---- .../Repository/NetworkStorageRepository.swift | 12 ++++---- .../Common/Repository/Repository.swift | 28 +++++++++++++++++++ .../Repository/UserDefaultsDataSource.swift | 12 ++++---- 6 files changed, 80 insertions(+), 24 deletions(-) diff --git a/Example/MJSwiftCore/Core/Repository/Network/ItemNetworkDataSource.swift b/Example/MJSwiftCore/Core/Repository/Network/ItemNetworkDataSource.swift index dbb3d3df..a46e22f8 100644 --- a/Example/MJSwiftCore/Core/Repository/Network/ItemNetworkDataSource.swift +++ b/Example/MJSwiftCore/Core/Repository/Network/ItemNetworkDataSource.swift @@ -34,7 +34,7 @@ class ItemNetworkDataSource : AlamofireDataSource { case is IdQuery: return getById((query as! IdQuery).id) default: - fatalError("Undefined query class \(String(describing: type(of:query))) for method get on \(String(describing: type(of:self)))") + query.fatalError(.get, self) } } @@ -45,28 +45,28 @@ class ItemNetworkDataSource : AlamofireDataSource { case is SearchItemsQuery: return searchItems((query as! SearchItemsQuery).text) default: - fatalError("Undefined query class \(String(describing: type(of:query))) for method get on \(String(describing: type(of:self)))") + query.fatalError(.getAll, self) } } func putAll(_ array: [ItemEntity], in query: Query) -> Future<[ItemEntity]> { // Protocol-refactoring: NEEDS-IMPLEMENTATION OR ONLY CONFORMING TO GetDataSource - fatalError() + query.fatalError(.putAll, self) } func put(_ value: ItemEntity?, in query: Query) -> Future { // Protocol-refactoring: NEEDS-IMPLEMENTATION OR ONLY CONFORMING TO GetDataSource - fatalError() + query.fatalError(.put, self) } func deleteAll(_ query: Query) -> Future { // Protocol-refactoring: NEEDS-IMPLEMENTATION OR ONLY CONFORMING TO GetDataSource - fatalError() + query.fatalError(.deleteAll, self) } func delete(_ query: Query) -> Future { // Protocol-refactoring: NEEDS-IMPLEMENTATION OR ONLY CONFORMING TO GetDataSource - fatalError() + query.fatalError(.delete, self) } } diff --git a/MJSwiftCore/Classes/Common/Repository/DataSource.swift b/MJSwiftCore/Classes/Common/Repository/DataSource.swift index 0c8b3057..2a7176b5 100644 --- a/MJSwiftCore/Classes/Common/Repository/DataSource.swift +++ b/MJSwiftCore/Classes/Common/Repository/DataSource.swift @@ -16,9 +16,37 @@ import Foundation +public enum DataSourceCRUD : CustomStringConvertible { + case get + case getAll + case put + case putAll + case delete + case deleteAll + case custom(String) + + public var description: String { + switch self { + case .get: return "get" + case .getAll: return "getAll" + case .put: return "put" + case .putAll: return "putAll" + case .delete: return "delete" + case .deleteAll: return "deleteAll" + case .custom(let string): return string + } + } +} + /// Default query interface public protocol Query { } +extension Query { + public func fatalError(_ method: DataSourceCRUD, _ origin: D) -> Never where D : DataSource { + Swift.fatalError("Undefined query \(String(describing: self)) for method \(method) on \(String(describing: type(of: origin)))") + } +} + /// Blank query public class BlankQuery : Query { public init() {} diff --git a/MJSwiftCore/Classes/Common/Repository/InMemoryDataSource.swift b/MJSwiftCore/Classes/Common/Repository/InMemoryDataSource.swift index bd47cbc7..247b76ea 100644 --- a/MJSwiftCore/Classes/Common/Repository/InMemoryDataSource.swift +++ b/MJSwiftCore/Classes/Common/Repository/InMemoryDataSource.swift @@ -31,7 +31,7 @@ public class InMemoryDataSource : DataSource { } return Future(value) default: - fatalError() + query.fatalError(.get, self) } } @@ -43,7 +43,7 @@ public class InMemoryDataSource : DataSource { } return Future(CoreError.NotFound()) default: - fatalError() + query.fatalError(.getAll, self) } } @@ -57,7 +57,7 @@ public class InMemoryDataSource : DataSource { objects[query.key] = value return Future(value) default: - fatalError() + query.fatalError(.put, self) } } @@ -68,7 +68,7 @@ public class InMemoryDataSource : DataSource { arrays[query.key] = array return Future(array) default: - fatalError() + query.fatalError(.putAll, self) } } @@ -79,7 +79,7 @@ public class InMemoryDataSource : DataSource { objects[query.key] = nil return Future(Void()) default: - fatalError() + query.fatalError(.delete, self) } } @@ -90,7 +90,7 @@ public class InMemoryDataSource : DataSource { arrays[query.key] = nil return Future(Void()) default: - fatalError() + query.fatalError(.deleteAll, self) } } } diff --git a/MJSwiftCore/Classes/Common/Repository/NetworkStorageRepository.swift b/MJSwiftCore/Classes/Common/Repository/NetworkStorageRepository.swift index 98f30b22..81731d43 100644 --- a/MJSwiftCore/Classes/Common/Repository/NetworkStorageRepository.swift +++ b/MJSwiftCore/Classes/Common/Repository/NetworkStorageRepository.swift @@ -59,7 +59,7 @@ public class NetworkStorageRepository : Repository where N : DataSource, } } default: - fatalError("Undefined operation \(String(describing: operation)) for method get on \(String(describing: type(of:self)))") + operation.fatalError(.get, self) } }() } @@ -85,7 +85,7 @@ public class NetworkStorageRepository : Repository where N : DataSource, } } default: - fatalError("Undefined operation \(String(describing: operation)) for method get on \(String(describing: type(of:self)))") + operation.fatalError(.getAll, self) } }() } @@ -107,7 +107,7 @@ public class NetworkStorageRepository : Repository where N : DataSource, return self.network.put(value, in: query) } default: - fatalError("Undefined operation \(String(describing: operation)) for method get on \(String(describing: type(of:self)))") + operation.fatalError(.put, self) } }() } @@ -129,7 +129,7 @@ public class NetworkStorageRepository : Repository where N : DataSource, return self.network.putAll(array, in: query) } default: - fatalError("Undefined operation \(String(describing: operation)) for method get on \(String(describing: type(of:self)))") + operation.fatalError(.putAll, self) } }() } @@ -151,7 +151,7 @@ public class NetworkStorageRepository : Repository where N : DataSource, return self.network.delete(query) } default: - fatalError("Undefined operation \(String(describing: operation)) for method get on \(String(describing: type(of:self)))") + operation.fatalError(.delete, self) } }() } @@ -173,7 +173,7 @@ public class NetworkStorageRepository : Repository where N : DataSource, return self.network.deleteAll(query) } default: - fatalError("Undefined operation \(String(describing: operation)) for method get on \(String(describing: type(of:self)))") + operation.fatalError(.deleteAll, self) } }() } diff --git a/MJSwiftCore/Classes/Common/Repository/Repository.swift b/MJSwiftCore/Classes/Common/Repository/Repository.swift index d288e5c6..2835c908 100644 --- a/MJSwiftCore/Classes/Common/Repository/Repository.swift +++ b/MJSwiftCore/Classes/Common/Repository/Repository.swift @@ -16,11 +16,39 @@ import Foundation +public enum RepositoryCRUD : CustomStringConvertible { + case get + case getAll + case put + case putAll + case delete + case deleteAll + case custom(String) + + public var description: String { + switch self { + case .get: return "get" + case .getAll: return "getAll" + case .put: return "put" + case .putAll: return "putAll" + case .delete: return "delete" + case .deleteAll: return "deleteAll" + case .custom(let string): return string + } + } +} + /// /// An operation defines an abstraction on how data must be fetched (to which DataSource a query must be forwarded). /// public protocol Operation { } +extension Operation { + public func fatalError(_ method: RepositoryCRUD, _ origin: R) -> Never where R : Repository { + Swift.fatalError("Undefined operation \(String(describing: self)) for method \(method) on \(String(describing: type(of: origin)))") + } +} + /// An empty operation definition public class BlankOperation : Operation { public init() { } diff --git a/MJSwiftCore/Classes/Common/Repository/UserDefaultsDataSource.swift b/MJSwiftCore/Classes/Common/Repository/UserDefaultsDataSource.swift index 5cede940..85b6feeb 100644 --- a/MJSwiftCore/Classes/Common/Repository/UserDefaultsDataSource.swift +++ b/MJSwiftCore/Classes/Common/Repository/UserDefaultsDataSource.swift @@ -88,7 +88,7 @@ public class UserDefaultsDataSource : DataSource { } return Future(value) default: - fatalError() + query.fatalError(.get, self) } } @@ -100,7 +100,7 @@ public class UserDefaultsDataSource : DataSource { } return Future(array) default: - fatalError() + query.fatalError(.getAll, self) } } @@ -115,7 +115,7 @@ public class UserDefaultsDataSource : DataSource { userDefaults.synchronize() return Future(value) default: - fatalError() + query.fatalError(.put, self) } } @@ -127,7 +127,7 @@ public class UserDefaultsDataSource : DataSource { userDefaults.synchronize() return Future(array) default: - fatalError() + query.fatalError(.putAll, self) } } @@ -139,7 +139,7 @@ public class UserDefaultsDataSource : DataSource { userDefaults.synchronize() return Future(Void()) default: - fatalError() + query.fatalError(.delete, self) } } @@ -151,7 +151,7 @@ public class UserDefaultsDataSource : DataSource { userDefaults.synchronize() return Future(Void()) default: - fatalError() + query.fatalError(.deleteAll, self) } } }