Skip to content

Commit 761edea

Browse files
committed
[Feat] Enable to throw some errors when use FirestoreQueryHandler closure.
1 parent e800b8e commit 761edea

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

Sources/SHFirestoreService/FirestoreQueryable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import FirebaseFirestore
1111

1212
#if os(iOS)
1313
public protocol FirestoreQueryable {
14-
typealias FirestoreQueryHandler = (FirestoreReference) -> Query
14+
typealias FirestoreQueryHandler = (FirestoreReference) throws -> Query
1515
typealias FirestoreQueryForPaginationHandler = (CollectionReference) -> Query
1616

1717
var queryForPagination: Query? { get }

Sources/SHFirestoreService/FirestoreService.swift

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -252,17 +252,21 @@ extension FirestoreService: FirestoreQueryable {
252252
guard let collectionRef = endpoint.reference as? CollectionReference else {
253253
return Fail(error: FirestoreServiceError.collectionNotFound).eraseToAnyPublisher()
254254
}
255-
let query = makeQuery(collectionRef)
256-
return query.getDocuments()
257-
.subscribe(on: backgroundQueue)
258-
.receive(on: backgroundQueue)
259-
.tryMap { querySnapshot in
260-
try querySnapshot.documents.map { snapshot in
261-
try snapshot.data(as: D.self)
255+
do {
256+
let query = try makeQuery(collectionRef)
257+
return query.getDocuments()
258+
.subscribe(on: backgroundQueue)
259+
.receive(on: backgroundQueue)
260+
.tryMap { querySnapshot in
261+
try querySnapshot.documents.map { snapshot in
262+
try snapshot.data(as: D.self)
263+
}
262264
}
263-
}
264-
.convertFirestoreServiceError()
265-
.eraseToAnyPublisher()
265+
.convertFirestoreServiceError()
266+
.eraseToAnyPublisher()
267+
} catch {
268+
return Fail(error: FirestoreServiceError.failedToMakeQuery(error)).eraseToAnyPublisher()
269+
}
266270
}
267271

268272
/// Notes:

Sources/SHFirestoreService/FirestoreServiceError.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import Foundation
1818
case encodingError(Error)
1919
case decodingError(Error)
2020
case failedTransaction(Error)
21+
case failedToMakeQuery(Error)
2122

2223
/// when paging
2324
case noMorePage
@@ -47,6 +48,8 @@ import Foundation
4748
return "Fail to retrieving collection: \(error?.localizedDescription ?? "Unknown error")"
4849
case .failedTransaction(let error):
4950
return "Fail transaction: \(error.localizedDescription)"
51+
case .failedToMakeQuery(let error):
52+
return "Fail to make query :\(error.localizedDescription)"
5053
}
5154
}
5255
}

0 commit comments

Comments
 (0)