Skip to content

Commit 478df3f

Browse files
authored
SWIFT-704 Update async docstrings to reflect error propagation (#394)
1 parent da86e1b commit 478df3f

12 files changed

+357
-239
lines changed

Sources/MongoSwift/ClientSession.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ import NIO
1717
* ```
1818
* let opts = CollectionOptions(readConcern: ReadConcern(.majority), writeConcern: try WriteConcern(w: .majority))
1919
* let collection = database.collection("mycoll", options: opts)
20-
* try client.withSession { session in
21-
* try collection.insertOne(["x": 1], session: session)
22-
* try collection.find(["x": 1], session: session)
20+
* let futureCount = client.withSession { session in
21+
* collection.insertOne(["x": 1], session: session).flatMap { _ in
22+
* collection.countDocuments(session: session)
23+
* }
2324
* }
2425
* ```
2526
*

Sources/MongoSwift/ConnectionPool.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ internal class ConnectionPool {
3838
/// Initializes the pool using the provided `ConnectionString`.
3939
internal init(from connString: ConnectionString, options: TLSOptions? = nil) throws {
4040
guard let pool = mongoc_client_pool_new(connString._uri) else {
41-
throw InvalidArgumentError(message: "libmongoc not built with TLS support")
41+
throw InternalError(message: "Failed to initialize libmongoc client pool")
4242
}
4343

4444
guard mongoc_client_pool_set_error_api(pool, MONGOC_ERROR_API_VERSION_2) else {

Sources/MongoSwift/MongoClient.swift

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,6 @@ public class MongoClient {
242242
*
243243
* - Throws:
244244
* - A `InvalidArgumentError` if the connection string passed in is improperly formatted.
245-
* - A `InvalidArgumentError` if the connection string specifies the use of TLS but libmongoc was not
246-
* built with TLS support.
247245
*/
248246
public init(
249247
_ connectionString: String = "mongodb://localhost:27017",
@@ -303,7 +301,8 @@ public class MongoClient {
303301
}
304302
}
305303

306-
/// Starts a new `ClientSession` with the provided options.
304+
/// Starts a new `ClientSession` with the provided options. When you are done using this session, you must call
305+
/// `ClientSession.end()` on it.
307306
public func startSession(options: ClientSessionOptions? = nil) -> ClientSession {
308307
return ClientSession(client: self, options: options)
309308
}
@@ -312,8 +311,16 @@ public class MongoClient {
312311
* Starts a new `ClientSession` with the provided options and passes it to the provided closure.
313312
* The session is only valid within the body of the closure and will be ended after the body completes.
314313
*
315-
* - Throws:
316-
* - `RuntimeError.compatibilityError` if the deployment does not support sessions.
314+
* - Parameters:
315+
* - options: Options to use when creating the session.
316+
* - sessionBody: A closure which takes in a `ClientSession` and returns an `EventLoopFuture<T>`.
317+
*
318+
* - Returns:
319+
* An `EventLoopFuture<T>`, the return value of the user-provided closure.
320+
*
321+
* If the future fails, the error is likely one of the following:
322+
* - `CompatibilityError` if the deployment does not support sessions.
323+
* - `LogicError` if this client has already been closed.
317324
*/
318325
public func withSession<T>(
319326
options: ClientSessionOptions? = nil,
@@ -344,18 +351,21 @@ public class MongoClient {
344351
}
345352

346353
/**
347-
* Run the `listDatabases` command.
354+
* Retrieves a list of databases in this client's MongoDB deployment.
348355
*
349356
* - Parameters:
350357
* - filter: Optional `Document` specifying a filter that the listed databases must pass. This filter can be based
351358
* on the "name", "sizeOnDisk", "empty", or "shards" fields of the output.
352359
* - session: Optional `ClientSession` to use when executing this command.
353360
*
354-
* - Returns: An `EventLoopFuture<[DatabaseSpecification]>` containing the databases matching provided criteria.
361+
* - Returns:
362+
* An `EventLoopFuture<[DatabaseSpecification]>`. On success, the future contains an array of the specifications
363+
* of databases matching the provided criteria.
355364
*
356-
* - Throws:
357-
* - `LogicError` if the provided session is inactive.
358-
* - `EncodingError` if an error is encountered while encoding the options to BSON.
365+
* If the future fails, the error is likely one of the following:
366+
* - `LogicError` if the provided session is inactive.
367+
* - `LogicError` if this client has already been closed.
368+
* - `EncodingError` if an error is encountered while encoding the options to BSON.
359369
*
360370
* - SeeAlso: https://docs.mongodb.com/manual/reference/command/listDatabases/
361371
*/
@@ -373,16 +383,19 @@ public class MongoClient {
373383
}
374384

375385
/**
376-
* Get a list of `MongoDatabase`s.
386+
* Get a list of `MongoDatabase`s corresponding to the databases in this client's MongoDB deployment.
377387
*
378388
* - Parameters:
379389
* - filter: Optional `Document` specifying a filter on the names of the returned databases.
380390
* - session: Optional `ClientSession` to use when executing this command
381391
*
382-
* - Returns: An `EventLoopFuture<[MongoDatabase]>` containing databases that match the provided filter.
392+
* - Returns:
393+
* An `EventLoopFuture<[MongoDatabase]>`. On success, the future contains an array of `MongoDatabase`s that
394+
* match the provided filter.
383395
*
384-
* - Throws:
385-
* - `LogicError` if the provided session is inactive.
396+
* If the future fails, the error is likely one of the following:
397+
* - `LogicError` if the provided session is inactive.
398+
* - `LogicError` if this client has already been closed.
386399
*/
387400
public func listMongoDatabases(
388401
_ filter: Document? = nil,
@@ -392,16 +405,19 @@ public class MongoClient {
392405
}
393406

394407
/**
395-
* Get a list of names of databases.
408+
* Get the names of databases in this client's MongoDB deployment.
396409
*
397410
* - Parameters:
398411
* - filter: Optional `Document` specifying a filter on the names of the returned databases.
399412
* - session: Optional `ClientSession` to use when executing this command
400413
*
401-
* - Returns: An `EventLoopFuture<[String]>` containing names of databases that match the provided filter.
414+
* - Returns:
415+
* An `EventLoopFuture<[String]>`. On success, the future contains an array of names of databases that
416+
* match the provided filter.
402417
*
403-
* - Throws:
404-
* - `LogicError` if the provided session is inactive.
418+
* If the future fails, the error is likely one of the following:
419+
* - `LogicError` if the provided session is inactive.
420+
* - `LogicError` if this client has already been closed.
405421
*/
406422
public func listDatabaseNames(
407423
_ filter: Document? = nil,
@@ -426,7 +442,7 @@ public class MongoClient {
426442
* - name: the name of the database to retrieve
427443
* - options: Optional `DatabaseOptions` to use for the retrieved database
428444
*
429-
* - Returns: a `MongoDatabase` corresponding to the provided database name
445+
* - Returns: a `MongoDatabase` corresponding to the provided database name.
430446
*/
431447
public func db(_ name: String, options: DatabaseOptions? = nil) -> MongoDatabase {
432448
return MongoDatabase(name: name, client: self, options: options)

Sources/MongoSwift/MongoCollection+BulkWrite.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ extension MongoCollection {
1111
* - options: optional `BulkWriteOptions` to use while executing the operation.
1212
* - session: Optional `ClientSession` to use when executing this command
1313
*
14-
* - Returns: an `EventLoopFuture` containing the `BulkWriteResult`, or containing `nil` if the write concern is
15-
* unacknowledged.
14+
* - Returns:
15+
* An `EventLoopFuture<BulkWriteResult?>`. On success, the future contains either a `BulkWriteResult`, or
16+
* contains `nil` if the write concern is unacknowledged.
1617
*
17-
* - Throws:
18-
* - `InvalidArgumentError` if `requests` is empty.
19-
* - `LogicError` if the provided session is inactive.
20-
* - `BulkWriteError` if any error occurs while performing the writes. This includes errors that would
21-
* typically be thrown as `RuntimeError`s or `CommandError`s elsewhere.
22-
* - `EncodingError` if an error occurs while encoding the `CollectionType` or the options to BSON.
18+
* If the future fails, the error is likely one of the following:
19+
* - `InvalidArgumentError` if `requests` is empty.
20+
* - `LogicError` if the provided session is inactive.
21+
* - `LogicError` if this collection's parent client has already been closed.
22+
* - `BulkWriteError` if any error occurs while performing the writes. This includes errors that would
23+
* typically be propagated as `RuntimeError`s or `CommandError`s elsewhere.
24+
* - `EncodingError` if an error occurs while encoding the `CollectionType` or the options to BSON.
2325
*/
2426
public func bulkWrite(
2527
_ requests: [WriteModel<T>],

Sources/MongoSwift/MongoCollection+FindAndModify.swift

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ extension MongoCollection {
1111
* - options: Optional `FindOneAndDeleteOptions` to use when executing the command
1212
* - session: Optional `ClientSession` to use when executing this command
1313
*
14-
* - Returns: An `EventLoopFuture` containing the deleted document, represented as a `CollectionType`, or
15-
* containing `nil` if no document was deleted.
14+
* - Returns:
15+
* An `EventLoopFuture<CollectionType?>`. On success, contains either the deleted document, represented as a
16+
* `CollectionType`, or contains `nil` if no document was deleted.
1617
*
17-
* - Throws:
18-
* - `InvalidArgumentError` if any of the provided options are invalid.
19-
* - `LogicError` if the provided session is inactive.
20-
* - `CommandError` if an error occurs that prevents the command from executing.
21-
* - `WriteError` if an error occurs while executing the command.
22-
* - `DecodingError` if the deleted document cannot be decoded to a `CollectionType` value.
18+
* If the future fails, the error is likely one of the following:
19+
* - `InvalidArgumentError` if any of the provided options are invalid.
20+
* - `LogicError` if the provided session is inactive.
21+
* - `LogicError` if this collection's parent client has already been closed.
22+
* - `CommandError` if an error occurs that prevents the command from executing.
23+
* - `WriteError` if an error occurs while executing the command.
24+
* - `DecodingError` if the deleted document cannot be decoded to a `CollectionType` value.
2325
*/
2426
public func findOneAndDelete(
2527
_ filter: Document,
@@ -40,16 +42,19 @@ extension MongoCollection {
4042
* - options: Optional `FindOneAndReplaceOptions` to use when executing the command
4143
* - session: Optional `ClientSession` to use when executing this command
4244
*
43-
* - Returns: An `EventLoopFuture` containing a `CollectionType`, representing either the original document or its
44-
* replacement, depending on selected options, or containing `nil` if there was no match.
45+
* - Returns:
46+
* An `EventLoopFuture<CollectionType?>`. On success, contains a `CollectionType`, representing either the
47+
* original document or its replacement, depending on selected options; or containing `nil` if there was no
48+
* matching document.
4549
*
46-
* - Throws:
47-
* - `InvalidArgumentError` if any of the provided options are invalid.
48-
* - `LogicError` if the provided session is inactive.
49-
* - `CommandError` if an error occurs that prevents the command from executing.
50-
* - `WriteError` if an error occurs while executing the command.
51-
* - `DecodingError` if the replaced document cannot be decoded to a `CollectionType` value.
52-
* - `EncodingError` if `replacement` cannot be encoded to a `Document`.
50+
* If the future fails, the error is likely one of the following:
51+
* - `InvalidArgumentError` if any of the provided options are invalid.
52+
* - `LogicError` if the provided session is inactive.
53+
* - `LogicError` if this collection's parent client has already been closed.
54+
* - `CommandError` if an error occurs that prevents the command from executing.
55+
* - `WriteError` if an error occurs while executing the command.
56+
* - `DecodingError` if the replaced document cannot be decoded to a `CollectionType` value.
57+
* - `EncodingError` if `replacement` cannot be encoded to a `Document`.
5358
*/
5459
public func findOneAndReplace(
5560
filter: Document,
@@ -74,15 +79,17 @@ extension MongoCollection {
7479
* - options: Optional `FindOneAndUpdateOptions` to use when executing the command
7580
* - session: Optional `ClientSession` to use when executing this command
7681
*
77-
* - Returns: An `EventLoopFuture` containing a `CollectionType` representing either the original or updated
78-
* document, depending on selected options, or containing `nil` if there was no match.
82+
* - Returns:
83+
* An `EventLoopFuture<CollectionType>`. On success, contains either the original or updated document, depending
84+
* on selected options, or contains `nil` if there was no match.
7985
*
80-
* - Throws:
81-
* - `InvalidArgumentError` if any of the provided options are invalid.
82-
* - `LogicError` if the provided session is inactive.
83-
* - `CommandError` if an error occurs that prevents the command from executing.
84-
* - `WriteError` if an error occurs while executing the command.
85-
* - `DecodingError` if the updated document cannot be decoded to a `CollectionType` value.
86+
* If the future fails, the error is likely one of the following:
87+
* - `InvalidArgumentError` if any of the provided options are invalid.
88+
* - `LogicError` if the provided session is inactive.
89+
* - `LogicError` if this collection's parent client has already been closed.
90+
* - `CommandError` if an error occurs that prevents the command from executing.
91+
* - `WriteError` if an error occurs while executing the command.
92+
* - `DecodingError` if the updated document cannot be decoded to a `CollectionType` value.
8693
*/
8794
public func findOneAndUpdate(
8895
filter: Document,
@@ -96,12 +103,16 @@ extension MongoCollection {
96103
/**
97104
* A private helper method for findAndModify operations to use.
98105
*
99-
* - Throws:
100-
* - `InvalidArgumentError` if any of the provided options are invalid.
101-
* - `LogicError` if the provided session is inactive.
102-
* - `CommandError` if an error occurs that prevents the command from executing.
103-
* - `WriteError` if an error occurs while executing the command.
104-
* - `DecodingError` if the updated document cannot be decoded to a `CollectionType` value.
106+
* - Returns:
107+
* An `EventLoopFuture<CollectionType?>. On success, contains the document returned by the server, if one exists.
108+
*
109+
* If the future fails, the error is likely one of the following:
110+
* - `InvalidArgumentError` if any of the provided options are invalid.
111+
* - `LogicError` if the provided session is inactive.
112+
* - `LogicError` if this collection's parent client has already been closed.
113+
* - `CommandError` if an error occurs that prevents the command from executing.
114+
* - `WriteError` if an error occurs while executing the command.
115+
* - `DecodingError` if the updated document cannot be decoded to a `CollectionType` value.
105116
*/
106117
private func findAndModify(
107118
filter: Document,

0 commit comments

Comments
 (0)