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
+30-27
Original file line number
Diff line number
Diff line change
@@ -14,53 +14,56 @@
14
14
*[See Also](#see-also)
15
15
16
16
## Error Types
17
-
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 types of errors (`ServerError`, `UserError`, `RuntimeError`), each of which conforms to the `MongoError` protocol. These errors are defined in `MongoError.swift` and 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.
17
+
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 (`ServerError`, `UserError`, `RuntimeError`), each of which are protocols that inherit from the `MongoError` 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.
18
18
19
-
**Error Labels:** Some types of errors may contain more specific information describing the context in which they occured. This information is conveyed through the usage of `errorLabels`. Specifically, any server error or connection related error may contain labels.
19
+
**Error Labels:** Some types of errors may contain more specific information describing the context in which they occured. Such errors conform to the `LabeledError` protocol, and the extra information is conveyed through the `errorLabels` property. Specifically, any server error or connection related error may contain labels.
20
20
21
21
### Server Errors
22
-
Server errors correspond to failures that occur in the database itself and are returned to the driver via some response to a command. Each `ServerError` case contains at least one error code representing what went wrong on the server.
22
+
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.
23
23
24
-
For an enumeration of the possible server error codes, [see this list](https://github.com/mongodb/mongo/blob/master/src/mongo/base/error_codes.err).
24
+
For an enumeration of the possible server error codes, [see this list](https://github.com/mongodb/mongo/blob/master/src/mongo/base/error_codes.yml).
- Thrown when a single write command fails on the server (e.g. insertOne, updateOne, updateMany).
32
+
-`BulkWriteError`
34
33
- Thrown when the server returns errors as part of an executed bulk write.
35
-
- If WriteConcernError is populated, writeErrors may not be.
36
-
-**Note:**`InsertMany` throws a `.bulkWriteError`, _not_ a `.writeError`.
34
+
- If WriteConcernFailure is populated, writeErrors may not be.
35
+
-**Note:**`InsertMany` throws a `BulkWriteError`, _not_ a `WriteError`.
37
36
38
37
39
38
### User Errors
40
39
User applications can sometimes cause errors by using the driver incorrectly (e.g. by passing invalid argument combinations). This category of error covers those cases.
41
40
42
-
The `UserError` cases are as follows:
43
-
-`.logicError(message: String)`
41
+
The possible errors that conform to `UserError` are as follows:
42
+
-`LogicError`
44
43
- Thrown when the user uses the driver incorrectly (e.g. advancing a dead cursor).
45
-
-`.invalidArgument(message: String)`
44
+
-`InvalidArgumentError`
46
45
- Thrown when user passes invalid arguments to some driver function.
47
46
48
47
49
48
### Runtime Errors
50
49
The driver may experience errors that happen at runtime unexpectedly. These errors don't fit neatly into the categories of occurring only server-side or only as part of the user's fault, so they are represented by their own set of cases.
51
50
52
51
The `RuntimeError` cases are as follows:
53
-
-`.internalError(message: String)`
52
+
-`InternalError`
54
53
- Thrown when something is null when it shouldn't be, the driver has an internal failure, or MongoSwift cannot understand a server response.
55
-
- This is generally indicative of a bug somewhere in the driver stack or a system related failure (e.g. memory allocation failure). If you experience an error that you think is the result of a bug, please file a bug report on GitHub or our Jira project.
- This is generally indicative of a bug somewhere in the driver stack or a system related failure (e.g. a memory allocation failure). If you experience an error that you think is the result of a bug, please file a bug report on GitHub or our Jira project.
55
+
-`ConnectionError`
57
56
- Thrown during any connection establishment / socket related errors.
58
-
-`.authenticationError(message: String)`
57
+
- This error also conforms to `LabeledError`.
58
+
-`AuthenticationError`
59
59
- Thrown when the driver is not authorized to perform a requested command (e.g. due to invalid credentials)
60
+
-`ServerSelectionError`
61
+
- Thrown when the driver was unable to select a server for an operation (e.g. due to a timeout or unsatisfiable read preference)
62
+
- See [the official MongoDB documentation](https://docs.mongodb.com/manual/core/read-preference-mechanics/) for more information.
60
63
61
64
62
65
### Encoding/Decoding Errors
63
-
As part of the driver, `BSONEncoder` and `BSONDecoder` are implemented according to the `Encoder` and `Decoder` protocols [defined in Apple's Foundation](https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types). User applications can use them to seamlessly convert between their Swift data structures and the BSON documents stored in the database. While this functionality is part of the public API, the driver itself also makes heavy use of it internally. During any encoding or decoding operations, errors can occur that prevent the data from being written to or read from BSON. In these cases, the driver throws an `EncodingError` or `DecodingError` as appropriate. These error types are not unique to MongoSwift and are commonly used by other encoder implementations, such as Foundation's `JSONEncoder`, so they do not conform to the `MongoError` protocol.
66
+
As part of the driver, `BSONEncoder` and `BSONDecoder` are implemented according to the `Encoder` and `Decoder` protocols [defined in Apple's Foundation](https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types). User applications can use them to seamlessly convert between their Swift data structures and the BSON documents stored in the database. While this functionality is part of the public API, the driver itself also makes heavy use of it internally. During any encoding or decoding operations, errors can occur that prevent the data from being written to or read from BSON. In these cases, the driver throws an `EncodingError` or `DecodingError` as appropriate. These error types are not unique to MongoSwift and are commonly used by other encoder implementations, such as Foundation's `JSONEncoder`, so they do not conform to the `MongoError` protocol or any of the other error protocols defined in the driver.
64
67
65
68
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.
0 commit comments