Skip to content

Commit d4ece63

Browse files
committed
update docs and errors
1 parent cf8219a commit d4ece63

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

Sources/MongoSwift/MongoCollection+BulkWrite.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extension MongoCollection {
1313
*
1414
* - Throws:
1515
* - `MongoError.invalidArgument` if `requests` is empty
16-
* - `MongoError.commandError` if any error occurs while performing the writes
16+
* - `MongoError.bulkWriteError` if any error occurs while performing the writes
1717
*/
1818
@discardableResult
1919
public func bulkWrite(_ requests: [WriteModel], options: BulkWriteOptions? = nil) throws -> BulkWriteResult? {

Sources/MongoSwift/MongoCollection+FindAndModify.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ extension MongoCollection {
1010
* - options: Optional `FindOneAndDeleteOptions` to use when executing the command
1111
*
1212
* - Returns: The deleted document, represented as a `CollectionType`, or `nil` if no document was deleted.
13+
* - Throws:
14+
* - `MongoError.commandError` if there are any errors executing the command.
15+
* - A `DecodingError` if the deleted document cannot be decoded to a `CollectionType` value
1316
*/
1417
@discardableResult
1518
public func findOneAndDelete(_ filter: Document, options: FindOneAndDeleteOptions? = nil) throws -> CollectionType? {
@@ -26,6 +29,10 @@ extension MongoCollection {
2629
*
2730
* - Returns: A `CollectionType`, representing either the original document or its replacement,
2831
* depending on selected options, or `nil` if there was no match.
32+
* - Throws:
33+
* - `MongoError.commandError` if there are any errors executing the command.
34+
* - An `EncodingError` if `replacement` cannot be encoded to a `Document`
35+
* - A `DecodingError` if the replaced document cannot be decoded to a `CollectionType` value
2936
*/
3037
@discardableResult
3138
public func findOneAndReplace(filter: Document, replacement: CollectionType,
@@ -43,6 +50,9 @@ extension MongoCollection {
4350
*
4451
* - Returns: A `CollectionType` representing either the original or updated document,
4552
* depending on selected options, or `nil` if there was no match.
53+
* - Throws:
54+
* - `MongoError.commandError` if there are any errors executing the command.
55+
* - A `DecodingError` if the updated document cannot be decoded to a `CollectionType` value
4656
*/
4757
@discardableResult
4858
public func findOneAndUpdate(filter: Document, update: Document,

Sources/MongoSwift/MongoError.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ public enum MongoError {
2424
/// Thrown when there is an error involving a `ReadConcern`.
2525
case readConcernError(message: String)
2626
/// Thrown when there is an error involving a `WriteConcern`.
27-
case writeConcernError(message: String)
27+
case writeConcernError(code: Int32, message: String)
2828
/// Thrown when a user-provided argument is invalid.
2929
case invalidArgument(message: String)
30+
/// Thrown when there is an error executing a write command.
31+
case writeError(code: Int32, message: String)
32+
/// Thrown when there is an error executing a bulk write command.
33+
indirect case bulkWriteError(code: Int32, message: String, result: BulkWriteResult?, writeErrors: [MongoError]?, writeConcernError: MongoError?)
3034
}
3135

3236
/// An extension of `MongoError` to support printing out descriptive error messages.
@@ -37,8 +41,11 @@ extension MongoError: LocalizedError {
3741
let .invalidCollection(message), let .commandError(message),
3842
let .bsonParseError(_, _, message), let .bsonEncodeError(message),
3943
let .typeError(message), let .readConcernError(message),
40-
let .writeConcernError(message):
44+
let .invalidArgument(message):
4145
return message
46+
case let .writeConcernError(code, message), let .writeError(code, message),
47+
let .bulkWriteError(code, message, _, _, _):
48+
return "\(message) (error code \(code))"
4249
default:
4350
return nil
4451
}

Sources/MongoSwift/WriteConcern.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public class WriteConcern: Codable {
128128
let journalStr = String(describing: journal)
129129
let wStr = String(describing: w)
130130
let timeoutStr = String(describing: wtimeoutMS)
131-
throw MongoError.writeConcernError(message:
131+
throw MongoError.writeConcernError(code: 0, message:
132132
"Invalid combination of WriteConcern options: journal=\(journalStr), w=\(wStr), wtimeoutMS=\(timeoutStr)")
133133
}
134134
}

0 commit comments

Comments
 (0)