Skip to content

Commit 42cf78c

Browse files
committed
update docs and errors
1 parent 01737fb commit 42cf78c

File tree

5 files changed

+22
-8
lines changed

5 files changed

+22
-8
lines changed

Sources/MongoSwift/BSON/BsonValue.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public struct Binary: BsonValue, Equatable, Codable {
200200
/// Throws an error if the base64 `String` is invalid.
201201
public init(base64: String, subtype: UInt8) throws {
202202
guard let dataObj = Data(base64Encoded: base64) else {
203-
throw MongoError.invalidValue(message: "failed to create Data object from invalid base64 string \(base64)")
203+
throw MongoError.invalidArgument(message: "failed to create Data object from invalid base64 string \(base64)")
204204
}
205205
self.init(data: dataObj, subtype: subtype)
206206
}

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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import libmongoc
55
public enum MongoError {
66
/// Thrown when an invalid connection string is provided when initializing a `MongoClient`.
77
case invalidUri(message: String)
8-
/// Thrown when a user-provided value is invalid.
9-
case invalidValue(message: String)
108
/// Thrown when a `MongoClient` is invalid.
119
case invalidClient()
1210
/// Thrown when the server sends an invalid response.
@@ -28,9 +26,13 @@ public enum MongoError {
2826
/// Thrown when there is an error involving a `ReadPreference`.
2927
case readPreferenceError(message: String)
3028
/// Thrown when there is an error involving a `WriteConcern`.
31-
case writeConcernError(message: String)
29+
case writeConcernError(code: Int32, message: String)
3230
/// Thrown when a user-provided argument is invalid.
3331
case invalidArgument(message: String)
32+
/// Thrown when there is an error executing a write command.
33+
case writeError(code: Int32, message: String)
34+
/// Thrown when there is an error executing a bulk write command.
35+
indirect case bulkWriteError(code: Int32, message: String, result: BulkWriteResult?, writeErrors: [MongoError]?, writeConcernError: MongoError?)
3436
}
3537

3638
/// An extension of `MongoError` to support printing out descriptive error messages.
@@ -41,9 +43,11 @@ extension MongoError: LocalizedError {
4143
let .invalidCollection(message), let .commandError(message),
4244
let .bsonParseError(_, _, message), let .bsonEncodeError(message),
4345
let .typeError(message), let .readConcernError(message),
44-
let .readPreferenceError(message), let .writeConcernError(message),
45-
let .invalidValue(message):
46+
let .readPreferenceError(message), let .invalidArgument(message):
4647
return message
48+
case let .writeConcernError(code, message), let .writeError(code, message),
49+
let .bulkWriteError(code, message, _, _, _):
50+
return "\(message) (error code \(code))"
4751
default:
4852
return nil
4953
}

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)