Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
Improving error management in repository and data source
Browse files Browse the repository at this point in the history
  • Loading branch information
Joan Martin committed Jun 26, 2018
1 parent 02563e3 commit 9c653db
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ItemNetworkDataSource : AlamofireDataSource {
case is IdQuery<String>:
return getById((query as! IdQuery<String>).id)
default:
fatalError("Undefined query class \(String(describing: type(of:query))) for method get on \(String(describing: type(of:self)))")
query.fatalError(.get, self)
}
}

Expand All @@ -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<ItemEntity> {
// Protocol-refactoring: NEEDS-IMPLEMENTATION OR ONLY CONFORMING TO GetDataSource
fatalError()
query.fatalError(.put, self)
}

func deleteAll(_ query: Query) -> Future<Void> {
// Protocol-refactoring: NEEDS-IMPLEMENTATION OR ONLY CONFORMING TO GetDataSource
fatalError()
query.fatalError(.deleteAll, self)
}

func delete(_ query: Query) -> Future<Void> {
// Protocol-refactoring: NEEDS-IMPLEMENTATION OR ONLY CONFORMING TO GetDataSource
fatalError()
query.fatalError(.delete, self)
}
}

Expand Down
28 changes: 28 additions & 0 deletions MJSwiftCore/Classes/Common/Repository/DataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<D>(_ 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() {}
Expand Down
12 changes: 6 additions & 6 deletions MJSwiftCore/Classes/Common/Repository/InMemoryDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class InMemoryDataSource<T> : DataSource {
}
return Future(value)
default:
fatalError()
query.fatalError(.get, self)
}
}

Expand All @@ -43,7 +43,7 @@ public class InMemoryDataSource<T> : DataSource {
}
return Future(CoreError.NotFound())
default:
fatalError()
query.fatalError(.getAll, self)
}
}

Expand All @@ -57,7 +57,7 @@ public class InMemoryDataSource<T> : DataSource {
objects[query.key] = value
return Future(value)
default:
fatalError()
query.fatalError(.put, self)
}
}

Expand All @@ -68,7 +68,7 @@ public class InMemoryDataSource<T> : DataSource {
arrays[query.key] = array
return Future(array)
default:
fatalError()
query.fatalError(.putAll, self)
}
}

Expand All @@ -79,7 +79,7 @@ public class InMemoryDataSource<T> : DataSource {
objects[query.key] = nil
return Future(Void())
default:
fatalError()
query.fatalError(.delete, self)
}
}

Expand All @@ -90,7 +90,7 @@ public class InMemoryDataSource<T> : DataSource {
arrays[query.key] = nil
return Future(Void())
default:
fatalError()
query.fatalError(.deleteAll, self)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class NetworkStorageRepository<N,S,T> : Repository where N : DataSource,
}
}
default:
fatalError("Undefined operation \(String(describing: operation)) for method get on \(String(describing: type(of:self)))")
operation.fatalError(.get, self)
}
}()
}
Expand All @@ -85,7 +85,7 @@ public class NetworkStorageRepository<N,S,T> : Repository where N : DataSource,
}
}
default:
fatalError("Undefined operation \(String(describing: operation)) for method get on \(String(describing: type(of:self)))")
operation.fatalError(.getAll, self)
}
}()
}
Expand All @@ -107,7 +107,7 @@ public class NetworkStorageRepository<N,S,T> : 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)
}
}()
}
Expand All @@ -129,7 +129,7 @@ public class NetworkStorageRepository<N,S,T> : 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)
}
}()
}
Expand All @@ -151,7 +151,7 @@ public class NetworkStorageRepository<N,S,T> : 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)
}
}()
}
Expand All @@ -173,7 +173,7 @@ public class NetworkStorageRepository<N,S,T> : 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)
}
}()
}
Expand Down
28 changes: 28 additions & 0 deletions MJSwiftCore/Classes/Common/Repository/Repository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> a query must be forwarded).
///
public protocol Operation { }

extension Operation {
public func fatalError<R>(_ 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() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public class UserDefaultsDataSource <T> : DataSource {
}
return Future(value)
default:
fatalError()
query.fatalError(.get, self)
}
}
Expand All @@ -100,7 +100,7 @@ public class UserDefaultsDataSource <T> : DataSource {
}
return Future(array)
default:
fatalError()
query.fatalError(.getAll, self)
}
}
Expand All @@ -115,7 +115,7 @@ public class UserDefaultsDataSource <T> : DataSource {
userDefaults.synchronize()
return Future(value)
default:
fatalError()
query.fatalError(.put, self)
}
}
Expand All @@ -127,7 +127,7 @@ public class UserDefaultsDataSource <T> : DataSource {
userDefaults.synchronize()
return Future(array)
default:
fatalError()
query.fatalError(.putAll, self)
}
}
Expand All @@ -139,7 +139,7 @@ public class UserDefaultsDataSource <T> : DataSource {
userDefaults.synchronize()
return Future(Void())
default:
fatalError()
query.fatalError(.delete, self)
}
}
Expand All @@ -151,7 +151,7 @@ public class UserDefaultsDataSource <T> : DataSource {
userDefaults.synchronize()
return Future(Void())
default:
fatalError()
query.fatalError(.deleteAll, self)
}
}
}

0 comments on commit 9c653db

Please sign in to comment.