You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Guides/Error-Handling.md
+28-2Lines changed: 28 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,8 +15,7 @@
15
15
*[See Also](#see-also)
16
16
17
17
## Error Types
18
-
The driver uses errors to communicate that an operation failed, an assumption wasn't met, or that the user did something incorrectly. Applications that use the driver can in turn catch these errors and respond appropriately without crashing or resulting in an otherwise inconsistent state. To correctly model the different sources of errors, the driver defines three separate caregories of errors (`MongoServerError`, `MongoUserError`, `MongoRuntimeError`), each of which are protocols that inherit from the `MongoErrorProtocol` protocol. These protocols are defined in `MongoError.swift`, and the structs that conform to them are outlined here. The documentation for every public function that throws lists some of the errors that could possibly be thrown and the reasons they might be. The errors listed there are not comprehensive but will generally cover the most common cases.
19
-
18
+
The driver uses errors to communicate that an operation failed, an assumption wasn't met, or that the user did something incorrectly. Applications that use the driver can in turn catch these errors and respond appropriately without crashing or resulting in an otherwise inconsistent state. To correctly model the different sources of errors, the driver defines three separate categories of errors (`MongoServerError`, `MongoUserError`, `MongoRuntimeError`), each of which are protocols that inherit from the `MongoErrorProtocol` protocol. These protocols are defined in `MongoError.swift`, and the structs that conform to them are outlined here. The documentation for every public function that throws lists some of the errors that could possibly be thrown and the reasons they might be. The errors listed there are not comprehensive but will generally cover the most common cases.
20
19
21
20
### Server Errors
22
21
Server errors correspond to failures that occur in the database itself and are returned to the driver via some response to a command. Each error that conforms to `ServerError` contains at least one error code representing what went wrong on the server.
@@ -77,6 +76,15 @@ As part of the driver, `BSONEncoder` and `BSONDecoder` are implemented according
77
76
78
77
See the official documentation for both [`EncodingErrors`](https://developer.apple.com/documentation/swift/encodingerror) and [`DecodingErrors`](https://developer.apple.com/documentation/swift/decodingerror) for more information.
79
78
79
+
### BSON Errors
80
+
81
+
The BSON library has its own subset of errors that communicate issues when constructing or using BSON.
82
+
BSON Errors can be found in [Sources/MongoSwift/BSON/BSONError.swift](Sources/MongoSwift/BSON/BSONError.swift) and are as follows:
83
+
84
+
-`BSONError.InvalidArgumentError` - This error is thrown when a BSON type is being incorrectly constructed.
85
+
-`BSONError.InternalError` - This error is thrown when there is an issue that is a result of system failure (e.g, allocation issue).
86
+
-`BSONError.LogicError` - This error is thrown when there is an unexpected usage of the the API.
87
+
-`BSONError.DocumentTooLargeError` - This error is thrown when the document exceeds the maximum encoding size of 16MB.
80
88
81
89
## Examples
82
90
### Handling any error thrown by the driver
@@ -154,6 +162,24 @@ Result:
154
162
nInserted: 1
155
163
InsertedIds: [0: 2]
156
164
```
165
+
166
+
### Handling a BSONError
167
+
168
+
```swift
169
+
var string ="+1..23e8"
170
+
do {
171
+
returntryBSONDecimal128(string)
172
+
} catchlet bsonError as BSONError.InvalidArgumentError {
173
+
print(bsonError.message)
174
+
}
175
+
```
176
+
177
+
Output:
178
+
179
+
```plain
180
+
Invalid Decimal128 string +1..23e8
181
+
```
182
+
157
183
## See Also
158
184
-[Error handling in Swift](https://docs.swift.org/swift-book/LanguageGuide/ErrorHandling.html)
159
185
-[List of server error codes](https://github.com/mongodb/mongo/blob/master/src/mongo/base/error_codes.err)
0 commit comments